class User { fileprivate var name = "dacai" private var age = "18" var sex = "dacai" var accessPrivateAge: String { return age } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() print(User().accessPrivateAge) //想访问private的 你可以这样玩 print(User().name) //print(User().age) 报错 :因为private } }
在另一个文件中:
class Test: NSObject { func Visit() { print(User().sex) //print(User().age) 报错 :因为fileprivate } }原文链接:https://www.f2er.com/swift/322624.html