zoukankan      html  css  js  c++  java
  • 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)
        }
    }
  • 相关阅读:
    【C语言】学习笔记5——指针(1)
    【C语言】学习笔记4——数组
    【leetcode】Contest98
    【Python】从0开始写爬虫——豆瓣电影
    C# WPF 文件复制,相对路径
    WPF DataGrid多表头/列头,多行头,合并单元格,一列占据多行
    WPF Image Source 设置相对路径图片
    WPF Blend 一个动画结束后另一个动画开始执行(一个一个执行)
    WPF 操作XML 读写
    WPF 选择电脑文件显示路径,弹出资源管理器,打开文件
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4849201.html
Copyright © 2011-2022 走看看