1.存儲類型的屬性的初始化
- 存儲類型
struct YXRect {
var origin:CGPoint
var width:CGFloat
var height:CGFloat
}
- 存儲類型的屬性的初始化,存儲類型的屬性,在結(jié)構(gòu)體或者對象創(chuàng)建的時候必須保證初始化一次
- 在初始化方法中初始化,在聲明的時候沒有初始化,那么在init函數(shù)中就是必須有的
//錯誤
//class YXRect {
// var origin:CGPoint
// var width:CGFloat
// var height:CGFloat
// init(width:CGFloat,height:CGFloat){
// self.width = width
// self.height = height
// }
//}
class YXRect {
var origin:CGPoint
var width:CGFloat
var height:CGFloat
init(origin:CGPoint,width:CGFloat,height:CGFloat){
self.origin = origin
self.width = width
self.height = height
}
}
- 直接在聲明的時候初始化,在聲明的時候有初始化的炕舵,在init函數(shù)中就不是必須的了
class YXRect {
var origin:CGPoint = CGPoint(x:1,y:1)
var width:CGFloat
var height:CGFloat
init(width:CGFloat,height:CGFloat){
self.width = width
self.height = height
}
}
- optional屬性不強(qiáng)制初始化
//正確
class YXRect {
var origin:CGPoint?
var width:CGFloat
var height:CGFloat
init(width:CGFloat,height:CGFloat){
self.width = width
self.height = height
}
}
- struct會默認(rèn)提供全部屬性的init函數(shù),而class不會
struct YXRect {
var origin:CGPoint
var width:CGFloat
var height:CGFloat
}
//錯誤
//class YXRect4 {
// var origin:CGPoint
// var width:CGFloat
// var height:CGFloat
//
//}
class YXRect{
var origin:CGPoint
var width:CGFloat
var height:CGFloat
init(origin:CGPoint,width:CGFloat,height:CGFloat){
self.origin = origin
self.width = width
self.height = height
}
}
2.存儲類型屬性的監(jiān)聽
struct YXRect {
var origin:CGPoint
var width:CGFloat{
willSet(newValue){
print("will set \(newValue)")
}
didSet(newValue){
print("did set \(newValue)")
if(newValue>200){
width = 200
}
}
}
var height:CGFloat
init(origin:CGPoint,width:CGFloat,height:CGFloat){
self.origin = origin
self.width = width
self.height = height
}
}
3.計算類型的屬性
- 計算屬性的獲取
struct YXRect {
var origin:CGPoint
var width:CGFloat
var height:CGFloat
init(origin:CGPoint,width:CGFloat,height:CGFloat){
self.origin = origin
self.width = width
self.height = height
}
//只有獲取的時候不需要加上get關(guān)鍵字
var center : CGPoint {
print("獲取center")
let x = self.origin.x + self.width*0.5
let y = self.origin.y + self.height * 0.5
return CGPoint(x:x,y:y)
}
}
- 計算屬性的設(shè)置
struct YXRect {
var origin:CGPoint
var width:CGFloat
var height:CGFloat
init(origin:CGPoint,width:CGFloat,height:CGFloat){
self.origin = origin
self.width = width
self.height = height
}
//有獲取阐污,也有設(shè)置就需要加上get 和 set方法
var center : CGPoint {
get{
print("獲取center2")
let x = self.origin.x + self.width*0.5
let y = self.origin.y + self.height * 0.5
return CGPoint(x:x,y:y)
}
set(newValue){
print("設(shè)置center2")
self.origin.x = newValue.x - self.width * 0.5
self.origin.y = newValue.y - self.height * 0.5
}
}
}
4.懶加載屬性
class Account{
var name : String
//懶加載屬性的self容易造成鏈循環(huán)蛇受,從而導(dǎo)致self不能釋放财岔,此處應(yīng)使用unowned
lazy var hellow :String = {[unowned self] in
print("初始化拉")
return "hellow \(self.name)"
}()
init(name:String){
self.name = name
}
}
var accout = Account(name:"aaa")
accout.hellow
accout.hellow
accout.hellow
//"初始化啦"只打印一次
5.類的靜態(tài)屬性
Swift中可以像java一樣在一個類中聲明一個靜態(tài)變量
而OC必須借助類方法才能達(dá)到這樣的效果
- Swift
class Person {
static var type = "地球人"
}
//這樣就可以直接訪問
Person.type //地球人
- OC
@interface Person : NSObject
+(NSString *)type;
@end
@implementation Person
static NSString *type = @"地球人";
+(NSString *)type{
return type;
}
@end
[Person type];
可見相比較而言還是Swift語法吞杭,更加簡練一些呐籽。