Swift private(set) All In One
阅读原文时间:2023年07月09日阅读:1

Swift private(set) All In One

SwiftUI

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18

public private(set)

SDK

struct TrackedString {
    // private(set) 对内可修改, 对外可见
    private(set) var numberOfEdits = 0
    var value: String = "" {
        didSet {
            numberOfEdits += 1
        }
    }
}

public private(set)

public struct Counter {
    // I specify that only the setter is private!
    public private(set) var count: Int
}

https://www.natashatherobot.com/swift-magic-public-getter-private-setter/

struct Person {
  // private(set) 对外只读,对内可写
  private(set) var name: String = "xgqfrms"
  // func getName() {
  //   return self.name;
  // }
  func setName(name: String) {
    return self.name = name;
  }
  // public let age: Int = 18
  private var age: Int = 18
  // 闭包 closure, 对内开放,对外关闭
  func getAge() {
    return self.age;
  }
  func setAge(name: Int) {
    return self.age = age;
  }
}

https://www.youtube.com/watch?v=4GjXq2Sr55Q&ab_channel=Stanford

https://www.tryswift.co/



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章