UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: What are these constants private to?

Forums > 100 Days of Swift

In project28, we are asked to add a file downloaded from Paul's github to our project. It is a file created by MIT to make the keychain easier to use. But I noticed that there are a bunch of constants declared at the beginning of the code outside of the class definition, yet they are marked with private. So, if these constants aren't properties of any class or struct, then what are they private to? Is this the same as declaring them inside of the class definition, but marking them as fileprivate?

import Foundation

private let SecMatchLimit: String! = kSecMatchLimit as String
private let SecReturnData: String! = kSecReturnData as String
private let SecReturnPersistentRef: String! = kSecReturnPersistentRef as String
private let SecValueData: String! = kSecValueData as String
private let SecAttrAccessible: String! = kSecAttrAccessible as String
private let SecClass: String! = kSecClass as String
private let SecAttrService: String! = kSecAttrService as String
private let SecAttrGeneric: String! = kSecAttrGeneric as String
private let SecAttrAccount: String! = kSecAttrAccount as String
private let SecAttrAccessGroup: String! = kSecAttrAccessGroup as String
private let SecReturnAttributes: String = kSecReturnAttributes as String

/// KeychainWrapper is a class to help make Keychain access in Swift more straightforward. It is designed to make accessing the Keychain services more like using NSUserDefaults, which is much more familiar to people.
open class KeychainWrapper {
    //KeychainWrapper class code goes here...


Is this the same as declaring them inside of the class definition, but marking them as fileprivate?

Yes, private access at the top level functions the same as fileprivate.

In fact, I'd suggest in such cases to actually use fileprivate rather than private, as it communicates what's going on in a clearer fashion. They may function the same, but if this code had used fileprivate instead, you wouldn't have had to ask this question, no?


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.