Swift5 属性与方法
1. Swift 存储属性
class Phone {
var system = "iOS"
let brand: String
var price: Int
init(brand: String, price: Int) {
self.brand = brand
self.price = price
print("brand: \(brand), price: \(price)")
}
}
let iPhone = Phone(brand: "iPhone", price: 5999)
iPhone.price -= 600
class Consumer {
var money: Int
lazy var phone: Phone = Phone(brand: "iPhone", price: 6999)
init(money: Int) {
self.money = money
}
}
var richMan = Consumer(money: 100_000)
print(richMan.phone)
2. Swift 计算属性
class Android {
let system: String = "android"
var version = "12"
var apiLevel: String = "31"
var info: String {
get {
return "system: \(system), version: \(version), level: \(apiLevel)"
}
set {
version = newValue.split(separator: "-").first?.description ?? ""
apiLevel = newValue.split(separator: "-").last?.description ?? ""
}
}
var price: ClosedRange<Int> {
get {
if (apiLevel > "30") {
return 4000...6999
} else {
return 1000...3999
}
}
set(newPrice) {
if (newPrice.lowerBound > 3999) {
apiLevel = "31"
version = "12"
} else {
apiLevel = "30"
version = "11"
}
}
}
}
var newPhone = Android()
print(newPhone.info)
newPhone.info = "11-30"
print(newPhone.info)
newPhone.price = 4000...4999
print(newPhone.info)
3. Swift 属性监听器
class iOS {
var brand: String {
willSet {
print("new value : \(newValue)")
}
didSet {
print("old value : \(oldValue)")
}
}
var price: Int {
willSet(newPrice) {
print("new price : \(newPrice)")
}
didSet(oldPrice) {
print("old price : \(oldPrice)")
}
}
init(brand: String, price: Int) {
self.brand = brand
self.price = price
print("brand: \(brand), price: \(price)")
}
}
let newIPhone = iOS(brand: "iphone 12", price: 5999)
newIPhone.brand = "iphone 13"
newIPhone.price = 6999
4. Swift 属性包装器
@propertyWrapper
struct StringNotEmpty {
var value: String
init() {
self.value = "default string"
}
var wrappedValue: String {
get { return value }
set {
if (newValue.count > 0) {
self.value = newValue
} else {
self.value = "default string"
}
}
}
}
class Student: CustomStringConvertible {
@StringNotEmpty
var name: String
var description: String {
return "student's name is \(name)"
}
}
let student = Student()
student.name = ""
print(student)
5. Swift 静态属性与静态方法
class BaseClass {
static var param = "param"
static var computeParam: String {
return "computeParam"
}
class var openParam: String {
return "openParam"
}
static func method() {
print("static method")
}
class func openMethod() {
print("static openMethod")
}
}
class SubClass : BaseClass {
override class var openParam: String {
return "SubClass openParam"
}
override class func openMethod() {
print("SubClass openMethod")
}
}
BaseClass.param
BaseClass.computeParam
BaseClass.openParam
SubClass.openParam
BaseClass.method()
BaseClass.openMethod()
SubClass.openMethod()
6. Swift 下标方法
class CustomList {
var list: Array<String>
init(list: String...) {
self.list = list
}
subscript(index: Int) -> String {
set {
list[index] = newValue
}
get {
return list[index]
}
}
}
let list = CustomList(list: "1", "2", "3")
list[0] = "item 1"
print(list[1])
|