Stored Properties 与 Computed Properties
Stored Properties 与 Computed Properties

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的更多相关文章
- Computed Properties vs Property Requirements - protocol
In addition to stored properties, classes, structures, and enumerations can define computed properti ...
- 2.3 The Object Model -- Computed Properties
一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...
- 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?
有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...
- [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 ...
- Properties类读写.properties配置文件
package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...
- SSM-MyBatis-04:Mybatis中使用properties整合jdbc.properties
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------properties整合jdbc.properties首先准备好jdbc.properties,里面的key值写 ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
- log4j.properties与db.properties
log4j.properties与db.properties db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql:///mybatis?useUnico ...
- 【Properties】获取Properties文件
获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...
随机推荐
- mongodb 错误 SCRAM-SHA-1 authentication failed for --转
log 日志错误信息 2018-10-24T16:14:42.244+0800 I NETWORK [initandlisten] connection accepted from 192.168.1 ...
- LogStash启动报错:<Redis::CommandError: ERR unknown command 'script'>与batch_count 的 配置
环境条件: 系统版本:centos 6.8 logstash版本:6.3.2 redis版本:2.4 logstash input配置: input { redis { host => &qu ...
- mysql保留两位小数
--这个是保留整数位 SELECT CONVERT(4545.1366,DECIMAL); --这个是保留两位小数 ,)); --这个是截取两位,并不会四舍五入保留两位小数 );
- 01-python基础
前几天, 觉得python简单的不行, 没有仔细做笔记, 然后今天翻了下前几天看的东西, 还是记下来吧 对于python2.7 和 python3 , 建议使用python3 的模式编程, 然后使用p ...
- C#中给滚动条增加鼠标滚动轮控制事件
当滚动条的父控件获得焦点时,可以使用鼠标的滚动轮来控制滚动条 public partial class Form1 : Form { public Form1() { InitializeCompon ...
- activemq的三种通信方式
一.安装与启动 1.下载安装activemq,下载地址:http://activemq.apache.org/download.html. 2.安装完成后,进入其所在目录的bin目录下面,根据系统位数 ...
- Core中使用Hangfire
之前使用Quartz.Net,后来发现hangfire对Core的继承更加的好,而且自带管理后台,这就比前者好用太多了. 安装注册 安装 PM> Install-Package Hangfire ...
- Extjs相关知识点梳理
store是一个为Ext器件提供record对象的存储容器,行为和属性都很象数据表 方法:不列举继承来的方法 Store( Object config ) 构造,config定义为{ autoLo ...
- cordova打包APK,SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...
javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...
- django 数据库 ORM创建表单是出错
WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default' HINT: MySQL ...