swift扩展Extension_011-swift延展基本使用

//: Playground - noun: a place where people can play

import UIKit

//----扩展Extension---------//
//作用:给已有的类、结构体、枚举扩展新的功能(方法和属性)
//Swift中的扩展没有名字 

//1.扩展属性:只能添加计算属性,不能添加存储属性
extension UIView {
    
    //var isSuperView = false
    
    
    var height : CGFloat {
        
          get {
            
            return self.frame.size.height

         }
        set {
            
            self.frame.size.height = newValue
            
        }
        
        
    }
    

}

var myView = UIView.init(frame: CGRectMake(100, 100, 100, 100))
myView.height = 200
print(myView.height)

//2.扩展方法

extension String {
    
    //字符串子串的截取
    func subStringFromStartIndexAndEndIndex(start : Int, end : Int) -> String {
        
        var count = 0
        var result = ""
        
        for char in self.characters {
            
            if count >= start {
                result += "(char)"
            }
            
            if count >= end {
                break
            }
            
            count++
            
        }
        
        return result
        
    }
    
    //Bool转化为String
    static func stringWithBool(value : Bool) -> String {
        
        return "(value)"
    }
    
    
    
}

var str = "Hello 88"
str.subStringFromStartIndexAndEndIndex(0, end: 4)
String.stringWithBool(true)
时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5569331.html