Swift小公举

来源:互联网 发布:hadoop ubuntu 编辑:程序博客网 时间:2024/04/19 20:48
//标准输出func printLog<T>(message: T,                    file: String = #file,                  method: String = #function,                    line: Int = #line){    print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")}
    //属性观察器    var intValue: Int{                willSet{            //属性值将要被重新赋值        }                didSet{            //属性值重新赋值完成        }    }
 
//下标。可以给结构体等添加类似于数组,字典的下标。下标支持重载。
//下标的定义
struct TimesOfNum {
    let num:Int
    let otherNum:Int
    
    subscript(index:Int) -> Int{
        return num * index
    }
    
    subscript(index:Int,index2:Int) -> Int{
        return num * index + index2
    }
}
//调用
        let numValue = TimesOfNum(num: 5, otherNum: 3)
        
        print("5 的三倍是\(numValue[3])")
        print("5 的三倍再加上3是\(numValue[3,3])")
0 0
原创粉丝点击