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. ConfigurationManager

    ConfigurationManager读取和写入 提供对客户端应用程序配置文件的访问 通过引入System.Configuration.dll可以用ConfigurationManager类来读取项 ...

  2. elasticsearch(一):JAVA api操作

    1.创建一个mavan项目,项目的以来配置如下. <?xml version="1.0" encoding="UTF-8"?> <projec ...

  3. C语言——<算法>_冒泡算法的使用及理解

    对数组内数值进行有规则排序时,就要用冒泡算法,也是比较简单的一个算法 #include <stdio.h> #include <stdlib.h> int main() { i ...

  4. Linux内核源码目录

    linux和Android的Makefile和android.mk Uboot流程分析(未编辑完) Kernel的IIC驱动分析(未编辑完)

  5. 【转】外国朋友出的js题目,你能对几道

    原文地址 http://perfectionkills.com/javascript-quiz/ 中文地址加解释:by Aaron:http://www.cnblogs.com/aaronjs/p/3 ...

  6. [CQOI 2018]交错序列

    Description 题库链接 定义长度为 \(n\) 的"交错序列"为:长度为 \(n\) 序列中仅含 \(0,1\) 且没有相邻的 \(1\) .给出 \(a,b\) ,假设 ...

  7. C#在.NET编译执行过程

    1..NET语言的编译器接受源代码文件,并生成名为程序集的输出文件. 程序集要么是可执行的,要么是DLL 程序集里的代码并不是本机代码,而是一种名称为CIL的中间语言 程序集包含如下信息: 程序的CI ...

  8. 【转载&&干货】Noip应试技巧

    NOIP应试技巧 如何看待别人的经验? 我想大家都有台上的学长滔滔不绝,但是自己在台下漠不关心,或是老师考试前的叮嘱说完一会儿功夫就忘记了的经历吧.所以,有可能我接下来的所说的话,一到考场上就全部忘记 ...

  9. 前端(十):使用redux管理数据

    react本身能够完成动态数据的监听和更新,如果不是必要可以不适用redux. 安装redux: cnpm install redux --save,或者yarn add redux. 一.react ...

  10. Linux学习4-远程登录管理工具安装

    1.配置虚拟机网络环境 桥接模式:使用真实网卡进行通信,配置简单,可以和通往内的其他真实机直接进行通讯,缺点是它会占用网段的一个IP地址. NAT模式:使用虚拟机模拟的虚拟网卡进行通讯,会使用VMne ...