1、类拓展里面的方法不能被子类复写
1 class UserDataDao { 3 let realm = try! Realm() 5 func test1() { 7 } 9 } 10 extension UserDataDao { 13 func test2() { 15 } 16 } 17 class KCLDataDao:UserDataDao { 18 19 override func test1() { 21 } 22 // 报错:Declarations from extensions cannot be overridden yet 23 override func test2() { 25 } 26 }
2、filter map函数
filter很明显是过滤一些东西、map是映射、可以将输入的东西映射为不同类型的数据,如下
let array = [1,2,4,5,6] let arr2 = array.filter { (index) -> Bool in if index > 2{ return true //此处如果是正确的将保存 } return false //为false 被清除掉了 }.map { (index) -> String in //String 是可以自定义你所需要的类型 return "11\(index)" //转换为string类型的数据 }.filter { (str) -> Bool in if str == "114"{ return true } return false } let arr2 = array.filter{$0>1}.map{$0+2} print("arr2=\(arr2)")
3、swift3使用#selector指定的方法,只有当方法权限为private时需要加@objc修饰符,现在Swift4.0全都要加@objc修饰符
registerBtn.addTarget(self, action: #selector(self.buttonClick), for: .touchUpInside) @objc func buttonClick() { }
4、新增filePrivate
1 class UserDataDao { 2 3 fileprivate func test1() { //如果这里是private 4 print("UserDataDao test1") 5 } 6 } 7 extension UserDataDao { 8 func test2() { 9 print("UserDataDao test2") 10 } 11 } 12 class KCLDataDao { 13 14 func test() { 15 let user = UserDataDao() 16 user.test1() //这里报错 17 user.test2() 18 } 19 }
5、swift中 struct与class的区别
1:struct不能继承、自然也不存在对成员属性和成员方法, 类属性和类方法的重载
2:struct构造函数, 会自动生成带参数的构造器。class不会对有初始化赋值的属性, 生成带参数的构造器。
class UserDataDao { var name:String? let realm = try! Realm() fileprivate func test1() { print("UserDataDao test1") } } extension UserDataDao { func test2() { print("UserDataDao test2") } } struct UserDataDbo { var name:String? let realm = try! Realm() fileprivate func test1() { print("UserDataDao test1") } } extension UserDataDbo { func test2() { print("UserDataDao test2") } } class test:UserDataDao{ func test3() { let dao = UserDataDao() let boo = UserDataDbo() dao.test1() boo.test1() } } struct test2:UserDataDbo {//Inheritance from non-protocol type 'UserDataDbo' 报错 }