Stored Properties 与 Computed Properties

About Swift

Stored Properties

In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).

You can provide a default value for a stored property as part of its definition, as described in Default Property Values. You can also set and modify the initial value for a stored property during initialization. This is true even for constant stored properties, as described in Assigning Constant Properties During Initialization.

Computed Properties

In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

细节

computed properties 本质上只是一个setter,getter方法,但在使用上却跟使用 stored properties 时效果一致, 所以,我们可以将 computed properties 理解为方法.

两者的一些区别

以及使用心得

源码

//
// Model.swift
// ModelDemo
//
// Created by YouXianMing on 15/9/30.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
// import UIKit class Model: NSObject { // MARK: Stored Properties private var cellFlag : String?
private var cellHeight : CGFloat?
internal var data : AnyObject? // MARK: Computed Properties var rowHeight : CGFloat { get { if let _ = cellHeight { return cellHeight! } else { return
}
} set(newVal) { cellHeight = newVal
}
} var reusableCellIdentifier : String { get { if let _ = cellFlag { return cellFlag! } else { return "reusableCellIdentifier"
}
} set(newVal) { cellFlag = newVal
}
} // MARK: Method
override init() { super.init()
} init(reusableCellIdentifier : String, rowHeight : CGFloat, data : AnyObject?) { super.init() self.reusableCellIdentifier = reusableCellIdentifier
self.rowHeight = rowHeight
self.data = data
}
}
//
// ViewController.swift
// ModelDemo
//
// Created by YouXianMing on 15/9/30.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
// import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let model1 = Model()
print(model1.rowHeight)
print(model1.reusableCellIdentifier)
print(model1.data) let model2 = Model(reusableCellIdentifier: "cellTypeOne", rowHeight: , data: nil)
print(model2.rowHeight)
print(model2.reusableCellIdentifier)
print(model2.data)
}
}

Stored Properties 与 Computed Properties的更多相关文章

  1. Computed Properties vs Property Requirements - protocol

    In addition to stored properties, classes, structures, and enumerations can define computed properti ...

  2. 2.3 The Object Model -- Computed Properties

    一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...

  3. 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?

    有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...

  4. [Vue] Use Vue.js Component Computed Properties

    You can add computed properties to a component to calculate a property on the fly. The benefit of th ...

  5. Properties类读写.properties配置文件

    package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...

  6. SSM-MyBatis-04:Mybatis中使用properties整合jdbc.properties

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------properties整合jdbc.properties首先准备好jdbc.properties,里面的key值写 ...

  7. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  8. log4j.properties与db.properties

    log4j.properties与db.properties db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql:///mybatis?useUnico ...

  9. 【Properties】获取Properties文件

    获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...

随机推荐

  1. 将AJAX Post的Data转为对应的Class

    在使用DataTables从服务端获取数据时,在非MVC的情况下没有MVC的自动绑定功能,所以需要自己写一个绑定,将Post过来的InputStream转为对应的类. HTML: <form i ...

  2. Java生成指定范围内的工具类

    /** * 生成[min, max]之间的随机整数 * * @param min 最小整数 * @param max 最大整数 * @return * @author jqlin */ private ...

  3. 怎么掌握微信小程序的取值、传值、数据存储

    小程序界面设定如下 父级页面:A界面 子级页面:B界面 异级页面:C界面 一.本页面取值(A界面→A界面) a.例如:input输入文字获取到value(键盘输入时触发) wxml: <inpu ...

  4. asdfasdfasdfasdf

  5. Ionic3与Angular4新特性

    之前(17年3月底)Angular4.0.0正式发布,这个月(4月十几号)Ionic3又发布了,很多人看到这个估计都是一脸懵圈,其实,Angular4只是Angular2的后续版本,Ionic3也是I ...

  6. C#中is与as的区别分析

    这篇文章主要介绍了C#中is与as的区别,较为详细的分析了is与as的原理与特性及用法区别,具有很好的学习借鉴价值,需要的朋友可以参考下 本文实例分析了C#中is与as的区别,分享给大家供大家参考.具 ...

  7. IBatis项目中com.ibatis.common.xml.NodeletException的解决方案

    一 现象: 今天在写IBatis项目是总是提示我有如下异常: com.ibatis.common.xml.NodeletException java.util.NoSuchElementExcepti ...

  8. iOS调用系统页面中文显示

    在开发的过程中,我们会接入很多的sdk,比如相机,相册,是否允许获取位置等,大多数的情况下是默认显示英文, 在plist文件里面添加一个key就可以了,如下图: key:Localization na ...

  9. ActiveMQ 报错 Temporary Store limit is 51200 mb

    ERROR | Temporary Store limit is 51200 mb, whilst the temporary data directory: D:\tool\apache-actil ...

  10. Differences between page and segment

    https://techdifferences.com/difference-between-paging-and-segmentation-in-os.html how does paging so ...