Swift属性(九)
2500 ワード
/**
、 。
。
*/
class Person{
//
var name: String?
var age: Int?
}
struct Model{
//
var p: Person?
var index: Int
}
let p = Person()
p.name = "xiaoma"
p.age = 18
let model: Model = Model(p: p, index: 1)
開発では、ロードプロパティで特にUIで怠惰なロードを利用することがよくあります.
//lazy var : = { }()
class MyView: UIView {
lazy var label = UILabel()
lazy var tableView: UITableView = {() -> UITableView in
let tb = UITableView(frame: CGRect(x: 0, y: 0, width: 300, height: 300), style: .plain)
// tb.delegate = self
// tb.dataSource = self
return tb
}()
}
class NewClass {
var firstName = " "
var lastName = "XiaoMa"
var fullName: String {
get{
return firstName+lastName
}
}
}
print(NewClass().fullName)
// set
class Student {
var book = "iOS"
var changeBook: String {
get {
return book
}
set(newBook) {
book = newBook
}
}
}
let s = Student()
print(s.changeBook)
s.changeBook = "Swift"
print(s.changeBook)
//
struct Animal {
var kind = ""
var age = 0
// mutating
mutating func changeAnimalKind(newKind: String) {
self.kind = newKind
}
// :
var changeKind: String {
get {
return self.kind
}
set(newKind) {
self.kind = newKind
}
}
}
var ani = Animal(kind: " ", age: 3)
print(ani.kind)
ani.changeAnimalKind(newKind: " ")
print(ani.kind)
ani.changeKind = " "
print(ani.kind)
class example {
var age : Int = 10 {
//
willSet {
print("example age newValue \(newValue)")
}
////
didSet {
print("example oldvalue \(oldValue)")
}
}
}
let ex = example()
print(ex.age)
ex.age = 20
print(ex.age)
class CarClass {
static var whole = 4
}
//
print(CarClass.whole)
Swift HelloWorld(一)Swift基本データ型(二)Swift文字列(三)Swift閉パケット(Closures)(四)Swift集合型(五)Swift関数(六)Swift類(七)Swift構造体、列挙(八)Swift属性(九)Swiftメソッド(十)