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 var
keyword) 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 ...
随机推荐
- golang基础--控制语句
go基础之控制语句 补充知识 指针 与其他语言不同,在Go中不支持指针运算即->运算符,而直接采用.选择符来操作指针目标对象的成员. 操作符&取变量的地址,使用*通过指针间间接访问目标对 ...
- kubernetes continually evict pod when node's inode exhausted
kubernetes等容器技术可以将所有的业务进程运行在公共的资源池中,提高资源利用率,节约成本,但是为避免不同进程之间相互干扰,对底层docker, kubernetes的隔离性就有了更高的要求,k ...
- JavaScript try...catch用法
一.用法 try...catch语句用了处理代码中可能出现的错误信息,将要进行测试的代码块放在try中,如果代码出现异常,则会被catch捕获 其格式如下: <script> try{ / ...
- java linux ssh jar
Ganymed SSH-2 for Java http://www.ganymed.ethz.ch/ssh2/ Ganymed SSH-2 for Java is a library which im ...
- (在数据库中调用webservices。)SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
--开启 Ole Automation Procedures sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_config ...
- Java中泛型通配符的一点概念
以List<T>为例,通常如果我们想在List中存储某种类型的数据,我们会用下面的语法进行声明和使用: List<String> allMsg = new ArrayList& ...
- WPF绑定之索引器值变化通知
背景 在某些应用中,需要在界面上绑定到索引器,并在值发生变化时实时更新. 解决方案 只要将包含索引器的类实现INotifyPropertyChanged接口,并在索引值更改时引发PropertyCha ...
- MySQL4:索引
什么是索引 索引是对数据库表中一列或者多列的值进行排序的一种结构,所引用于快速找出在某个 列中有一特定值的行.不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行.表越大,查询数据所 ...
- javax.naming.NamingException: Cannot create resource instance报错修改
//下面内容为网上获取 avax.naming.NamingException: Cannot create resource instance at org.apache.naming.factor ...
- java性能监控工具jstat
jstat Monitors Java Virtual Machine (JVM) statistics. This command is experimental and unsupported. ...