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


目录
相关文章
|
6月前
|
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. 报错解决
662 0
|
Java 数据库连接 mybatis
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
206 0
|
3月前
Cannot get property 'versionCode' on extra properties extension as it does not exist
Cannot get property 'versionCode' on extra properties extension as it does not exist
130 0
|
5月前
|
存储 Java
Properties
Properties
70 1
Missing required prop: “modelValue“
Missing required prop: “modelValue“
118 0
|
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
|
机器学习/深度学习 关系型数据库 Oracle
xtt.properties
Reduce Transportable Tablespace Downtime using Incremental Backups (Doc ID 1389592.1) Properties file for xttdriver.
973 0
|
Java Spring