zoukankan      html  css  js  c++  java
  • 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)
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    A1052. Linked List Sorting (25)
    A1032. Sharing (25)
    A1022. Digital Library (30)
    A1071. Speech Patterns (25)
    A1054. The Dominant Color (20)
    A1060. Are They Equal (25)
    A1063. Set Similarity (25)
    电子码表
    矩阵键盘
    对象追踪、临时对象追踪、绝对坐标与相对坐标
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5569331.html
Copyright © 2011-2022 走看看