Stored Properties 与 Computed Properties

简介:

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


目录
相关文章
|
4月前
|
JavaScript
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
119 0
|
5月前
Missing required prop: “modelValue“
Missing required prop: “modelValue“
|
JavaScript 算法 前端开发
Property xxx was accessed during render but is not defined on instance
目前el-form的model主要用表单验证的,也就是配合el-form的rules和el-form-item的prop来使用的。不信的话,你可以增加一个rules和prop(为了调用验证方法,也el-form也加一个ref属性,相当于id或者class选择器的意思),但是不写model,然后验证的话,会提示缺少model,导致无法验证成功。
Property xxx was accessed during render but is not defined on instance
|
存储 自然语言处理 数据库
Settings 和 Mappings_Mappings_Ⅰ_介绍|学习笔记
快速学习 Settings 和 Mappings_Mappings_Ⅰ_介绍。
61 0
How to bind multiple properties with formatter on one control
How to bind multiple properties with formatter on one control
How to bind multiple properties with formatter on one control
|
JavaScript
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property b
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: vue项目示例,请参考甄佰 单向数据流所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。
2864 0
|
机器学习/深度学习 关系型数据库 Oracle
xtt.properties
Reduce Transportable Tablespace Downtime using Incremental Backups (Doc ID 1389592.1) Properties file for xttdriver.
942 0