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 40
            }
        }

        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: 20, data: nil)
        print(model2.rowHeight)
        print(model2.reusableCellIdentifier)
        print(model2.data)
    }
}
时间: 2024-11-23 02:36:51

Stored Properties 与 Computed Properties的相关文章

2.3 The Object Model -- Computed Properties

一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个function. 之后你可以用同样的方法使用它,任何正常静态属性. 2. 对于获取一个或多个正常的属性和转换或者操纵它们的数据去创建一个新的值,它是超级便利. 二.Computed Properties In Action example: Person = Ember.Object.extend(

Properties类读写.properties配置文件

package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Uns

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

配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/resources/conf/boot.properties com.ieen.boot.name="123" com.ieen.boot.value="456" 调用方法@Value 注解调用application.properties属性值: @Value("

读取properties文件以及properties的用法

package cn.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { //读取jdbc.properties配置文件,单例模式 private static ConfigManager configManager; private static Properties properties; //单例模式必须

九、属性 Properties

1. Stored Properties 1.1 概述 classes, structures, and enumerations都可以定义 Stored Properties Stored Properties 这是最简单的属性类型.例如: struct FixedLengthRange { var firstValue: Int let length: Int } var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length:

Swift学习 (一)

以后会自己总结学习Swift的笔记与深化.希望能够帮助已经有Objective-C经验的开发者更快地学习Swift.我们一起学习,同时也品味到Swift的精妙之处. 结论放在开头:我认为Swift比Objective-C更优雅,更安全同时也更现代,更性感. 首先学习从Objective-C到Swift的语法差异.我们熟悉的Objective-C特性在Swift中如何展现: 1.属性(property)和实例变量(instance variable) Objective-C property in

Swift学习: 从Objective-C到Swift

文章组织脉络: 从Objective-C到Swift的语法差异.我们熟悉的Objective-C特性在Swift中如何展现. 从Objective-C到Swift的进步改进.研究对比Swift在安全性,易用性上的提升,给我们带来的新编程范式. 目录: 1.属性(property)和实例变量(instance variable) 2.控制流 3.函数 4.类与初始化(Initializers) 5.枚举与结构体 6.协议(Protocols) 7.Swift与Cocoa 8.总结 1.属性(pro

springboot application.properties 常用完整版配置信息

从springboot官方文档中扒出来的,留存一下以后应该会用到 # =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application. ^^^ # ==

SpringBoot标准Properties

# =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application. ^^^ # ===================================