TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 11: How to limit access to internal data using access control - private var not working?

Forums > SwiftUI

In the following code provided in the lesson:

struct BankAccount {
    private var funds = 0  // changed to private to protect the property

    mutating func deposit(amount: Int) {
        funds += amount
    }

    mutating func withdraw(amount: Int) -> Bool {
        if funds >= amount {
            funds -= amount
            return true
        } else {
            return false
        }
    }
}

my understanding is that by adding 'private' to the property, the following code should not be possible:

var account = BankAccount()
account.funds -= 1000

Printing account.funds shows I am able to access the property from outside when run in playgrounds.

Again this is all code run in playgrounds and I have the same issue with my Checkpoint 6 code and from Checkpoint 6 solutions from the forums.

2      

I pasted your code into a Playground. After creating the account, then using the account.deposit(1000) method, I tried accessing the funds var.

I received an error stating:

This property is defined on a BankAccount and may not be available in this context.

Even Xcode helps. After typing the account var, Xcode only offers me the deposit() and withdraw()methods as type-ahead suggestions.

Keep Coding!

2      

Thanks for the reply!!!!

I'm using Xcode Version 15.2 (15C500b)

Don't know if that helps.

Also to be clear, this isn't my code, rather it's code from Day 11 of 100 days with SwiftUI from this website. The only thing I changed was var funds = 0 to private var funds = 0 per the suggestion of the website.

I restarted Xcode opened a new playground and can't recreate the error code that you get.

Xcode doesn't offer me 'funds' as a type ahead suggestion, but I'm able to type out account.funds = -1500 without any problem.

This seems to be a bug.

Below is the entire code:

import UIKit

struct BankAccount {
    private var funds = 0

    mutating func deposit(amount: Int) {
        funds += amount
    }

    mutating func withdraw(amount: Int) -> Bool {
        if funds >= amount {
            funds -= amount
            return true
        } else {
            return false
        }
    }
}
var account = BankAccount()

account.deposit(amount: 1000)
print(account.funds)
account.funds -= 1500
print(account.funds)

The output is:

1000

-500

Let me know if you can recreate this on your end.

2      

same version of XCode. Cannot access private.

2      

I recently updated to Sonoma 14.2.1

There shouldn't be any known reason that 'private' doesn't work. If it's some security flaw or bug, how do I remedy the situation?

Any recommendations on where to go for answers would be appreciated.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.