swift 学习(6)Properties

  • 实在无法用自己的语言表示出来,以后就当是读书摘录吧。

• Properties are variables and constants that are part of a named type.
• Stored properties allocate memory to store a value.
• Computed properties are calculated each time your code requests them and aren’t stored as a value in memory.
• The static keyword marks a type property that’s universal to all instances of a particular type.
• The lazy keyword prevents a value of a stored property from being calculated until your code uses it for the first time. You’ll want to use lazy initialization when a property’s initial value is computationally intensive or when you won’t know the initial value of a property until after you’ve initialized the object.

===符号

=== 是比较引用指向的地址是否一致。
== 是比较值是否一致。

stored properties

struct Car {
  let make: String
  let color: String
}

Values like these are called properties. The two properties of Car are both stored properties, which means they store actual string values for each instance of Car.

  • Default values
struct Contact {
  var fullName: String
  let emailAddress: String
  var type = "Friend"
}

computed properties

Some properties calculate values rather than store them; that means there’s no actual memory allocated for them, but they get calculated on-the-fly each time you access them. Naturally, these are called computed properties.

有些属性是没有为他们分配实际的存储空间的,每次读取他们都是经过即时计算得到的。

  • Getter and setter

计算属性会默认实现get,可选实现set方法。当不写get、set关键字是默认当做get实现。

struct TV {
    var heigth : Double
    var width : Double
    var diagonal : Int {
        get {
            let result = sqrt(heigth * heigth + width * width)
            let roundResult = result.rounded()
            return Int(roundResult);
        }
        set {
            //设定屏幕比例
            let ratioWidth = 16.0
            let ratioHeight = 9.0
            //设置计算属性的值,实际上还是设置存储属性的值
            heigth = Double(newValue) * ratioHeight / sqrt(ratioWidth * ratioWidth + ratioHeight * ratioHeight)
            width = heigth * ratioWidth / ratioHeight
        }
       
    }
}

存储属性可以是常量也可以是变量,但是计算属性只能是变量。

While a stored property can be a constant or a variable, a computed property must be defined as a variable
Type properties

However, the type itself may also need properties that are common across all
instances. These properties are called type properties.

然而,类型本身也可能需要在所有的共同的属性
实例。 这些属性称为类型属性。

使用 static 修饰。

struct Level {
  static var highestLevel = 1
  let id: Int
  var boss: String
  var unlocked: Bool
}

Property observers

willSet
didSet

Lazy properties

If you have a property that might take some time to calculate, you don’t want to slow things down until you actually need the property. Say hello to the lazy stored property. This could be useful for such things as downloading a user’s profile picture or making a serious calculation.

如果你有一个属性可能需要一些时间来计算,你不想减慢的事情,直到你实际需要的属性。 向懒惰的存储属性打个招呼吧。 这可能对下载用户的个人资料图片或进行严重的计算有用。

struct Circle {
  lazy var pi = {
    return ((4.0 * atan(1.0 / 5.0)) - atan(1.0 / 239.0)) * 4.0
  }()
  var radius = 0.0
  var circumference: Double {
    mutating get {
      return pi * radius * 2
    }
  }
  init (radius: Double) {
    self.radius = radius
  }
}

Key points

  • Properties are variables and constants that are part of a named type.
  • Stored properties allocate memory to store a value.
  • Computed properties are calculated each time your code requests them and aren’t stored as a value in memory.
  • The static keyword marks a type property that’s universal to all instances of a particular type.
  • The lazy keyword prevents a value of a stored property from being calculated until your code uses it for the first time. You’ll want to use lazy initialization when a property’s initial value is computationally intensive or when you won’t know the initial value of a property until after you’ve initialized the object.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,783评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,396评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,834评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,036评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,035评论 5 362
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,242评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,727评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,376评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,415评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,463评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,140评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,734评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,809评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,028评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,521评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,119评论 2 341

推荐阅读更多精彩内容