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. xgboost使用

    xgboost的实现方式为多颗CART树,其实xgboost就是类似于随机森林,但是与随机森林不同,他不是多个子树决策的结果,CART树最后会算出一个得分,是一个值,最后算出分类的时候,是多个值结合在 ...

  2. Markdown编辑器-图形化

    SELECT * from yffee_favourable_detail yfd LEFT JOIN yffee_favourable yf on yfd.minor_id = yf.major_i ...

  3. Golang 并发Groutine实例解读(一)

    Go语言的并发和并行 不知道你有没有注意到一个现象,还是这段代码,如果我跑在两个goroutines里面的话: var quit chan int = make(chan int) func loop ...

  4. JBoss 实战(2)

    转自:https://www.cnblogs.com/aiwz/p/6154591.html JBOSS HTTP的Thread Group概念 JBOSS是一个企业级的J2EE APP Contai ...

  5. JS中的编码,解码类型及说明

    使用ajax向后台提交的时候 由于参数中含有#  默认会被截断 只保留#之前的字符  json格式的字符串则不会被请求到后台的action 可以使用encodeURIComponent在前台进行编码, ...

  6. ObjectMapper将json转对象报错处理

    在使用ObjectMapper将json转对象,调用mapper.readValue(jsonStr, XwjUser.class)时,报如下错: com.fasterxml.jackson.data ...

  7. mybatis之@Select、@Insert、@Delete、@Param

    之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Map ...

  8. Ajax 学习(一)

    此篇为学习笔记 概述 Ajax(Asynchronous Javascrpt And Xml)是一种运用于浏览器的技术,它可以在浏览器与服务器之间使用异步通信机制进行数据通信,从而允许浏览器向服务器获 ...

  9. Struts2和MVC的简单整合

    1.首先还是创建一个简单Maven的项目,导入jar包, <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  10. 网络基础1_TCP和HTTP

    TCP/IP 是互联网相关的各类协议族的总称,并且进行分层,分为应用层,传输层,网络层,数据链路层这四层协议,分层的好处,是便于后期的优化与改进,扩展性好 应用层:主要为客户提供应用服务,       ...