├── swift 4 demo
├── 34.关联
│ ├── 34.关联
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── ViewController.swift
│ │ ├── Image.swift
│ │ ├── Info.plist
│ │ ├── Base.lproj
│ │ │ ├── LaunchScreen.storyboard
│ │ │ └── Main.storyboard
│ │ ├── Assets.xcassets
│ │ │ └── AppIcon.appiconset
│ │ │ │ └── Contents.json
│ │ └── AppDelegate.swift
│ ├── 34.关联.xcodeproj
│ │ ├── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ │ ├── xcdebugger
│ │ │ │ └── Breakpoints_v2.xcbkptlist
│ │ │ │ └── xcschemes
│ │ │ │ └── xcschememanagement.plist
│ │ └── project.xcworkspace
│ │ │ ├── contents.xcworkspacedata
│ │ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ ├── 34.关联Tests
│ │ ├── Info.plist
│ │ └── _4___Tests.swift
│ └── 34.关联UITests
│ │ ├── Info.plist
│ │ └── _4___UITests.swift
├── 12.协议protocol.playground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ ├── contents.xcplayground
│ └── Contents.swift
├── 33.访问权限
│ ├── MyWorkspace.xcworkspace
│ │ ├── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ │ ├── xcdebugger
│ │ │ │ └── Breakpoints_v2.xcbkptlist
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── contents.xcworkspacedata
│ ├── MyApp1
│ │ ├── MyApp1.xcodeproj
│ │ │ ├── project.xcworkspace
│ │ │ │ ├── contents.xcworkspacedata
│ │ │ │ └── xcuserdata
│ │ │ │ │ └── weifan.xcuserdatad
│ │ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── xcuserdata
│ │ │ │ └── weifan.xcuserdatad
│ │ │ │ └── xcschemes
│ │ │ │ └── xcschememanagement.plist
│ │ ├── MySDK.xcodeproj
│ │ │ └── xcuserdata
│ │ │ │ └── weifan.xcuserdatad
│ │ │ │ └── xcschemes
│ │ │ │ └── xcschememanagement.plist
│ │ ├── MyApp1Tests
│ │ │ ├── Info.plist
│ │ │ └── MyApp1Tests.swift
│ │ ├── MyApp1UITests
│ │ │ ├── Info.plist
│ │ │ └── MyApp1UITests.swift
│ │ └── MyApp1
│ │ │ ├── Info.plist
│ │ │ ├── Base.lproj
│ │ │ ├── LaunchScreen.storyboard
│ │ │ └── Main.storyboard
│ │ │ ├── Assets.xcassets
│ │ │ └── AppIcon.appiconset
│ │ │ │ └── Contents.json
│ │ │ ├── ViewController.swift
│ │ │ └── AppDelegate.swift
│ ├── MySDK
│ │ ├── MySDK
│ │ │ ├── WelComeTest.swift
│ │ │ ├── InternalTest.swift
│ │ │ ├── OpenTest.swift
│ │ │ ├── MySDK.h
│ │ │ ├── Info.plist
│ │ │ └── PublicTest.swift
│ │ ├── MySDK.xcodeproj
│ │ │ └── xcuserdata
│ │ │ │ └── weifan.xcuserdatad
│ │ │ │ └── xcschemes
│ │ │ │ └── xcschememanagement.plist
│ │ └── MySDKTests
│ │ │ ├── Info.plist
│ │ │ └── MySDKTests.swift
│ └── MyApp2
│ │ ├── MyApp2.xcodeproj
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── xcschemes
│ │ │ └── xcschememanagement.plist
│ │ ├── MyApp2
│ │ ├── ViewController.swift
│ │ ├── Info.plist
│ │ ├── Base.lproj
│ │ │ ├── Main.storyboard
│ │ │ └── LaunchScreen.storyboard
│ │ ├── Assets.xcassets
│ │ │ └── AppIcon.appiconset
│ │ │ │ └── Contents.json
│ │ └── AppDelegate.swift
│ │ ├── MyApp2Tests
│ │ ├── Info.plist
│ │ └── MyApp2Tests.swift
│ │ └── MyApp2UITests
│ │ ├── Info.plist
│ │ └── MyApp2UITests.swift
├── 14.枚举.playground
│ ├── contents.xcplayground
│ └── Contents.swift
├── 15.扩展.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 16.下标.playground
│ ├── contents.xcplayground
│ └── Contents.swift
├── 17.泛型.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 19.断言.playground
│ ├── contents.xcplayground
│ └── Contents.swift
├── 1初识.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── 23.注释.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 28.类簇.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 2数据类型.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 32.镜像.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 4字典.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 5可选值.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 6集合.playground
│ ├── contents.xcplayground
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 7流控制.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 8对象和类.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 9.属性.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 10.函数和闭包.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── timeline.xctimeline
├── 11.初始化和释放.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 18.异常错误.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 21.检测API.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 22. 对象的标识.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 24. 元类型.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 25.控件命名.playground
│ ├── contents.xcplayground
│ └── Contents.swift
├── 26.对象判等.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ └── Contents.swift
├── 30.文本输出.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 3Array.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcuserdata
│ │ │ ├── weifan.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── yt_lwf.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ ├── timeline.xctimeline
│ └── Contents.swift
├── 13.类和结构体的区别.playground
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ └── weifan.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 27.探究Self的用途.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 29.动态调用对象(实例).playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── 31.黑魔法swizzle.playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── weifan.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
└── 20.自动引用计数(strong,weak,unowned).playground
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ │ └── weifan.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── Contents.swift
├── TheSwiftProgrammingLanguage(Swift4).epub
└── swift 4 中文
├── 06、函数 – Swift 编程语言.webarchive
├── 07、闭包 – Swift 编程语言.webarchive
├── 08、枚举 – Swift 编程语言.webarchive
├── 10、属性 – Swift 编程语言.webarchive
├── 11、方法 – Swift 编程语言.webarchive
├── 12、下标 – Swift 编程语言.webarchive
├── 13、继承 – Swift 编程语言.webarchive
├── 21、扩展 – Swift 编程语言.webarchive
├── 22、协议 – Swift 编程语言.webarchive
├── 23、泛型 – Swift 编程语言.webarchive
├── 29、类型 – Swift 编程语言.webarchive
├── 31、特性 – Swift 编程语言.webarchive
├── 01、基础内容 – Swift 编程语言.webarchive
├── 04、集合类型 – Swift 编程语言.webarchive
├── 05、控制流 – Swift 编程语言.webarchive
├── 14、初始化 – Swift 编程语言.webarchive
├── 15、反初始化 – Swift 编程语言.webarchive
├── 17、可选链 – Swift 编程语言.webarchive
├── 18、错误处理 – Swift 编程语言.webarchive
├── 19、类型转换 – Swift 编程语言.webarchive
├── 20、内嵌类型 – Swift 编程语言.webarchive
├── 25、访问控制 – Swift 编程语言.webarchive
├── 28、词汇结构 – Swift 编程语言.webarchive
├── 30、表达式 – Swift 编程语言.webarchive
├── 02、基本运算符 – Swift 编程语言.webarchive
├── 03、字符串和字符 – Swift 编程语言.webarchive
├── 09、类和结构体 – Swift 编程语言.webarchive
├── 16、自动引用计数 – Swift 编程语言.webarchive
├── 24、内存安全性 – Swift 编程语言.webarchive
├── 26、高级运算符 – Swift 编程语言.webarchive
└── 27、关于语言参考 – Swift 编程语言.webarchive
/swift 4 demo/34.关联/34.关联/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/34.关联/34.关联/4.png
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/34.关联/34.关联/5.png
--------------------------------------------------------------------------------
/TheSwiftProgrammingLanguage(Swift4).epub:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/TheSwiftProgrammingLanguage(Swift4).epub
--------------------------------------------------------------------------------
/swift 4 中文/06、函数 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/06、函数 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/07、闭包 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/07、闭包 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/08、枚举 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/08、枚举 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/10、属性 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/10、属性 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/11、方法 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/11、方法 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/12、下标 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/12、下标 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/13、继承 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/13、继承 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/21、扩展 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/21、扩展 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/22、协议 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/22、协议 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/23、泛型 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/23、泛型 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/29、类型 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/29、类型 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/31、特性 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/31、特性 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/01、基础内容 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/01、基础内容 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/04、集合类型 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/04、集合类型 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/05、控制流 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/05、控制流 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/14、初始化 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/14、初始化 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/15、反初始化 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/15、反初始化 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/17、可选链 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/17、可选链 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/18、错误处理 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/18、错误处理 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/19、类型转换 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/19、类型转换 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/20、内嵌类型 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/20、内嵌类型 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/25、访问控制 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/25、访问控制 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/28、词汇结构 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/28、词汇结构 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/30、表达式 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/30、表达式 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/02、基本运算符 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/02、基本运算符 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/03、字符串和字符 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/03、字符串和字符 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/09、类和结构体 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/09、类和结构体 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/16、自动引用计数 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/16、自动引用计数 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/24、内存安全性 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/24、内存安全性 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/26、高级运算符 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/26、高级运算符 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 中文/27、关于语言参考 – Swift 编程语言.webarchive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 中文/27、关于语言参考 – Swift 编程语言.webarchive
--------------------------------------------------------------------------------
/swift 4 demo/12.协议protocol.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联.xcodeproj/xcuserdata/weifan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyWorkspace.xcworkspace/xcuserdata/weifan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/swift 4 demo/14.枚举.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/15.扩展.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/16.下标.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/17.泛型.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/19.断言.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/1初识.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/23.注释.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/28.类簇.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/2数据类型.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/32.镜像.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/4字典.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/5可选值.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/6集合.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/7流控制.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/8对象和类.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/11.初始化和释放.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/18.异常错误.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/21.检测API.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/22. 对象的标识.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/25.控件命名.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/30.文本输出.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/12.协议protocol.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/13.类和结构体的区别.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/1初识.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/27.探究Self的用途.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/29.动态调用对象(实例).playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/31.黑魔法swizzle.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/4字典.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/15.扩展.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/17.泛型.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/18.异常错误.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/21.检测API.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/23.注释.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/28.类簇.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/2数据类型.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/30.文本输出.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/32.镜像.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/5可选值.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/7流控制.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/8对象和类.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/11.初始化和释放.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/13.类和结构体的区别.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/22. 对象的标识.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/27.探究Self的用途.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/29.动态调用对象(实例).playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/31.黑魔法swizzle.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/20.自动引用计数(strong,weak,unowned).playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyWorkspace.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/33.访问权限/MyWorkspace.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/20.自动引用计数(strong,weak,unowned).playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/9.属性.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/18.异常错误.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/18.异常错误.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/1初识.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/1初识.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/4字典.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/4字典.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/5可选值.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/5可选值.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/7流控制.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/7流控制.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/9.属性.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/xcuserdata/lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/15.扩展.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/15.扩展.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/17.泛型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/17.泛型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/18.异常错误.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/18.异常错误.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/23.注释.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/23.注释.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/24. 元类型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/24. 元类型.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/26.对象判等.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/26.对象判等.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/28.类簇.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/28.类簇.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/2数据类型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/2数据类型.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/30.文本输出.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/30.文本输出.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/32.镜像.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/32.镜像.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联.xcodeproj/project.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/34.关联/34.关联.xcodeproj/project.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/3Array.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/3Array.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/8对象和类.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/8对象和类.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/11.初始化和释放.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/11.初始化和释放.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/21.检测API.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/21.检测API.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/22. 对象的标识.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/22. 对象的标识.playground/playground.xcworkspace/xcuserdata/yt_lwf.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/13.类和结构体的区别.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/13.类和结构体的区别.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/27.探究Self的用途.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/27.探究Self的用途.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/12.协议protocol.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/12.协议protocol.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/29.动态调用对象(实例).playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/29.动态调用对象(实例).playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/31.黑魔法swizzle.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/31.黑魔法swizzle.playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1.xcodeproj/project.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/33.访问权限/MyApp1/MyApp1.xcodeproj/project.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/20.自动引用计数(strong,weak,unowned).playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qq20539350/swift4/HEAD/swift 4 demo/20.自动引用计数(strong,weak,unowned).playground/playground.xcworkspace/xcuserdata/weifan.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/18.异常错误.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/21.检测API.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/23.注释.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/22. 对象的标识.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/WelComeTest.swift:
--------------------------------------------------------------------------------
1 | //
2 | // WelComeTest.swift
3 | // MySDK
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | open class WelComeTest: NSObject {
12 |
13 | open func sayHello() -> String {
14 | print("你好");
15 | return "你好";
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/swift 4 demo/19.断言.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //基本用法
8 |
9 | var name = "小星星";
10 | assert(name == "小星星", "不是小星星");// 条件为真,不执行断言
11 |
12 |
13 | let age = -3;
14 | assert(age >= 0, "年龄不能小于0");// 条件为真,不执行断言
15 |
16 | //结果
17 | //Assertion failed: 年龄不能小于0: file 19.断言.playground, line 10
18 |
19 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyWorkspace.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
9 |
10 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联.xcodeproj/xcuserdata/weifan.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 34.关联.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MySDK.xcodeproj/xcuserdata/weifan.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | MySDK.xcscheme
8 |
9 | orderHint
10 | 1
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK.xcodeproj/xcuserdata/weifan.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | MySDK.xcscheme
8 |
9 | orderHint
10 | 1
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1.xcodeproj/xcuserdata/weifan.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | MyApp1.xcscheme
8 |
9 | orderHint
10 | 2
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2.xcodeproj/xcuserdata/weifan.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | MyAPP2.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/swift 4 demo/21.检测API.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 | //基本写法
7 |
8 | //写法1
9 |
10 | if #available(iOS 10 ,*){
11 | print("支持10")
12 | }else{
13 | print("不支持10,支持10之前的版本")
14 | }
15 |
16 | //写法2
17 |
18 | if #available(iOS 10, macOS 10.12, *) {
19 | // Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
20 | } else {
21 | // Fall back to earlier iOS and macOS APIs
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/InternalTest.swift:
--------------------------------------------------------------------------------
1 | //
2 | // InternalTest.swift
3 | // MySDK
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class InternalTest{
12 | var string : String?;
13 | init(string: String) {
14 | self.string = string;
15 | }
16 |
17 | func returnStr() -> String? {
18 | return "\(string ?? "hello")-3.internal:默认访问级别,在整个模块内可以被访问,继承和重写.";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/OpenTest.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | //
4 | // OpenTest.swift
5 | // MySDK
6 | //
7 | // Created by weifan on 2018/1/26.
8 | // Copyright © 2018年 xiaoguo. All rights reserved.
9 | //
10 |
11 | import UIKit
12 |
13 | open class OpenTest{
14 | open var string : String?;
15 | public init(string: String) {
16 | self.string = string;
17 | }
18 |
19 | open func returnStr() -> String? {
20 | return "\(string ?? "hello")-open:公开权限, 最高的权限, 可以被其他模块访问, 继承及重写.";
21 | }
22 |
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/MySDK.h:
--------------------------------------------------------------------------------
1 | //
2 | // MySDK.h
3 | // MySDK
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | //! Project version number for MySDK.
12 | FOUNDATION_EXPORT double MySDKVersionNumber;
13 |
14 | //! Project version string for MySDK.
15 | FOUNDATION_EXPORT const unsigned char MySDKVersionString[];
16 |
17 | // In this header, you should import all the public headers of your framework using statements like #import
18 |
19 |
20 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // MyAPP2
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController {
12 |
13 | override func viewDidLoad() {
14 | super.viewDidLoad()
15 | // Do any additional setup after loading the view, typically from a nib.
16 | }
17 |
18 | override func didReceiveMemoryWarning() {
19 | super.didReceiveMemoryWarning()
20 | // Dispose of any resources that can be recreated.
21 | }
22 |
23 |
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/swift 4 demo/4字典.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/swift 4 demo/6集合.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/swift 4 demo/22. 对象的标识.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //怎么获取类的标识
8 | class Student {
9 |
10 | }
11 |
12 | let x = Student();
13 | let y = x;
14 | print(ObjectIdentifier(x));
15 | print(ObjectIdentifier(x));
16 | print(ObjectIdentifier(x) == ObjectIdentifier(x));//标识
17 | print(x);
18 |
19 | print(x === y);
20 | let z = Student();
21 | print(ObjectIdentifier(z));
22 | print(ObjectIdentifier(x) == ObjectIdentifier(z));
23 | print(z);
24 | print(x === z);
25 |
26 | if x is Student && y is Student {
27 | print("是同一类");
28 | }
29 |
30 | //创建对象的时候,会给对象分配一个独一无二的标识
31 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDKTests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/swift 4 demo/5可选值.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //1 ?的用法
6 | class Square{
7 | var sideLength : Float;
8 | var name : String;
9 | init(sideLength: Float, name: String) {
10 | self.sideLength = sideLength;
11 | self.name = name;
12 | }
13 | }
14 |
15 |
16 | let optionalSquare : Square? = Square(sideLength: 11.0, name: "你好");
17 | let length = optionalSquare?.sideLength;
18 |
19 | //什么时候定义可选值
20 | //当你创建的变量有可能会出现nil的时候
21 | //看第二句代码为什么那样写,如果不那样写会怎么?
22 | //如果我们第一句代码创建的结果为nil,在获取它里面的属性sideLength的时候
23 | //我们加了一个?系统会首先确认第一个是不是Nil,如果是nil,则直接返回一个nil给length,如果不是空值,则获取sideLength的值赋值给length
24 |
25 | //加入我们的解封第一个对象
26 | let lengthText = optionalSquare!.sideLength;
27 | //如果optionSquare不是nil还好,没有任何影响,如果为nil系统直接崩溃,相当于这样let sideLength = nil , swift不允许我们使用nil赋值给一个非可选值.
28 |
--------------------------------------------------------------------------------
/swift 4 demo/29.动态调用对象(实例).playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //实现步骤
8 | //1.定义一个父类
9 | //2.在父类中定义动态调用的方法
10 | //3.引用父类的动态方法
11 | //4.子类重写父类的动态方法
12 | //5.在3引用的动态方法中传入需要执行方法的子类
13 | //6.执行方法
14 |
15 | //示例代码
16 | //定义子类和父类
17 |
18 | //定义一个父类
19 | class Person {
20 | func describe(){
21 | print("我是父类人")
22 | }
23 | }
24 |
25 | //定义一个子类
26 | class Man : Person {
27 | override func describe() {
28 | print("我是子类男人");
29 | }
30 | }
31 |
32 | //动态调用
33 | // 引用父类的方法
34 | //let describe = Person.describe;
35 | // 传入实际执行的实例对象
36 | let man = Man();
37 | //let describeS = describe(man);
38 | //执行方法
39 | //describeS();
40 |
41 | //一句话完成
42 |
43 | Person.describe(man)()
44 | //
45 | //运行结果:
46 | //
47 | //我是子类男人
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | FMWK
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | $(CURRENT_PROJECT_VERSION)
21 | NSPrincipalClass
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/swift 4 demo/32.镜像.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //输出对象的内容到控制台
8 | struct Person {
9 | var name : String;
10 | var age : Int;
11 | var weight : Double;
12 | }
13 |
14 | let person : Any = Person(name : "13",age: 12,weight: 99);
15 | dump(person);
16 | //结果
17 | //__lldb_expr_195.Person
18 | // - name: "13"
19 | //- age: 12
20 | //- weight: 99.0
21 |
22 | //镜像的使用
23 | //做一个简单的对象转字典应用
24 | func anyobjectConvertDic(any:Any)->[String:Any]?{
25 | var dic:[String:Any] = [:]
26 | for childer in Mirror(reflecting: any).children {//镜像
27 | dic[childer.label!] = childer.value
28 | }
29 | return dic
30 | }
31 |
32 | //运行下面代码:
33 | print(anyobjectConvertDic(any: person));
34 | //结果
35 | //Optional(["name": "13", "age": 12, "weight": 99.0])
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/swift 4 demo/28.类簇.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //类簇
8 | //使用一个统一的接口单一的对象
9 | class Drinking : NSObject{
10 | class func drink(name : String) -> Drinking? {
11 | var drink : Drinking?
12 |
13 | switch name {
14 | case "Coke":
15 | drink = Coke(name : name);
16 | case "Beer":
17 | drink = Beer(name : name);
18 | default:
19 | break;
20 | }
21 | return drink;
22 | }
23 | }
24 |
25 | class Coke : Drinking {
26 | var name : String;
27 | init(name : String) {
28 | self.name = name;
29 | }
30 | }
31 |
32 | class Beer : Drinking {
33 | var name:String;
34 | init(name: String) {
35 | self.name = name;
36 | }
37 | }
38 |
39 | let coke = Drinking.drink(name: "Coke") as! Coke;
40 | print(coke.name);
41 |
42 | //运行结果
43 | //Coke
44 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // 34.关联
4 | //
5 | // Created by weifan on 2018/2/6.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController {
12 |
13 |
14 | override func viewDidLoad() {
15 | super.viewDidLoad()
16 |
17 |
18 | }
19 |
20 | @IBAction func AssociationImplementAction(_ sender: Any) {
21 |
22 | animatedImageData()
23 |
24 | }
25 |
26 | func animatedImageData() -> Void {
27 | let image = UIImage(named: "4.png")
28 | let data = UIImagePNGRepresentation(image!);
29 | image?.animatedImageData = data;
30 | print(image?.animatedImageData);
31 | }
32 |
33 | override func didReceiveMemoryWarning() {
34 | super.didReceiveMemoryWarning()
35 | // Dispose of any resources that can be recreated.
36 | }
37 |
38 |
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/swift 4 demo/26.对象判等.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //实现步骤
8 | //1.d对象遵守协议Equatable
9 | //2.实现协议的方法
10 |
11 | //a.第一种写法 在类内部实现
12 |
13 | class A : Equatable {
14 | var name : String?
15 | static func ==(lhs: A,rhs: A) -> Bool{
16 | return lhs.name == rhs.name;
17 | }
18 | }
19 |
20 | //b.第二种写法将实现方法写到全局去
21 | class B : Equatable {
22 | var name : String?
23 | }
24 |
25 | func ==(lhs: B, rhs : B) -> Bool {
26 | return lhs.name == rhs.name;
27 | }
28 |
29 | //c.你也可以使用扩展方式去实现协议
30 | extension A {
31 | static func equal(lhs: A, rhs: A) -> Bool{
32 | return lhs.name == rhs.name
33 | }
34 | }
35 |
36 | let classa = A();
37 | classa.name = "小星星";
38 | let classb = A();
39 | classb.name = "小星星";
40 | if classa == classb { //其实判断的是classa.name 是否相等
41 | print("==");
42 | }
43 | //提示:
44 | //如果对象没有实现Equatable 协议方法,系统不允许使用 == 进行判断,这是swift的严谨性
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联Tests/_4___Tests.swift:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | import XCTest
4 | @testable import ___VARIABLE_productName:identifier___
5 |
6 | class ___FILEBASENAMEASIDENTIFIER___: XCTestCase {
7 |
8 | override func setUp() {
9 | super.setUp()
10 | // Put setup code here. This method is called before the invocation of each test method in the class.
11 | }
12 |
13 | override func tearDown() {
14 | // Put teardown code here. This method is called after the invocation of each test method in the class.
15 | super.tearDown()
16 | }
17 |
18 | func testExample() {
19 | // This is an example of a functional test case.
20 | // Use XCTAssert and related functions to verify your tests produce the correct results.
21 | }
22 |
23 | func testPerformanceExample() {
24 | // This is an example of a performance test case.
25 | self.measure {
26 | // Put the code you want to measure the time of here.
27 | }
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDKTests/MySDKTests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MySDKTests.swift
3 | // MySDKTests
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import XCTest
10 | @testable import MySDK
11 |
12 | class MySDKTests: XCTestCase {
13 |
14 | override func setUp() {
15 | super.setUp()
16 | // Put setup code here. This method is called before the invocation of each test method in the class.
17 | }
18 |
19 | override func tearDown() {
20 | // Put teardown code here. This method is called after the invocation of each test method in the class.
21 | super.tearDown()
22 | }
23 |
24 | func testExample() {
25 | // This is an example of a functional test case.
26 | // Use XCTAssert and related functions to verify your tests produce the correct results.
27 | }
28 |
29 | func testPerformanceExample() {
30 | // This is an example of a performance test case.
31 | self.measure {
32 | // Put the code you want to measure the time of here.
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1Tests/MyApp1Tests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MyApp1Tests.swift
3 | // MyApp1Tests
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import XCTest
10 | @testable import MyApp1
11 |
12 | class MyApp1Tests: XCTestCase {
13 |
14 | override func setUp() {
15 | super.setUp()
16 | // Put setup code here. This method is called before the invocation of each test method in the class.
17 | }
18 |
19 | override func tearDown() {
20 | // Put teardown code here. This method is called after the invocation of each test method in the class.
21 | super.tearDown()
22 | }
23 |
24 | func testExample() {
25 | // This is an example of a functional test case.
26 | // Use XCTAssert and related functions to verify your tests produce the correct results.
27 | }
28 |
29 | func testPerformanceExample() {
30 | // This is an example of a performance test case.
31 | self.measure {
32 | // Put the code you want to measure the time of here.
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2Tests/MyApp2Tests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MyAPP2Tests.swift
3 | // MyAPP2Tests
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import XCTest
10 | @testable import MyAPP2
11 |
12 | class MyAPP2Tests: XCTestCase {
13 |
14 | override func setUp() {
15 | super.setUp()
16 | // Put setup code here. This method is called before the invocation of each test method in the class.
17 | }
18 |
19 | override func tearDown() {
20 | // Put teardown code here. This method is called after the invocation of each test method in the class.
21 | super.tearDown()
22 | }
23 |
24 | func testExample() {
25 | // This is an example of a functional test case.
26 | // Use XCTAssert and related functions to verify your tests produce the correct results.
27 | }
28 |
29 | func testPerformanceExample() {
30 | // This is an example of a performance test case.
31 | self.measure {
32 | // Put the code you want to measure the time of here.
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/swift 4 demo/10.函数和闭包.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联UITests/_4___UITests.swift:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | import XCTest
4 |
5 | class ___FILEBASENAMEASIDENTIFIER___: XCTestCase {
6 |
7 | override func setUp() {
8 | super.setUp()
9 |
10 | // Put setup code here. This method is called before the invocation of each test method in the class.
11 |
12 | // In UI tests it is usually best to stop immediately when a failure occurs.
13 | continueAfterFailure = false
14 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
15 | XCUIApplication().launch()
16 |
17 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
18 | }
19 |
20 | override func tearDown() {
21 | // Put teardown code here. This method is called after the invocation of each test method in the class.
22 | super.tearDown()
23 | }
24 |
25 | func testExample() {
26 | // Use recording to get started writing UI tests.
27 | // Use XCTAssert and related functions to verify your tests produce the correct results.
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/swift 4 demo/15.扩展.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | //: Playground - noun: a place where people can play
3 |
4 | import UIKit
5 |
6 | var str = "Hello, playground"
7 |
8 | //给存在的类型增加扩展
9 | //a.我们演示一个简单的例子
10 | //线定义一个协议
11 | protocol Custom {
12 | var describe : String {
13 | get
14 | }
15 | }
16 |
17 | //通过扩展的方式,让Int实现协议
18 | extension Int : Custom {
19 | var describe : String {
20 | return String(self);
21 | }
22 | }
23 |
24 | //高级用法
25 | //a.需求:给Array扩展一个属性,但是只能让元素为Int 时才能使用
26 | //思路:承接上文,我们给Int遵守了一个协议Custom,我们就借助这个来实现需求
27 |
28 | extension Array where Element : Custom {
29 | var lastValue : Element {
30 | return self[count - 1];
31 | }
32 | }
33 | // 使用
34 | let nums = [1,2,3,4,5];
35 | print(nums.lastValue);//使用
36 |
37 | //b.如果想要在定义协议的时候,不指定变量名称,在实现协议的时候,再去设定变量类型,应该怎么写?
38 | //定义一个协议
39 | protocol Draw {
40 | associatedtype CompatableType //自定义一个变量名字
41 | var name : CompatableType{
42 | get
43 | }
44 | }
45 |
46 | //如何继承协议
47 | class Person1 : Draw {
48 | typealias CompatableType = String;//定义类型别名
49 | var name : String {
50 | return "人";
51 | }
52 | }
53 |
54 | let person1 = Person1();
55 | person1.name;
56 |
57 | //当然由于Swift 有类型推断能力,可以像线面这样写
58 | class Person2 : Draw {
59 | var name : String {
60 | return "人";
61 | }
62 | }
63 |
64 | //你不能干的事情
65 | //不能给对象扩展存储属性
66 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1UITests/MyApp1UITests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MyApp1UITests.swift
3 | // MyApp1UITests
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import XCTest
10 |
11 | class MyApp1UITests: XCTestCase {
12 |
13 | override func setUp() {
14 | super.setUp()
15 |
16 | // Put setup code here. This method is called before the invocation of each test method in the class.
17 |
18 | // In UI tests it is usually best to stop immediately when a failure occurs.
19 | continueAfterFailure = false
20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
21 | XCUIApplication().launch()
22 |
23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
24 | }
25 |
26 | override func tearDown() {
27 | // Put teardown code here. This method is called after the invocation of each test method in the class.
28 | super.tearDown()
29 | }
30 |
31 | func testExample() {
32 | // Use recording to get started writing UI tests.
33 | // Use XCTAssert and related functions to verify your tests produce the correct results.
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2UITests/MyApp2UITests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MyAPP2UITests.swift
3 | // MyAPP2UITests
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import XCTest
10 |
11 | class MyAPP2UITests: XCTestCase {
12 |
13 | override func setUp() {
14 | super.setUp()
15 |
16 | // Put setup code here. This method is called before the invocation of each test method in the class.
17 |
18 | // In UI tests it is usually best to stop immediately when a failure occurs.
19 | continueAfterFailure = false
20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
21 | XCUIApplication().launch()
22 |
23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
24 | }
25 |
26 | override func tearDown() {
27 | // Put teardown code here. This method is called after the invocation of each test method in the class.
28 | super.tearDown()
29 | }
30 |
31 | func testExample() {
32 | // Use recording to get started writing UI tests.
33 | // Use XCTAssert and related functions to verify your tests produce the correct results.
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/swift 4 demo/23.注释.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //第一种 单行注释
8 | // 这是一个单行注释
9 |
10 | //第二种 多行注释
11 | /* 这是一个注释多行 */
12 |
13 | //第三种 有头和尾的多行注释
14 | /* 注释的开头
15 | /*注释的内部部分 */
16 | 注释的结尾 */
17 |
18 | //第四种 标记函数
19 | //MARK: - 标记在函数前面
20 |
21 | //第五种Java风格注释
22 | /*
23 | * @func 定义一个查询学生成绩的接口
24 | * @param name 学生姓名
25 | * @param nums 序号
26 | * @return 返回查询到的成绩
27 | */
28 |
29 | //第六种 苹果官方使用///注释属性
30 | /// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
31 | //public static var timeIntervalSinceReferenceDate: TimeInterval { get }
32 |
33 | //第七种 苹果官方注释方法
34 |
35 | /// 定义一个查询学生成绩的接口
36 | ///
37 | /// - Parameters:
38 | /// - name: 学生姓名
39 | /// - nums: 学号
40 | /// - Returns: 返回学生成绩
41 | func getScoreStudentName(name: String,nums: String) -> Double {
42 | return 0.0;
43 | }
44 |
45 | //好处:
46 | //按住option + 鼠标的左键查看方法说明
47 | let score = getScoreStudentName(name: "小明", nums: "12");
48 |
49 |
50 | //第八种 和上面差不多
51 | /**
52 | 定义一个查询学生成绩的接口
53 |
54 | let score = getScoreStudentName(name: "酷走天涯", nums: "111111111")
55 |
56 | - Parameters:
57 | - name: 学生姓名
58 | - nums: 学号
59 | - Returns: 返回学生成绩
60 | */
61 |
62 | //提示 最好使用苹果官方提供的操作简单 option + command + /
63 |
64 | //其他特别查找标注
65 | // MARK: 粗体标签
66 | // WARNING: 警告提示
67 | // TODO: 提示未完成的地方
68 | // FIXME: 提示需要修改的地方
69 |
70 |
71 |
--------------------------------------------------------------------------------
/swift 4 demo/16.下标.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 | //定义下标写法
7 | //subscript(index : Int) -> Int {
8 | // get{
9 | //
10 | // }
11 | // set(newValue){
12 | //
13 | // }
14 | //}
15 |
16 | //应用
17 | //a.需求:获取字符串第2个字符
18 | //一般写法:
19 | func getCharacterByIndex(n: Int,str: String) -> Character? {
20 | let index = str.index(str.startIndex, offsetBy: n);
21 | return str[index];
22 | }
23 | let c = getCharacterByIndex(n: 1, str: "你好吗");
24 |
25 |
26 | //扩展下标写法(好东西,写共有方法,经常用)
27 | extension String {
28 | subscript(n : Int) -> Character {
29 | let index = self.index(self.startIndex , offsetBy: n);
30 | return self[index];
31 | }
32 | }
33 | let string = "你好,我是酷走天涯"
34 | let bbbstr = string[2];
35 |
36 | //b.需求:.截取范围内的字符串
37 | extension String{
38 | subscript(begin:Int,end:Int) -> String{
39 | //判断begin小于end
40 | let start = self.index(startIndex, offsetBy: begin)
41 | let end = self.index(startIndex, offsetBy: end);
42 | let str = self[start ... end];
43 | return String(str);
44 | }
45 | }
46 | // 调用
47 | let subString = string[2,4]
48 |
49 | //更优雅的写法
50 | extension String{
51 | subscript(range:ClosedRange)->String{
52 | let range = self.index(startIndex, offsetBy: range.lowerBound )...self.index(startIndex, offsetBy: range.upperBound)
53 | return String(self[range]);
54 | }
55 | }
56 | let subString1 = string[1...5];
57 | //1.下标的参数不能是inout 修饰的类型
58 | //2.类或者结构体可以提供任意多个下标实现
59 | //3.下标也是可以在枚举中使用的
60 |
61 |
--------------------------------------------------------------------------------
/swift 4 demo/17.泛型.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 | //你可以通用形式的函数和方法,以及类、枚举、结构。
7 |
8 | //需求: 讲一个不确定的类型初始化后放入一个数组中,设计一个这样的接口
9 | func makeArray- (repeating item: Item, numberOfTimes: Int) -> [Item] {
10 | var result = [Item]();
11 | for _ in 0..(_ lhs: T, _ rhs: U) -> Bool
20 | where T.Iterator.Element: Equatable,T.Iterator.Element
21 | == U.Iterator.Element {
22 |
23 | for lhsItem in lhs {
24 | for rhsItem in rhs {
25 | if lhsItem == rhsItem {
26 | return true
27 | }
28 | }
29 | }
30 | return false;
31 | }
32 | //认识几个东西
33 | //Sequence: 一种协议 序列
34 | //Equatable : 一种协议 实现了这种协议的对象 就能用 == 进行判断了
35 | //Iterator: 提供了序列的迭代接口
36 | //Element: 序列中的元素
37 |
38 | //理解
39 | //如果TU实现了Sequence协议,并且它里面的元素实现了Equatable协议 你就可以调用这个接口
40 |
41 | //如何返回相交的数据?
42 | func anyCommonElements1(_ lhs: T,_ rhs: U) -> Array
43 | where T.Iterator.Element: Equatable,T.Iterator.Element == U.Iterator.Element{
44 |
45 | var array:[T.Iterator.Element] = Array()
46 | for lhsItem in lhs {
47 | for rhsItem in rhs {
48 | if lhsItem == rhsItem {
49 | array.append(lhsItem)
50 | }
51 | }
52 | }
53 | return array
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/swift 4 demo/1初识.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/Image.swift:
--------------------------------------------------------------------------------
1 | //
2 | // Image.swift
3 | // 34.关联
4 | //
5 | // Created by weifan on 2018/2/6.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | private var animatedImageDataKey: Void?
12 |
13 | extension UIImage {
14 | //错误 扩展可能不包含存储属性
15 | //因为在扩展中无法实现"实例变量",所以属性setter没办法实现
16 | //所以使用关联对象 AssociatedObject
17 |
18 | // var animatedImageData: Data?{
19 | // get {
20 | // //写死不好只能PNG
21 | // let data = UIImagePNGRepresentation(self);
22 | // return data;
23 | // }
24 | // set {
25 | // animatedImageData = newValue;
26 | // }
27 | //
28 | // }
29 |
30 | //使用
31 | /**
32 | * Returns the value associated with a given object for a given key.
33 | * 返回与给定对象对于一个给定的键相关联的值。
34 | * @param object The source object for the association.该协会的源对象。
35 | * @param key The key for the association.关联的key。
36 | *
37 | * @return The value associated with the key \e key for \e object.
38 | * 返回值关联键和源对象
39 | * @see objc_setAssociatedObject
40 | */
41 | // @available(iOS 3.1, *)
42 | // public func objc_getAssociatedObject(_ object: Any, _ key: UnsafeRawPointer) -> Any?
43 |
44 | var animatedImageData : Data? {
45 | get {
46 | return objc_getAssociatedObject(self, &animatedImageDataKey) as? Data
47 | }
48 | set {
49 |
50 | objc_setAssociatedObject(self, &animatedImageDataKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
51 | }
52 | }
53 |
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/swift 4 demo/2数据类型.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/swift 4 demo/7流控制.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/swift 4 demo/4字典.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //1.基本
6 | //1_1创建
7 | var namesOfIntegers = [Int: String]();
8 | var airportsDic : [String : String] = ["YYZ":"Toronto","DUB":"Dublin"];
9 | var airPortsDic1 = ["YYZ":"Toronto Pearson","DUB":"Bublin"];
10 | //1_2 赋值,修改值
11 | namesOfIntegers = [16:"shiliu",15:"shiwu"];
12 | namesOfIntegers[16] = "sixteen";
13 | print(namesOfIntegers);
14 | airportsDic["YYZ"] = "London Heathrow";
15 |
16 | // 更新值返回久的值,值可能为nil
17 | if let oldValue = airportsDic.updateValue("Dublin Airport", forKey: "YYZ"){
18 | print("The old value for DUB was \(oldValue).");
19 | }
20 | if let x = namesOfIntegers[1] {
21 | print(x);
22 | }
23 | //清空
24 | namesOfIntegers = [:];
25 | //检测是否为空
26 | if namesOfIntegers.isEmpty {
27 | print("字典是空的");
28 | }
29 | namesOfIntegers[1] = "one";
30 | namesOfIntegers[2] = "two";
31 | print(namesOfIntegers);
32 | //移除值
33 | namesOfIntegers[1] = nil;
34 | namesOfIntegers.removeValue(forKey: 2);
35 | print(namesOfIntegers);
36 | //遍历值
37 | for (airPortsDicCode, airPortsDicName) in airportsDic {//字典item
38 | print("\(airPortsDicCode):\(airPortsDicName)");
39 | }
40 |
41 | for airportsDicCode in airportsDic.keys {//字典的key
42 | print("Airport code: \(airportsDicCode)");
43 | }
44 |
45 | for airportsDicName in airportsDic.values {//字典的值
46 | print("Airport name: \(airportsDicName)");
47 | }
48 |
49 | //中级
50 | //字典存储方是乱序的
51 | var dicA1 = [1:"23",3:"3"];
52 | dicA1.remove(at: dicA1.index(dicA1.startIndex, offsetBy: 1));
53 | print(dicA1);
54 | //怎么将字典的keys或者Values放入数组中
55 | let keys = [Int](dicA1.keys);
56 | let values = [String](dicA1.values);
57 | //let keys = dic.keys 获取的不是数组,但是可以通过for...in遍历它里面的元素
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/swift 4 demo/30.文本输出.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | class Person : CustomStringConvertible {
8 | var name : String;
9 | var age : Int;
10 | var weight : Double;
11 | init(name : String, age : Int,weight : Double) {
12 | self.name = name;
13 | self.age = age;
14 | self.weight = weight;
15 | }
16 | var description: String{
17 | return name + String(age) + String(weight);
18 | }
19 |
20 | }
21 |
22 | // 获取对象文本
23 | let person = Person(name: "小星星",age: 25,weight: 150);
24 | let des = String(describing: person); //描述
25 | print(des);
26 |
27 | //运行结果
28 | //酷走天涯25150.0
29 |
30 | //提示:
31 | //让对象文本输出很简单,只是实现协议CustomStringConvertible 即可
32 |
33 | //思考: 下面的代码为什么不正确
34 | //class Person1 : NSObject,CustomStringConvertible {
35 | // var name : String;
36 | // var age : Int;
37 | // var weight : Double;
38 | // init(name: String,age: Int,weight: Double) {
39 | // self.name = name;
40 | // self.age = age;
41 | // self.weight = weight;
42 | // }
43 | //
44 | // var description: String{
45 | // return name + String(age) + String(weight);
46 | // }
47 | //
48 | //}
49 |
50 | //提示:
51 | //NSObject 里面已经实现了协议CustomStringConvertible,
52 | //不能重复实现,我们只需要重写var description: String 即可
53 |
54 | //改正
55 | class Person2 : NSObject {
56 | var name : String;
57 | var age : Int;
58 | var weight : Double;
59 | init(name: String,age: Int,weight: Double) {
60 | self.name = name;
61 | self.age = age;
62 | self.weight = weight;
63 | }
64 |
65 | override var description: String{
66 | return name + String(age) + String(weight);
67 | }
68 |
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | }
88 | ],
89 | "info" : {
90 | "version" : 1,
91 | "author" : "xcode"
92 | }
93 | }
--------------------------------------------------------------------------------
/swift 4 demo/24. 元类型.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //认识AnyClass
8 | typealias AnyClass = AnyObject.Type
9 | //所有类都隐式实现的协议
10 | //当你在一个或者类中使用@obj创建类方法或者属性时,会被系统知道,认为它是AnyClass的方法
11 | //我们看一个例子
12 | class Student {
13 | @objc static let describe = "这是一个学生类";
14 | @objc class func getDefaultValue() -> Int {
15 | return 0;
16 | }
17 | }
18 |
19 | //定义两个方法
20 | func getDefaultValue(_ c : AnyClass) -> Int? {
21 | return c.getDefaultValue?();
22 | }
23 |
24 | func getDescribeValue(_ c : AnyClass) -> String? {
25 | return c.describe;
26 | }
27 |
28 | //接下来我们调用一下
29 | print(getDefaultValue(Student.self));
30 | print(getDescribeValue(Student.self));
31 |
32 | //结果
33 | //Optional(0)
34 | //Optional("这是一个学生类")
35 |
36 | //OC的类跟AnyObject 没关系 跟NSObject 有关系
37 | print(getDescribeValue(NSString.self));
38 | print(getDefaultValue(NSString.self));
39 |
40 | //类的类型
41 | //a.获取类的类型
42 | class Student1 {
43 | required init(){//required(必须)
44 |
45 | }
46 | }
47 |
48 | class Student2 : Student1{
49 | required init(){//required(必须)
50 |
51 | }
52 | }
53 |
54 | //定义类的类型变量
55 | let StudentType : Student1.Type = Student1.self;
56 | let StudentType2 : Student2.Type = Student2.self;
57 | // AnyClass 可以接受任何类的类型
58 | let type : AnyClass = Student1.self;
59 | let type2 : Student1.Type = StudentType2;
60 |
61 | //通过类的类型初始化对象
62 | let stu = StudentType.init();
63 | let stu2 = type2.init();
64 |
65 | //注意:
66 | //通过这种方式初始化对象的时候,必须有required 修饰的初始化方法才行
67 |
68 | protocol TeacherDelegate {
69 |
70 | }
71 |
72 | class Teacher : TeacherDelegate {
73 |
74 | }
75 |
76 | let teacherType = Teacher.self
77 | func isStudentDelegate(_ type:AnyClass ) -> Bool{
78 | if type is TeacherDelegate.Type {
79 | return true;
80 | }
81 | return false;
82 | }
83 | print(isStudentDelegate(teacherType));
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | }
88 | ],
89 | "info" : {
90 | "version" : 1,
91 | "author" : "xcode"
92 | }
93 | }
--------------------------------------------------------------------------------
/swift 4 demo/6集合.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //定义
6 | var letters = Set();
7 | var favoriteGenres: Set = ["Rock","Classical","Hip hop"];
8 | var favoriteGenres2: Set = ["Rock","Classical","Hip pop"]//类型推断
9 |
10 | //清空
11 | letters = [];
12 | letters.removeAll();
13 |
14 | //插入
15 | letters.insert("a");
16 |
17 | //判断集合是否为空
18 | if favoriteGenres.isEmpty {
19 |
20 | }
21 | print(favoriteGenres);
22 |
23 | //插入元素
24 | favoriteGenres.insert("Jazz");
25 | print(favoriteGenres);
26 |
27 | //移除元素
28 | if let removeGenre = favoriteGenres.remove("Rock") {
29 | print("\(removeGenre)? I'm over it.");
30 | } else {
31 | print("I never much cared for that.");
32 | }
33 |
34 | //是否包含元素
35 | if favoriteGenres.contains("Funk") {//包含
36 | print("I get up on the good foot");
37 | }else{
38 | print("It's too funky in here.");
39 | }
40 |
41 | // 遍历集合中的元素
42 | for genre in favoriteGenres {
43 | print("\(genre)");
44 | }
45 |
46 | //排序遍历
47 | let favoriteGenres3 : Set = ["A","B","C","G","D"];
48 | for genre in favoriteGenres3 {
49 | print("\(genre)");
50 | }
51 | let favoriteGenres4 : Set = ["A","C","G","d","r"];
52 | for genre in favoriteGenres4 {
53 | print("\(genre)");
54 | }
55 | //集合的运算
56 | //交集
57 | let intersection = favoriteGenres3.intersection(favoriteGenres4);
58 | print("输出交集:\(intersection)");
59 |
60 | //补集(并集且去交集)
61 | favoriteGenres3.symmetricDifference(favoriteGenres4);
62 |
63 | //并集
64 | favoriteGenres3.union(favoriteGenres4);
65 |
66 | //favoriteGenres3 的子集
67 | favoriteGenres3.symmetricDifference(favoriteGenres4).sorted();
68 |
69 | //让学习成为一种习惯
70 | let houseAnimals : Set = ["",""];
71 | let farmAnimals : Set = ["","","",""];
72 | let cityAnimals : Set = ["",""];
73 |
74 | houseAnimals.isSubset(of: farmAnimals);//true//子集
75 | farmAnimals.isSuperset(of: houseAnimals)//true//父集
76 | farmAnimals.isDisjoint(with: cityAnimals)//不相交的
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // MyApp1
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | import MySDK
11 |
12 | class ViewController: UIViewController {
13 | @IBAction func buttonAction(_ sender: Any) {
14 |
15 | let label = UILabel();
16 | self.view.addSubview(label);
17 | label.frame = CGRect(x: 10, y: UIScreen.main.bounds.height - 200, width: UIScreen.main.bounds.width - 20, height: 200);
18 | label.backgroundColor = UIColor.yellow;
19 | label.textAlignment = .center;
20 | label.numberOfLines = 0;
21 | //测试
22 | // let welCome = WelComeTest.init();
23 | // welCome.sayHello();
24 | // label.text = welCome.sayHello();
25 | //访问权限:Open
26 | // let openTest = OpenTest(string: "hello");
27 | // let openTestClass = OpenTestClass(string: "hello");
28 | //
29 | // label.text = "\(openTest.returnStr()!)\n \(openTestClass.returnStr()!)";
30 |
31 | //访问权限:Public
32 | // let publicTest = PublicTest(string: "hello");
33 | // label.text = publicTest.returnStr();
34 |
35 | //访问权限:InternalTest由PublicTest调用
36 | // let publicTest = PublicTest(string: "hello");
37 | // label.text = publicTest.internalTest();
38 |
39 | //访问权限:FilePrivate
40 | // let publicTest = PublicTest(string: "hello");
41 | // label.text = publicTest.filePrivateTest()
42 | //访问权限:Private
43 | let publicTest = PublicTest(string: "hello");
44 | label.text = publicTest.privateTest();
45 |
46 |
47 | }
48 |
49 | override func viewDidLoad() {
50 | super.viewDidLoad()
51 | // Do any additional setup after loading the view, typically from a nib.
52 |
53 |
54 | }
55 |
56 | override func didReceiveMemoryWarning() {
57 | super.didReceiveMemoryWarning()
58 | // Dispose of any resources that can be recreated.
59 | }
60 |
61 |
62 | }
63 |
64 | //MARK: open 测试重写
65 | class OpenTestClass: OpenTest {
66 |
67 | override func returnStr() -> String? {
68 | return "继承并重写"
69 | }
70 |
71 | }
72 |
73 |
74 |
--------------------------------------------------------------------------------
/swift 4 demo/31.黑魔法swizzle.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //举个例子理解一下
8 | //如果你想统计App中所有页面的点击事件,最简单会计的方式是什么?
9 | extension UIButton{
10 | class func swip() {
11 | //创建一个结构体,写个静态变量
12 | struct T {
13 | static let x : Bool = {
14 | let cls : AnyClass = UIButton.self;
15 | //创建消息对象
16 | let originalSelector = #selector(UIButton.sendAction(_:to:for:));
17 | let swizzleSelector = #selector(UIButton.swizzle_sendAction(action:to:forEvent:))
18 | // 创建方法
19 | let ori_method = class_getInstanceMethod(cls, originalSelector);
20 | let swi_method = class_getInstanceMethod(cls, swizzleSelector);
21 |
22 | print(ori_method);
23 | print(swi_method);
24 |
25 | // 交换两个方法的实现部分
26 | method_exchangeImplementations(ori_method!, swi_method!);
27 | print("执行了...............");
28 | return false;
29 | }();
30 | }
31 | // 这里必须执行一下,不然没法创建静态变量
32 | T.x;
33 | }
34 |
35 | // 定义要交换的函数
36 | @objc public func swizzle_sendAction(action: Selector,to: AnyClass!,forEvent: UIEvent!){
37 | // 定义一个累加器
38 | struct button_tap_count{
39 | static var count = 0
40 | }
41 | button_tap_count.count += 1
42 | print(button_tap_count.count)
43 | // 看似好像调用了自己构成死循环,但是 我们其实已经将两个方法名的实现进行了调换 所以 其实我们调用的是 方法sendAction:to:forEvent 的实现 这样就可以在不破环原先方法结构的基础上进行交换实现
44 | swizzle_sendAction(action: action, to: to, forEvent: forEvent)
45 | }
46 |
47 | }
48 |
49 | //使用
50 |
51 | class ViewController: UIViewController {
52 | var button: UIButton?
53 |
54 | func myView() -> Void {
55 | // 执行一次交换方法
56 | button = UIButton();
57 | UIButton.swip();
58 | button?.addTarget(self, action: #selector(tap(button:)), for: .touchUpInside)
59 | }
60 |
61 | @objc func tap(button:UIButton){
62 | print("你好")
63 | }
64 | }
65 |
66 | let VC = ViewController();
67 | VC.myView()
68 |
69 | //后面部分当你点击按钮才会出现结果
70 | //所以最好在工程测试,playground没法测试
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // 34.关联
4 | //
5 | // Created by weifan on 2018/2/6.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // MyApp1
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp2/MyApp2/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // MyAPP2
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/swift 4 demo/25.控件命名.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //为什么要使用空间命名
8 | //如果你不想给类型加前缀,然后又想避免出现类名冲突,这个时候,就可以使用它了
9 |
10 | //代码实例
11 | struct MyClassContrainer1{
12 | class Student {
13 | func getName() -> String {
14 | return "name";
15 | }
16 | }
17 | }
18 |
19 | struct MyClassContrainer2{
20 | class Student {
21 | func getName() -> String {
22 | return "name";
23 | }
24 | }
25 | }
26 |
27 | let student1 = MyClassContrainer1.Student();
28 | let student2 = MyClassContrainer2.Student();
29 |
30 | //应用场景1:
31 | //我们想通知中心发送一条消息
32 |
33 | class NotificationText{
34 |
35 | @objc func SocketConnectFailure(notification: Notification) -> Void {
36 | print("我接到通知只能是失败的");
37 | }
38 |
39 | func addNotification() {
40 | NotificationCenter.default.addObserver(self, selector: #selector(SocketConnectFailure(notification:)), name: Notification.Name("SocketConnectFailure"), object: nil);
41 | }
42 |
43 | func postNotification() {
44 | NotificationCenter.default.post(name: Notification.Name("SocketConnectFailure"), object: ["errorCode":000]);
45 |
46 | }
47 |
48 | }
49 |
50 | let notificationText = NotificationText();
51 | notificationText.addNotification();
52 | notificationText.postNotification();
53 |
54 | //思考: 我们如果能够更加直观的知道SocketConnectFailure 是哪一类消息呢?
55 |
56 | //命名空间优雅的写法
57 |
58 | extension Notification.Name {
59 | /// 使用命名空间的方式
60 | public struct SocketTask {
61 | public static let connectFailure = Notification.Name(rawValue: "SocketConnectFailure")
62 | public static let connectSuccess = Notification.Name(rawValue: "SocketConnectSuccess")
63 | }
64 | }
65 |
66 | class Notification2 {
67 |
68 | @objc func SocketConnectTask(notification: Notification) -> Void{
69 |
70 | print("我接到通知只能是失败的");
71 | }
72 |
73 | func addNotification() -> Void {
74 | NotificationCenter.default.addObserver(self, selector: #selector(SocketConnectTask(notification:)), name: Notification.Name.SocketTask.connectFailure, object: ["errorCode":000])
75 | }
76 |
77 | func postNotification() -> Void {
78 | NotificationCenter.default.post(name: Notification.Name.SocketTask.connectFailure, object: nil);
79 | }
80 |
81 | }
82 |
83 | let notificationText2 = Notification2();
84 | notificationText2.addNotification();
85 | notificationText2.postNotification();
86 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/swift 4 demo/12.协议protocol.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 | import Foundation
5 | import SceneKit
6 |
7 | var str = "Hello, playground"
8 | // 学习目标
9 | // Swift中的协议和OC中协议的区别
10 | // Swift中的协议的用法
11 | // 怎么使用
12 |
13 | // swift 和 Object-C 协议的区别
14 |
15 | // 1.OC中的协议只能用于类,swift中的协议不仅能用于类,还能用于结构体和枚举
16 | // 2.OC中的协议可以设置可选实现,但Swift中的协议必须实现
17 | // 3.Swift中的协议增加了一个关键字mutating可以决定结构体和枚举实现协议的时候,是否可以修改属性的值
18 |
19 | //Swift协议(protocol)
20 |
21 | //定义一个协议
22 | protocol LoveMusic {
23 | func songMusic();
24 | var simpleDescription: String {
25 | get
26 | }
27 | mutating func modifyMusic(name : String);
28 | }
29 |
30 | //协议可以继承协议(多个)
31 |
32 | protocol Love{
33 | func loveJazz();
34 | }
35 |
36 | protocol Skill{
37 | func highPitch();
38 | }
39 |
40 | protocol LoveMusic1 : Love,Skill{
41 | func songMusic();
42 | var simpleDescription : String {
43 | get
44 | }
45 | mutating func modifyMusic(name : String);
46 | }
47 | //注意方法不能重写
48 |
49 | //.可以实现多个协议
50 | class Student : Love,Skill{
51 | func loveJazz() {
52 | print("爵士乐");
53 | }
54 |
55 | func highPitch() {
56 | print("高音");
57 | }
58 |
59 | internal var name : String = "";
60 | internal func modifyMusic(name : String) {
61 |
62 | }
63 | }
64 |
65 | // 协议类型的变量
66 | // 协议的 标准写法
67 | var prptocol : LoveMusic;
68 | var delegate : LoveMusic & Love & Skill;
69 |
70 | // 协议做函数参数
71 | func getStudent(student : Love & Skill){
72 | student.loveJazz();
73 | student.highPitch();
74 | }
75 |
76 | //实现协议可选函数或者变量
77 | //1.在协议的前面加上关键字 @objc ,再在你需要设置可选方法或者属性的前面加上关键字@objc 和 optional 即可
78 | @objc protocol OptionalProtocol{//可选
79 | @objc optional func optionalMethod()
80 | @objc optional var name:String {set get}
81 | }
82 |
83 | //2.还有另外一种方法可以实现可选协议就是遵守NSObjectProtocol
84 | // 实例代码:
85 | public protocol SCNSceneRendererDelegate : NSObjectProtocol {
86 | func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval);
87 | }
88 |
89 | //你应该注意的
90 | //
91 | //在协议中定义属性的时候,必须使用set get \ get\ 修饰,但不能只用set修饰,swift要求我们属性必须有get方法
92 | //
93 | //类不能多次继承同一个协议
94 |
95 | //class Student4 : Love,Love {//错误的
96 | // internal var name : String;
97 | // internal func modifyMusic(name: String){
98 | //
99 | // }
100 | //}
101 |
102 | //如果多个协议中有相同的方法和属性,只需要实现一次即可
103 | protocol Love3{
104 | var name:String{get set};
105 | mutating func modifyMusic(name : String);
106 | }
107 |
108 | protocol Skill3{
109 | var name : String{
110 | get set
111 | };
112 | mutating func modifyMusic(name : String);
113 | }
114 |
115 | class Student3 : Love3 , Skill3{
116 | internal var name : String = "";
117 | internal func modifyMusic(name: String) {
118 |
119 | }
120 | }
121 |
122 | //一般人不知道
123 | //需求:创建一个协议只能被类遵守
124 | protocol MyClassDelegate : class {
125 | func getName();
126 | }
127 | //什么时候必须使用类协议?
128 | //当你要使用weak修饰的时候
129 | protocol UDPDelegate:class{
130 |
131 | }
132 |
133 | class UDP{
134 | weak var delegate : UDPDelegate?;
135 | }
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
--------------------------------------------------------------------------------
/swift 4 demo/34.关联/34.关联/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MyApp1/MyApp1/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/swift 4 demo/27.探究Self的用途.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 | import Foundation
5 |
6 | var str = "Hello, playground"
7 |
8 | // 如果实现对象拷贝
9 | //第一种方法:
10 | //1.遵守协议 NSCopying
11 | //2.实现协议
12 | //NSCopying 协议
13 | //public protocol NSCopying {
14 | //
15 | //
16 | // public func copy(with zone: NSZone? = nil) -> Any
17 | //}
18 |
19 | //示例代码:
20 | class Student : NSCopying {
21 | var name : String;
22 | required init(name : String){
23 | self.name = name;
24 | }
25 | func copy(with zone: NSZone? = nil) -> Any{
26 | let student = Student(name: self.name);
27 | return student;
28 | }
29 | }
30 | //使用
31 | let student1 = Student(name : "星星");
32 | let student2 : Student = student1.copy() as! Student;
33 | print(student2.name);
34 | student2.name = "小星星";
35 | print(student1.name);
36 | print(student2.name);
37 |
38 | //运行:
39 | //星星
40 | //星星
41 | //小星星
42 |
43 | //分析:
44 | //由于NSCopying协议里面的方法copy返回的值为Any系统没法推断出来的是Student类型,所以必须as!指明是Student类型过程有点繁琐
45 |
46 | //第二种方法
47 | //1.创建一个一个required 修饰的初始化方法
48 | //2.自定义一个copy
49 | //示例代码
50 | class StudentA{
51 | var name : String;
52 | required init(name : String){
53 | self.name = name;
54 | }
55 |
56 | func copy() -> Self {
57 | let result = type(of: self).init(name: self.name);
58 | return result;
59 | }
60 | }
61 | //使用
62 | let studentA1 = StudentA(name: "星星");
63 | let studentA2 = studentA1.copy();//
64 | studentA2.name = "小星星";
65 | print(studentA1.name);
66 | print(studentA2.name);
67 | //结果
68 | //星星
69 | //小星星
70 | //分析
71 | //type(of: self) 获取到自身类的类型,然后通过init(name:self.name) 初始化一个对象 ,初始化的对象为Self 类型
72 |
73 | //思考:初始化为什么要使用required修饰的初始化
74 | //参考元类型
75 | //设计规范:
76 | //像拷贝这种行为,不是很多对象都具体的行为,我们应该把它设计成协议,让类去集成它;
77 |
78 | protocol Copy {
79 | func copy() -> Self;
80 | }
81 |
82 | class Student1 {
83 | var name : String;
84 | init(name : String) {
85 | self.name = name;
86 | }
87 |
88 |
89 | //其实如果我们但从技术上实现,下面也可
90 | func copy() -> Student1{
91 | let result = Student1.self.init(name:self.name)
92 | return result
93 | }
94 |
95 | func copy1() -> Student1{
96 | let result = Student1.init(name: self.name)
97 | return result
98 | }
99 |
100 | func copy2() -> Student1{
101 | let result = Student1(name: self.name)
102 | return result
103 | }
104 |
105 | }
106 |
107 | //给多个对象扩展相同的方法
108 |
109 | //需求:给数字扩展一个返回平方的操作
110 |
111 | //定义个协议
112 | protocol NumberProtocol {
113 |
114 | }
115 | //扩展实现协议
116 | extension Int : NumberProtocol {
117 |
118 | }
119 |
120 | extension Double : NumberProtocol{
121 |
122 | }
123 |
124 | //给协议扩展方法
125 | extension NumberProtocol {
126 | // 我们不确定返回的Self 到底是什么类型
127 | func squareValue() -> Self{
128 | if self is Int {
129 | let n = self as! Int;
130 | return n*n as! Self;
131 | }
132 |
133 | if self is Double {
134 | let n = self as! Double;
135 | return n*n as! Self;
136 | }
137 |
138 | return 0 as! Self;
139 | }
140 | }
141 |
142 | print(3.44.squareValue());
143 | print(3.squareValue());
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
--------------------------------------------------------------------------------
/swift 4 demo/33.访问权限/MySDK/MySDK/PublicTest.swift:
--------------------------------------------------------------------------------
1 | //
2 | // PublicTest.swift
3 | // MySDK
4 | //
5 | // Created by weifan on 2018/1/26.
6 | // Copyright © 2018年 xiaoguo. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | public class PublicTest {
12 |
13 | public var string : String?;
14 | public init(string: String) {
15 | self.string = string;
16 | }
17 |
18 | deinit {
19 | print("父类");
20 | }
21 |
22 | public func returnStr() -> String? {
23 | let publicTestClass = PublicTestClass(string: "hello");
24 |
25 | return "\(string ?? "hello")-2.public:公有访问权限,类或者类的公有属性或者公有方法可以从文件或者模块的任何地方进行访问,但是Swift3.0后在其他模块不能被继承和重写.\n\(publicTestClass.returnStr()!)";
26 | }
27 |
28 | public func internalTest() -> String? {
29 | let internalTest = InternalTest(string: "hello");
30 | let internalTestClass = InternalTestClass(string: "hello");
31 |
32 | return "\(internalTest.returnStr()!)\n\(internalTestClass.returnStr()!)";
33 | }
34 |
35 | public func filePrivateTest() -> String? {
36 | let filePrivateTest = FilePrivateTest(string: "hello");
37 | return filePrivateTest.returnStr();
38 | }
39 |
40 | public func privateTest() -> String? {
41 | let privateTest = PrivateTest(string: "hello");
42 | return privateTest.returnStr();
43 | }
44 |
45 | private class PrivateTest{
46 |
47 | public var string : String?;
48 | public init(string: String) {
49 | self.string = string;
50 | }
51 |
52 | deinit {
53 | print("父类");
54 | }
55 |
56 | public func returnStr() -> String? {
57 |
58 | let privateTestClass = PrivateTestClass(string: "hello");
59 |
60 | return "\(string ?? "hello")-private:私有访问权限,被private修饰的类或者类的属性或方法可以在同一个物理文件中的同一个类型(包含extension)访问,继承和重写.\n\(privateTestClass.returnStr()!)";
61 | }
62 |
63 | }
64 |
65 | private class PrivateTestClass : PrivateTest {
66 | override func returnStr() -> String? {
67 | return "统一文件统一类中继承和重写"
68 | }
69 | }
70 |
71 |
72 | }
73 | //MARK: filePrivate
74 | fileprivate class FilePrivateTest {
75 |
76 | public var string : String?;
77 | public init(string: String) {
78 | self.string = string;
79 | }
80 |
81 | deinit {
82 | print("父类");
83 | }
84 |
85 | public func returnStr() -> String? {
86 |
87 | let filePrivateTestClass = FilePrivateTestClass(string: "hello");
88 |
89 | return "\(string ?? "hello")-4.filePrivate:文件私有访问权限,超出该物理文件那么有着fileprivate访问权限的类, 属性和方法就不能被访问,继承和重写.\n\(filePrivateTestClass.returnStr()!)";
90 | }
91 |
92 | }
93 |
94 |
95 |
96 | // MARK: public:不能高于父类权限 public
97 | public class PublicTestClass : PublicTest {
98 |
99 | deinit {
100 | print("子类");
101 | }
102 |
103 | public override func returnStr() -> String? {
104 | return "模块内继承和重写"
105 | }
106 | }
107 |
108 | // MARK: internal:不能高于父类权限 internal
109 | class InternalTestClass : InternalTest {
110 | public override func returnStr() -> String? {
111 | return "模块内继承和重写"
112 | }
113 | }
114 |
115 | // MARK: filePrivate:不能高于父类权限 filePrivate
116 | fileprivate class FilePrivateTestClass : FilePrivateTest {
117 | deinit {
118 | print("FilePrivateTestClass 子类")
119 | }
120 | public override func returnStr() -> String? {
121 | return "文件内继承和重写"
122 | }
123 | }
124 |
125 |
--------------------------------------------------------------------------------
/swift 4 demo/3Array.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //1.基本
6 | //定义
7 | let list1 = ["你好","2","3","4"];
8 | let list2:[String] = ["你好","2","3","4"];
9 | let list3:[Any] = ["你好","2","3",3,UILabel()];
10 | let lsit4:NSArray = ["你好","2","3","4",UILabel()];//OC
11 | let lsit5:NSMutableArray = ["1","2","3","4"];//OC
12 |
13 | //清空数组
14 | let listText1 : [Any] = ["你好","2","3","4",1];
15 | var listText2 : [Any] = ["你好","2","2","4",3];
16 | listText2.removeAll() // 如果定义为var
17 | listText2 = [] // 如果定义为var
18 |
19 | //取代操作
20 | var listA1 : [Any] = ["1","2","3","4","5","6","7","8"];
21 | listA1[2...6] = ["Bananas","Apples"]// 将数组4...6的范围用指定的数组取代
22 | print(listA1);
23 | //插入移除操作
24 | var listA2 : [Any] = ["1","2","3","4","5"];
25 | listA2.insert("maple Syrup", at: 0);
26 | print(listA2);
27 | let mapleSyrup = listA2.remove(at: 2);
28 | print(listA2);
29 |
30 | let apples = listA2.removeLast();
31 |
32 | print(listA2);
33 |
34 | //遍历数组
35 |
36 | var listA3 : [Any] = ["1","2","3","4","5",1,2,3,4,5];
37 | for item in listA3 {
38 | print(item);
39 | }
40 |
41 | for (index,value) in listA3.enumerated() {
42 | print("Item \(index + 1):\(value)")
43 | }
44 |
45 | //高级
46 | //认识 map
47 | //看一下它的定义
48 | //func map(_ transform: (Element) throws -> T) rethrows -> [T]
49 | //解释:
50 | //T为泛型
51 | //transform: 闭包函数 我们需要给它传入一个有参数返回值的闭包函数
52 | //[T]函数会返回给我们一个T类型的数组
53 | //应用:我们有这样一个需求,给一组数据中大于20的数组乘以3
54 |
55 | //1.定义一个数组
56 | var numbers = [21,19,7,12];
57 | //2.创建一个有一个参数一个返回值的闭包函数
58 | var closure = {(number : Int) -> Int in
59 | var result = number;
60 | if number > 20 {
61 | result *= 3;
62 | }
63 | return result;
64 | }
65 | //3.调用map函数
66 | print(numbers.map(closure))
67 | //由于swift有类型推断的功能,我们可以这样写
68 | let mappedNumbers = numbers.map({number -> Int in
69 | if number > 20{
70 | return 3 * number;
71 | }
72 | return number;
73 | })
74 |
75 | //如果没有逻辑判断,给所有值统一乘以3,我们甚至可以这样写
76 | let mappedNumbers1 = numbers.map { (number) -> Int in
77 | return number * 3;
78 | }
79 | //在简洁点
80 | let mappedNumbers2 = numbers.map { (number) -> Int in
81 | number * 3;
82 | }
83 |
84 | //如果你要将数组中的值全部替换为0 你可以这样写
85 | let mappedNumbers3 = numbers.map { _ in
86 | return 0;
87 | }
88 |
89 |
90 | //测试使用时间 for ... in 和 map
91 | //for ... in
92 | var numbersArray = Array.init(repeating: 3, count: 10000);
93 | print(numbersArray);
94 | var date1 = NSDate();//记录时间
95 | for i in 0.. Bool)) -> Element
113 | //areInIncreasingOrder 两个参数一个返回值得闭包
114 |
115 | //测试冒泡排序法时间
116 | var numbersArray3 : [Int] = [1,24,5,2,5,2,4,5,6,9,87];
117 | var date3 = NSDate();
118 | numbersArray3.sort(by: {$0 > $1});
119 | print(numbersArray3);
120 | print(NSDate().timeIntervalSince(date3 as Date))//0.002631
121 |
122 | var date4 = NSDate();
123 | for i in 0.. Bool{
36 |
37 | if score < 0 {
38 | throw ScoreInputError.lowZero(name: name, score: score)
39 |
40 | }else if score > 100{
41 | throw ScoreInputError.highHundred(name: name, score: score);
42 |
43 | }
44 | //开始录入
45 | //录入结束
46 | return true;
47 | }
48 |
49 | // 调用函数
50 | do {
51 | let result = try inputStudentName(name: "张丹", score:300)
52 | print(result);
53 | }catch ScoreInputError.lowZero(let name,let score){
54 | print("异常:\(name)的成绩为\(score) --小于0")
55 | }catch ScoreInputError.highHundred(let name,let score){
56 | print("异常:\(name)的成绩为\(score) -- 大于1000")
57 | }
58 |
59 | //处理不同类型的异常
60 |
61 | //需求:
62 | //用户输入的密码或者用户名为空抛出异常,如果是密码为空,输入密码为空的用户
63 | //定义异常
64 | enum OSUserLoginErrors : Error{
65 | case AllNoComplete;
66 | case UserNameEmpty;
67 | case PasswordEmpty(userName:String);
68 | }
69 | //定义含有异常机制的函数
70 | func verifyUserName(userName:String,password:String) throws -> Bool{
71 | if userName == "" && password == "" {
72 | throw OSUserLoginErrors.AllNoComplete;
73 | }else if (userName == "" && password != ""){
74 | throw OSUserLoginErrors.UserNameEmpty;
75 | }else if (userName == "" && password == ""){
76 | throw OSUserLoginErrors.PasswordEmpty(userName:userName);
77 | }
78 |
79 | return true;
80 | }
81 |
82 | //使用含有异常的函数
83 | do {
84 | try verifyUserName(userName: "你好", password: "");
85 | }catch OSUserLoginErrors.PasswordEmpty(let userName){
86 | print("用户:"+userName+"输入的密码为空");
87 | }catch OSUserLoginError.UserNameEmpty{
88 | print("用户名为空");
89 | }catch{
90 | print(error);
91 | }
92 |
93 | //用户:酷走天涯输入的密码为空
94 |
95 | //where在错误类型的使用
96 | enum ErrorType : Error {
97 | case success
98 | case error(code:Int)
99 | }
100 |
101 | func inputScore(score:Int) throws -> Bool{
102 | if score < 0 {
103 | throw ErrorType.error(code: 0);
104 | }else if score > 100 {
105 | throw ErrorType.error(code: 1);
106 | }
107 | return true;
108 | }
109 |
110 | do {
111 | let score = try inputScore(score: 1000);
112 | }catch ErrorType.error(let x) where x == 0{ // 可以做判断错误类型
113 | print("错误Code:x=0");
114 | }catch ErrorType.error(let x) where x == 1{
115 | print("错误Code:x=1")
116 | }catch ErrorType.error(let x) where x == 2{
117 | print("错误Code:x=2")
118 | }catch{
119 | print(error)
120 | }
121 | //运行结果:
122 | //第1步 抛出异常
123 | //第2步发生异常清理内存
124 | //NameEmpty
125 |
126 | //注意
127 | //1.类中使用defer,在对象方法中,使用defer,在defer块中,调用属性之前,必须初始化所有存储属性,重要事说三遍,所有的存储属性,所有的,存储属性
128 | //2.defer 在定义的 当前作用域 最后一步执行
129 |
130 | //try,try!,try?的用法
131 |
132 | //A.try
133 |
134 | class Student{
135 | var name : String?;
136 | init(name : String) {
137 | self.name = name;
138 | }
139 | }
140 |
141 | let student2 = try Student(name:"");
142 | print("运行结果aaa :\(student2)");
143 |
144 | //运行结果:
145 | //第一步
146 | //第二步 发生异常清理内存
147 |
148 | //结论:
149 | //我们发现print("运行结果(student)") 这句代码没有执行,说明发生了异常,后面的代码不会执行,但没有报错提示
150 |
151 | //B.try!
152 | let student = try! Student(name: "");
153 | print(student);
154 |
155 | //运行结果
156 | //第1步
157 | //第2步发生异常清理内存
158 | //fatal error: 'try!' expression unexpectedly raised an error: StudentError.NameEmpty: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.46.2/src/swift/stdlib/public/core/ErrorType.swift, line 178
159 |
160 | //结论:
161 | //一旦发生异常,系统会报错,后面的代码不再继续执行
162 |
163 | //C.try?
164 | //let student = try? Student(name: "");
165 | //print("运行结果\(student)");
166 |
167 | //第1步
168 | //第2步发生异常清理内存
169 | //运行结果nil
170 |
171 | //let student1 = try? Student(name: "123");
172 | //print("运行结果\(student1)")
173 |
174 |
175 | //应用1:
176 |
177 | public enum OSNetError:Error{
178 | /// 错误类型枚举值
179 | case requestError(RequestError)
180 | case responseError(ResponseError)
181 |
182 |
183 | /// 请求错误异常
184 | public enum RequestError{
185 | case invalidUrl(url:URL)
186 | case parameterException
187 | }
188 |
189 | /// 响应错误异常
190 | public enum ResponseError:Int{
191 | case dataFileNil
192 | case missingContentType
193 | case unacceptableStatusCode
194 | case dataFileReadFailed
195 | }
196 |
197 | }
198 |
199 | //请求:
200 |
201 | func requestUrl(url:URL)throws{
202 | if !url.absoluteString.contains("http"){
203 | throw OSNetError.requestError(.invalidUrl(url:url))
204 | }
205 | }
206 |
207 | //应用2: 如果你要使用的值有可能为nil,你最好的写法应该像下面这样
208 |
209 | let parameters = ["name":"星星"];
210 | guard let filter = CIFilter(name: "CIGaussianBlur",withInputParameters: parameters)
211 | else { fatalError() }
212 |
213 |
--------------------------------------------------------------------------------
/swift 4 demo/14.枚举.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //基本用法
8 | //最简单的定义
9 | //a.多行写法
10 | enum CompassPoint1{
11 | case north;
12 | case south;
13 | case east;
14 | case west;
15 | }
16 |
17 | //b.单行写法
18 | enum CompassPoint2 {
19 | case north,south,ease,west;
20 | }
21 |
22 | //提示 Swift 3.0 开始,定义枚举值,统一小写
23 |
24 | //指定枚举类型的原始数据类型
25 | enum Rank : Int {// Int 设置枚举值的类型
26 | // 定义枚举值设置值
27 | case ace;
28 | // 可以case后面一次定义多个枚举值
29 | case onr,two,three,four,five,six,seven,eight,nine,ten
30 | case jack,queen,king
31 |
32 | // 定义函数 如果多人合作的时候,可以使用这个让别人更加了解你定义的属性含义
33 | func simpleDescription() -> String {
34 | switch self {
35 | case .ace:
36 | return "ace1";
37 | case .jack:
38 | return "jack1";
39 | case .queen:
40 | return "queen1";
41 | case .king:
42 | return "king1";
43 | default:
44 | return String(self.rawValue);
45 | }
46 | }
47 | }
48 |
49 | //注意
50 | //枚举类型可以Int String等基本类型值,对象是不可以的,官方说必须是实现RawRepresentable协议的类型才可以
51 |
52 | //使用
53 | let ace = Rank.ace;
54 | let rank = Rank(rawValue : 4);
55 |
56 | //提示
57 |
58 | //第二种创建的枚举为可选类型,这样设计的原因是,你有可能创建一个nil对象,处于安全考虑它也必须是可选值,使用的时候要解包
59 |
60 | //给枚举设置原始值
61 | //enum CompassPoint3 {
62 | // case north = "1";
63 | // case south = "2";
64 | // case east = "3";
65 | // case west = "4";
66 | //}
67 | //提示:
68 |
69 | //你认为上面这样定义是否是正确的,答案是否定的,因为系统不知道你枚举的原始值是什么类型的,这里系统没有进行类型推断,所有我们要给枚举添加原始值之前,必须指定枚举原始值的类型,如下
70 | enum CompassPoint3 : Character {
71 | case north = "1";
72 | case south = "2";
73 | case east = "3";
74 | case west = "4";
75 | }
76 |
77 | let aceRawValue = ace.rawValue;
78 | //提示
79 | //1.如果你没有定义枚举的原始值类型,你不能通过上述方法获取原始值
80 | //2.输入定义的是数字类型 ,原始值对应的是数字本身,
81 | //3.如果定义的是字符串类型,但你没有給它赋值,这个时候,你获取的原始值就是你定义的名字
82 |
83 | //高级用法
84 | //提供值创建枚举
85 | enum ServerResponse {
86 | case result(String, String);
87 | case failure(String);
88 | }
89 |
90 | let success = ServerResponse.result("6:00 am", "8:09 pm");
91 | let failure = ServerResponse.failure("Out of cheese.");
92 |
93 | //提示
94 | //这个方式定义的枚举 不能使用 == 来做判断处理.我们应该使用模式匹配的方式处理,这种定义方式强调的是传值,如下
95 | switch success {
96 | case let .result(sunrise, sunset)://模式匹配
97 | print("Sunrise is at \(sunrise) and sunset is at \(sunset).")
98 | case let .failure(message):
99 | print("Failure...\(message)");
100 |
101 | }
102 |
103 | //或者
104 | switch success{
105 | case .result(let sunrise, let sunset): //模式匹配
106 | print("Sunrise is at \(sunrise) and sunset is at \(sunset).");
107 | case .failure(let message):
108 | print("Failure...\(message)");
109 | }
110 |
111 | //indirect使用方法
112 | //使用场景:
113 | //主要用于递归枚举,看下面例子,你应该很清楚怎么使用
114 |
115 | //a.使用方式1
116 | enum ArithmeticExpression {//递归枚举
117 | case number(Int);
118 | indirect case addition(ArithmeticExpression, ArithmeticExpression);
119 | indirect case multipliction(ArithmeticExpression,ArithmeticExpression);
120 | }
121 |
122 | let expression = ArithmeticExpression.addition(ArithmeticExpression.number(3), ArithmeticExpression.number(4));
123 |
124 | //b.使用方式2
125 | indirect enum ArithmeticExpression1 {
126 | case number(Int)
127 | case addition(ArithmeticExpression, ArithmeticExpression)
128 | case multiplication(ArithmeticExpression, ArithmeticExpression)
129 | }
130 |
131 | let expression1 = ArithmeticExpression.addition(ArithmeticExpression.number(3), ArithmeticExpression.number(4))
132 | //结论:
133 | //如果你定义的枚举是递归的形式,必须有关键字indirect 修饰,不然系统会编译报错哦!
134 |
135 | //.重新实现SWIFT标准库的可选类型(使用泛型技术)
136 | enum OptionalValue {
137 | case none;
138 | case some(Wrapped);
139 | }
140 | var possibleInteger : OptionalValue = .none;
141 | possibleInteger = .some(100);
142 |
143 | //枚举可以继承协议
144 | protocol Skill {
145 | mutating func modifyMusic(name : String);
146 | }
147 |
148 | enum Type : String,Skill{
149 | case name = "123";
150 | mutating internal func modifyMusic(name: String) {
151 | self = Type(rawValue : "123")!;
152 | }
153 | }
154 |
155 | //枚举不能包含存储属性,但是可以包含静态变量和计算属性
156 | enum SomeEnumeration : Int {
157 | case one = 2;
158 | case two = 345;
159 | // 静态变量
160 | static var storedTypeProperty = "Some value.";
161 | // 静态计算属性
162 | static var computedTypeProperty : Int {
163 | return 6;
164 | }
165 | // 计算属性
166 | var getRaw:Int{
167 | return self.rawValue;
168 | }
169 | }
170 |
171 | // 一般人都不知道的神级操作
172 | enum Locale {
173 | case none;
174 | case base;
175 | case language(String);
176 | }
177 |
178 | //我们创建一个变量
179 | let locale = Locale.language("english");
180 |
181 | //问题:我们怎么判断它是什么类型嗯?
182 | //if locale == Locale.base {
183 | //
184 | //}//错误的判断方式
185 |
186 | //我们可以提供下面的方式进行判断
187 | switch locale{
188 | case Locale.base : print(locale)
189 | case Locale.none : print(locale)
190 | case Locale.language("english"): print(locale)
191 | case Locale.language(let x):print(x)
192 | }
193 |
194 | //为了判断一个类型我们写这么一个判断是在是有点不雅,下面就体验一下关键字case 的神奇用法
195 |
196 | enum Locale1 {
197 | case none;
198 | case base;
199 | case language(String);
200 | // 判断是否是汉语
201 | var isChinese: Bool {
202 | if case .language("chinese") = self {
203 | return true;
204 | }
205 | return false;
206 | }
207 | // 是否是其他语言
208 | var isLanguage : Bool {
209 | if case .language = self {
210 | return true;
211 | }
212 | return false;
213 | }
214 |
215 | var isBase : Bool {
216 | if case .base = self {
217 | return true;
218 | }
219 | return false;
220 | }
221 |
222 | var isNone : Bool {
223 | if case .none = self {
224 | return true;
225 | }
226 | return false;
227 | }
228 |
229 | }
230 |
231 | //我们看下下如何
232 | let locale2 = Locale1.language("english");
233 | print(locale2.isChinese);
234 |
235 |
236 |
237 |
238 |
239 |
--------------------------------------------------------------------------------
/swift 4 demo/8对象和类.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 | //1.类的定义
7 | //2.属性定义
8 | //3.类的方法创建
9 | //4.对象方法定义
10 | //5.初始化
11 | //6.类的释放
12 | //7.给类添加协议
13 | //8.继承
14 | //9.重写
15 |
16 | //1 如何定义一个类
17 | class Shape {
18 | var numberOfSides = 0 //属性
19 | //对象方法
20 | func simpleDescription() -> String {
21 | return "A shape with \(numberOfSides) sides.";
22 | }
23 | //类方法
24 | class func shapeDescription() -> String {
25 | return "我是一个定义形状的类";
26 | }
27 | }
28 |
29 | Shape.shapeDescription();
30 |
31 | //初始化
32 | class NameShape {
33 | var numberOfSides : Int = 0;
34 | var name : String = ""
35 | //注意初始化前面没有func
36 | init(name : String) {
37 | self.name = name;
38 | print(self.name);
39 | }
40 | }
41 |
42 | NameShape(name: "你好");
43 |
44 | //继承
45 | class NameShape2 {
46 | var name : String;
47 | var numberOfSide : Int?
48 | init(name : String) {
49 | self.name = name;
50 | }
51 | }
52 |
53 | class Square : NameShape2 {
54 | var sideLength : Double
55 | init(sideLength : Double, name : String) {
56 | self.sideLength = sideLength;
57 | super.init(name: name);
58 | numberOfSide = 4;
59 | }
60 | }
61 |
62 | //计算属性setter 和 getter
63 | class Circle {
64 | var area : Double = 0.0;
65 | var r : Double {
66 | set{
67 | area = newValue * newValue * 3.1415926;
68 | }
69 | get{
70 | return sqrt(area / 3.14158926);
71 | }
72 | }
73 | }
74 |
75 | //监控属性willSet和didSet,顾名思义,就是用来检测属性值的变化.
76 | class Circle2 {
77 | // 监控面积的变化,修改半径
78 | var r: Double = 0.0;
79 | var area: Double = 0.0{
80 |
81 |
82 | willSet{
83 | r = sqrt(newValue / 3.1415926);
84 | print(r);
85 | }
86 |
87 | didSet{
88 | print(r);
89 | }
90 |
91 | }
92 |
93 | // var r: Double = 0.0 {
94 | // willSet{
95 | // area = newValue * newValue * 3.1415926;
96 | // print(area);
97 | // }
98 | // }
99 |
100 | }
101 | Circle2().area;
102 |
103 | //运行会报错,因为循环检测了,这个编译不会出错的,所以不用在两个基本类型之间相互检测,两个对象之间是可以的
104 |
105 |
106 | //添加协议
107 |
108 | // 协议1
109 | protocol LoveMusic{
110 | func songMusic();
111 | }
112 |
113 | //协议2
114 | protocol Draw{
115 | func draw();
116 | }
117 |
118 | //给类添加协议
119 | class Student : LoveMusic,Draw{
120 | var name = "小明";
121 | var music : String?;
122 |
123 | func addMusic(name : String) {
124 | music = name;
125 | }
126 |
127 | func draw() {
128 | print("会画画");
129 | }
130 |
131 | func songMusic() {
132 | print("会唱歌");
133 | }
134 | }
135 |
136 | //重写
137 | //1.重写计算属性
138 | //2.重写对象方法
139 | //3.重写类方法
140 | //4.重写初始化方法
141 | //5.重写存储属性(只能扩展检测,不能重写值)
142 |
143 | //定义父类
144 | class Person {
145 | var name : String
146 | var rename : String {
147 | return self.name;
148 | }
149 | init(_ name : String) {
150 | self.name = name;
151 | }
152 | func describe() -> String {
153 | return self.name;
154 | }
155 | class func describeClass() -> String{
156 | return "这是一个描述的类";
157 | }
158 |
159 | }
160 |
161 | //重写父类
162 | class Man:Person{
163 | var score : Double;
164 | // 重写计算属性
165 | override var rename: String{
166 | set{
167 | self.name = newValue;
168 | }
169 | get{
170 | return self.name;
171 | }
172 | }
173 | //重写初始化
174 | override init(_ name: String) {
175 | self.score = 0.0;
176 | super.init(name);
177 | }
178 | //重写对象方法
179 | override func describe() -> String {
180 | return self.name + "\(self.score)";
181 | }
182 | //重写类方法
183 | override class func describeClass() -> String {
184 | return "我是描述男人的类";
185 | }
186 |
187 | }
188 |
189 | class Car1 {
190 | var name : String? = "奔驰";
191 | var currentSpeed : Double = 0.0;
192 | var descirption : Double = 0.0;
193 | var myName : String? = "250";
194 | }
195 |
196 |
197 | //重写存储属性列子,重写后,它的初始化的值依然是父类的初始化的值
198 | class AutomaticCar : Car1 {
199 | override var currentSpeed: Double{
200 | didSet{
201 | descirption = Double(currentSpeed / 10.0);
202 | }
203 | }
204 | override var myName: String? {
205 | didSet{
206 |
207 | }
208 | }
209 | }
210 | //重写计算属性例子
211 | //重写下标方法
212 |
213 | //重写计算属性的时候,只能增加功能,不能减少功能,比如父类的计算属性为(set get) 重写的话,只能全部重写,但是如果父类只有get方法,可以给子类增加set功能
214 | //重写监测属性,不能有初始化的值
215 |
216 | //问题:怎么才能不让重写计算属性,下标,方法,或者不让继承.
217 | //使用关键字final
218 |
219 | //类型检测
220 | //a.is的使用
221 | class Person2 {
222 |
223 | }
224 |
225 | class Teacher : Person2 {
226 | var name : String
227 | init(name : String) {
228 | self.name = name;
229 | }
230 | }
231 |
232 | let teacher = Teacher(name:"酷走天涯");
233 | var persons:[Any] = [];
234 | persons.append(teacher);
235 | let person = Person2();
236 | persons.append(person);
237 |
238 | for person in persons {
239 | // 检测类型
240 | if person is Teacher {
241 | print("我是Teacher");
242 | }
243 |
244 | if person is Person2 {
245 | print("我是Person");
246 | }
247 | }
248 |
249 | //b.as? 的使用
250 | for person in persons {
251 | // 检测类型
252 | if let p = person as? Teacher {
253 | print("我是Teacher");
254 | }
255 | if let p = person as? Person2 {
256 | print("我是Person");
257 | }
258 |
259 | }
260 |
261 | //c.as!的使用
262 | let teacher1 : Any = Teacher(name: "酷走天涯");
263 | let p1 = teacher as! Person2;
264 | print("输出\(p1)");
265 | //注意:
266 | //一旦teacher不是Person类型或者它的子类,系统就会报错.
267 |
268 | //建议使用
269 | let p = teacher as? Person2;
270 |
271 | //d.更多应用请看下面代码
272 | //类
273 | class Movie {
274 | var name = "我的世界";
275 | }
276 | //闭包 前面是参数String方法返回值是String in 后面是实现
277 | var callBack = {(str:String)->(String) in
278 | return str;
279 | };
280 |
281 |
282 |
283 | let things : [Any] = [0,0.0,1,-1.0,1.0,3.0,"str",(1.0,3.0),Movie(),callBack];
284 | for thing in things {
285 | switch thing {
286 | case 0 as Int://如果值等于0 请检测是不是Int
287 | print("zero as an Int");
288 | case 0 as Double://和上面类型
289 | print("zero as a Double");
290 | case let someInt as Int:// 模式匹配上面的值,检测是不是Int
291 | print("an integer value of \(someInt)");
292 | case let someDouble as Double where someDouble > 0:// 模式匹配上面的值 检测是不是double 并且判断它是不是大于0
293 | print("a positive double value of \(someDouble)");
294 | case is Double: // 检测是不是double类型
295 | print("some other double value that I don't want to print");
296 | case let someString as String: // 模式匹配是不是String类型
297 | print("a string value of \"\(someString)");
298 | case let (x,y) as (Double,Double): // 模式匹配是不是元祖类型,并且检测数据类型
299 | print("an (x,y) point at \(x),\(y)");
300 | case let movie as Movie:
301 | print("a movie called \(movie.name)");
302 | case let stringConverter as (String) -> String:
303 | print(stringConverter("Michael"));
304 |
305 | default:
306 | print("something else");
307 | }
308 | }
309 |
310 |
311 | //注意事项
312 | //
313 | //1.对象中的所有变量或者常量在定义时如果不初始化,在对象初始化的时候,必须初始化,这个是swift安全性考虑,可选类型没有强制性要求初始化,因为系统默认给可选类型初始化为nil
314 | //
315 | //2.初始化顺序
316 | //
317 | //1.首先在自己的初始化方法中先给自己的属性初始化
318 | //2.然后调用父类的初始化
319 | //3.最后修改父类的属性
320 |
321 |
322 |
323 |
324 |
325 |
326 |
--------------------------------------------------------------------------------
/swift 4 demo/9.属性.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //存储属性
8 | //a.注意
9 | //1.类和结构体中不能使用枚举
10 | //2.不能被子类重写,但可以在子类中给它添加监测功能willSet{} didSet{}
11 | //b.定义
12 |
13 | struct Range { // 结构体定义存储属性
14 | let first : Int;
15 | var length : Int;
16 | }
17 |
18 | class Student { // 类定义存储属性
19 | var name : String!;
20 | var score : String!;
21 | }
22 |
23 | //计算属性
24 |
25 | //a.能在哪里使用
26 | //类,结构体,枚举
27 | //b.定义
28 | //1.结构体使用
29 | struct Range1 {
30 | var first : Int;
31 | var length : Int;
32 | var myFirst:Int {//setter ,getter的计算属性
33 | set {
34 | first = newValue;
35 | }
36 | get {
37 | return first;
38 | }
39 | }
40 |
41 | var myLength : Int {//只有get方法计算属性
42 | get{
43 | return length;
44 | }
45 | }
46 | }
47 |
48 | //2.没居中使用
49 | enum Person : String {
50 | case Man = "男";
51 | case Women = "女";
52 |
53 | //get
54 | var name : String {
55 | return self.rawValue;
56 | }
57 |
58 | //setter getter
59 | var rename : String {
60 | set(newName){
61 | self = Person(rawValue: newName)!;
62 | }
63 | get{
64 | return self.rawValue;
65 | }
66 | }
67 | }
68 |
69 | var person = Person.Man;
70 | print(person.name);
71 | person.rename = "女";
72 | print(person.rawValue);
73 |
74 | //结果:
75 | //男
76 | //女
77 |
78 | //提示:
79 | //1.如果计算属性只有getter 方法,那么这个get 可以被省略
80 | //2.setter 可以设置新值名字,如果没有设置默认为newValue
81 | //3.let不能修饰计算属性
82 |
83 | //监测属性
84 | //例子1:
85 | class Circle{
86 | var area:Double = 0.0;
87 | var r:Double = 0.0{
88 | willSet{
89 | area = Double.pi * newValue * newValue;
90 | }
91 | }
92 | }
93 | let circle = Circle();
94 | circle.r = 10.0;
95 | print(circle.area);
96 | //运行结果
97 | //0.0
98 | //314.159265358979
99 |
100 | //例子2:
101 | class Circle1{
102 | lazy var area : Double = 0.0;
103 | var r:Double = 0.0 {
104 | willSet{
105 | print("有新值");
106 | area = Double.pi*newValue*newValue;
107 | }
108 | }
109 | }
110 |
111 | let circle1 = Circle1();
112 | func calcalate(r:inout Double){
113 | print("函数执行开始");
114 | r = 2.0;
115 | print("函数执行结束");
116 | }
117 | calcalate(r: &circle1.r);
118 | //运行结果
119 | //函数执行开始
120 | //函数执行结束
121 | //有新值
122 |
123 | //提示:
124 | //1.监测属性,其实是给存储属性上添加的一种监测功能,willSet 监测新值,didSet 监测旧值
125 | //2.如果函数的参数是inout 修饰的,你如果将监测的属性传入这个函数的时候,此时会将属性的值拷贝一份,在函数结束的时候,将值重新付给属性,所以函数执行完毕后,会触发监测函数
126 |
127 | //Lazy 关键字使用(只用于存储属性)
128 | //使用时Lazy必须注意
129 | //1.只能用于存储属性
130 | //2.修饰的属性必须有初始化
131 | //3.在结构体中使用Lazy修饰属性,在访问的方法前必须加mutating 修饰
132 | //4.不能用于全局属性或者静态变量
133 | //5.存储属性被lazy修饰,只被初始化一次,在多线程访问时,不需要使用lazy标记
134 |
135 | //例子:
136 | struct Animals{
137 | lazy var name = "动物";
138 | mutating func calculate(){ // 调用了lazy 必须加mutating
139 | let name = self.name;
140 | }
141 |
142 | var myName : String { //只有get方法计算属性
143 | mutating get{ // 调用了lazy 必须加mutating
144 | return name;
145 | }
146 | }
147 | }
148 |
149 | //static 和 class 关键字的用法
150 | //相同点:
151 | //1.可以修饰方法,static修饰的方法叫做静态方法,class修饰的叫做类方法
152 | //2.都可以修饰计算属性.
153 |
154 | //不同点
155 | //class不能修饰存储属性
156 | //class修饰的计算属性可以被重写,static修饰的不能被重写
157 | //static可以修饰存储属性,static修饰的存储属性称为静态变量(常量);
158 | //static修饰的静态方法不能被重写,class修饰的类方法可以被重写.
159 | //class修饰的类方法被重写时,可以使用static让方法变为静态方法
160 | //class修饰的计算属性被重写时,可以使用static让其变为静态属性,但它的子类就不能重写了
161 | //class只能在类中使用,但是static可以在类,结构体,或者枚举中使用.
162 | //下面是个例子:
163 | class Person1{
164 | static var describe: String = "这是一个类";
165 |
166 | class var score : Int{
167 | return 0;
168 | }
169 | // class 修饰的类方法可以被子类重写,static修饰的静态方法不能被重写
170 | class func getScore() -> Int{
171 | return score;
172 | }
173 | }
174 |
175 | class Man : Person1 {
176 | //重新计算属性 可以使用static 来重写,但static重写后,就不能被它的子类再次重写了
177 | class override var score : Int {
178 | return 1;
179 | }
180 |
181 | //重写类方法时可以使用static 让其变成静态方法
182 | static override func getScore() -> Int {
183 | return score;
184 | }
185 |
186 |
187 | }
188 | //下面是个例子:
189 | class Person3{
190 | static var descibe:String = " 这是一个人类";
191 | class var score : Int {
192 | return 0;
193 | }
194 | //class 修饰的类方法可以被子类重写,static修饰的静态方法不能被重写
195 | class func getScore() -> Int{
196 | return score;
197 | }
198 | }
199 |
200 | class Man3 : Person3 {
201 | //class 修饰的类方法可以被子类重写,static修饰的静态方法不能被重写
202 | class override var score : Int {
203 | return 1;
204 | }
205 |
206 | //重写类方法时可以使用static让其变成静态方法
207 | static override func getScore() -> Int {
208 | return score;
209 | }
210 | }
211 |
212 | //了解这些东西
213 | //1.参数是闭包执行的结果和参数是闭包
214 | class Person4{
215 | var name : String;
216 | var score : Int;
217 | init(name: String,score : Int) {
218 | self.name = name;
219 | self.score = score;
220 | }
221 | let descirbe: String = {
222 | print("闭包已经执行");
223 | return "我在这个时间\(getTime())被创建了";
224 | }()
225 |
226 | }
227 | //获取时间
228 | func getTime() -> Date {
229 | return Date();
230 | }
231 |
232 | let p = Person4(name:"小星星",score:3);
233 |
234 | //运行结果:
235 | //闭包已经执行
236 | //Program ended with exit code: 0
237 |
238 |
239 | //分析:
240 | //属性describe 是一个String 类型 ,获取闭包函数执行的结果
241 | //其实和下面的代码是一个意思
242 |
243 | class Person6{
244 | var name : String;
245 | var score : Int;
246 | init(name : String,score : Int) {
247 | self.name = name;
248 | self.score = score;
249 | }
250 |
251 | let descirbe : String = myDescibe1();
252 |
253 | }
254 |
255 | // 获取时间
256 | func getTime1() -> Date{
257 | return Date();
258 | }
259 |
260 | func myDescibe1() -> String{
261 | return "我在\(getTime1())被创建了";
262 | }
263 |
264 | let p1 = Person6(name:"小星星", score: 3);
265 |
266 | //注意:
267 | //
268 | //属性 = 号后面的函数不能定义在类内,因为初始化未完成之前是不能调用对象方法的
269 |
270 | //接下来我们看参数是闭包
271 | class Person7{
272 | var name : String;
273 | var score : Int;
274 | init(name : String,score : Int) {
275 | self.name = name;
276 | self.score = score;
277 | }
278 | //注意和上面的区别
279 | let descibe: () -> String = {
280 | print("闭包执行了");
281 | return "我在\(getTime2())被创建了";
282 | }
283 | }
284 |
285 | //获取时间
286 | func getTime2() -> Date {
287 | return Date();
288 | }
289 |
290 | func myDescribe() -> String {
291 | return "我在\(getTime2())被创建了";
292 |
293 | }
294 |
295 | let p7 = Person7(name:"小星星",score:3);
296 | //啥都没有过
297 | //属性describe 是一个()->String 类型,后面跟的是他的值,它的值刚好是一个闭包类型 ,但是没有执行,我们让它执行一下
298 | let pd = p7.descibe();
299 | //运行结果
300 | //闭包执行了
301 | //其实我们还可以使用下面的等价代码
302 | class Person8 {
303 | var name : String;
304 | var score : Int;
305 | init(name : String, score : Int) {
306 | self.name = name;
307 | self.score = score;
308 | }
309 | let describe: ()->String = myDescribe3;//赋值个闭包
310 | }
311 |
312 | //获取时间
313 | func getTime4() -> Date{
314 | return Date();
315 | }
316 |
317 | func myDescribe3() -> String{
318 | return "我在\(getTime())被创建了";
319 | }
320 | let p8 = Person8(name: "小星星",score:3);
321 | let pd8 = p.descirbe;
322 |
323 | //搞清楚上面两者的区别
324 |
325 | //需求: 有个自定义类,类中有一个数组,数组中包含20位斐波那契数列
326 | //F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(
327 | class Calculate2 {
328 | var fibo : [Int] = {
329 | //定义一个数组
330 | var nums : [Int] = [];
331 | func Func(n:Int) -> Int{
332 | if n < 2{
333 | return n;
334 | }else{
335 | return Func(n: n-1) + Func(n: n-2);
336 | }
337 | }
338 |
339 | for i in 0..<20{
340 | nums.append(Func(n: i));
341 | }
342 | return nums;
343 | }();
344 | }
345 |
346 | let c = Calculate2();
347 | print(c.fibo);
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
--------------------------------------------------------------------------------
/swift 4 demo/20.自动引用计数(strong,weak,unowned).playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //自动引用计数的原理
8 |
9 | //1 栈区(stack)*先进后出* 由编译器自动分配并释放,一般存放函数的参数值,局部变量等
10 | //2 堆区(heap) *先进先出* 由程序员分配和释放,如果程序员不释放,程序结束时,可能会由操作系统回收
11 | //3 寄存器区:用来保存栈顶指针和指令指针
12 | //4 全局区(静态区)全局变量和静态变量的存储是放在一起的,初始化的全局变量和静态变量存放在一块区域,未初始化的全局变量和静态变量在相邻的另一块区域,程序结束由系统释放。
13 | //5 文字常量区:存放常量字符,横须结束后由系统释放
14 | //6 程序代码区:存放函数的二进制代码
15 |
16 |
17 | //1.每次创建一个类的新实例时,都会分配一个内存块来存储有关该实例的信息。此内存保存实例的类型的信息,以及与该实例关联的任何存储属性的值
18 | //2.当一个实例不再需要时,ARC释放由该实例使用的内存,以便内存可以用于其他用途
19 | //3.ARC 释放对象后,它将不能再继续访问对象的属性,或者调用对象方法,如果你依然继续访问对象,App将会崩溃
20 | //4.为了确保对象使用时, 不被释放,ARC 跟踪属性,变量和常量,只要有一个对象引用存在,在对象不会被释放
21 | //5.当您将一个类实例分配给属性、常量或变量时,属性、常量或变量对实例会有一个强引用,确保对象不会被释放
22 |
23 | //深讨Strong,weak和unowned的区别
24 | //我们先创建一个对象
25 | class Person {
26 | var name : String;
27 | init(name : String) {
28 | self.name = name;
29 | }
30 | deinit {
31 | print("对象释放了,name的值为:\(name)")
32 | }
33 | }
34 |
35 | //强引用
36 | //创建一个person实例对象,然后对其进行强引用
37 | var reference1 : Person? = Person(name: "求星星");
38 |
39 | //我们不在引用对象
40 | reference1 = nil;
41 | //结果
42 | //对象释放了,name的值为:酷走天涯
43 |
44 | //弱引用
45 | //我们使用weak引用对象
46 | weak var reference2 : Person? = Person(name: "求星星");
47 | //结果
48 | //对象释放了,name的值为:酷走天涯
49 |
50 | //我们写成不可选
51 | //weak var reference3 : Person = Person(name: "求星星");
52 | //结果报错
53 | //should have optional type 'Person?'
54 | //weak var reference3 : Person = Person(name: "求星星");
55 | //原因
56 | //weak 修饰的变量或者常量必须为可选值 ? 或者 !
57 |
58 | //使用unowned引用对象
59 | //unowned var reference4 : Person? = Person(name: "求星星");
60 | //报错
61 | //error: 20.自动引用计数(strong,weak,unowned).playground:54:13: error: 'unowned' may only be applied to class and class-bound protocol types, not 'Person!'
62 |
63 | //unowned var reference5 : Person! = Person(name: "求星星");
64 | //报错
65 | //error: 20.自动引用计数(strong,weak,unowned).playground:54:13: error: 'unowned' may only be applied to class and class-bound protocol types, not 'Person!'
66 | //unowned var reference5 : Person! = Person(name: "求星星");
67 |
68 | //unowned var reference7 : Person = Person(name: "酷走天涯")
69 | //不知道为何错误 正常是对的
70 |
71 | //原因
72 | //不能修饰可选类型
73 |
74 | //总结
75 | //1.强引用的对象,不会被释放
76 | //2.weak 或者unowned 引用的对象,当没有强引用的时候,会被立即释放
77 | //3.weak 修饰的变量和常量必须为可选类型,但是unowned刚好和其相反必须为非可选类型
78 |
79 | //验证结论
80 |
81 | class School {
82 | var name : String;
83 | init(name : String) {
84 | self.name = name;
85 | }
86 |
87 | deinit {
88 | print("学校对象释放了,name的值为:\(name)");
89 | }
90 | }
91 |
92 | //例子1
93 | var text1 : School = School(name: "北京");//非可选对象
94 | weak var text2 = text1;
95 | unowned var text3 = text1;
96 | print(text2);
97 | print(text3);
98 | //结果
99 | //Optional(__lldb_expr_1.School) //转成可选对象了
100 | //__lldb_expr_1.School//非可选对象
101 |
102 | //例子2
103 | var bText1 : School = School(name: "上海");//非可选值对象
104 | weak var bText2 = bText1; //这时候已经是可选对象
105 | //unowned var bText3 = bText2;//错误了
106 | //错误原因
107 | //weak 修饰的变量bText2 虽然没有指明变量类型,但是swift会推断出来它的类型为School?,
108 | //这个时候我们把一个School? 的类型付给unowned修饰的变量,系统出现错误
109 |
110 | //例子3
111 | var cText1 : School = School(name: "广州");//非可选值对象
112 | weak var cText2 = cText1;
113 | unowned var cText3 = cText2!;
114 |
115 |
116 | //为什么要使用weak和unowned
117 | //定义两个类Student和School1,Student有一个属性school,school也有一个属性student,我们让其互相引用
118 | //学生类
119 | class Student {
120 | var school : School1?;
121 | deinit {
122 | print("学生是释放了");
123 | }
124 | }
125 |
126 | //学校类
127 | class School1 {
128 | var student : Student? // 强引用
129 | deinit {
130 | print("学校释放了");
131 | }
132 | }
133 |
134 | var school : School1? = School1();//创建实例对象School1 属性student(可选)属性为nil
135 | var student : Student? = Student();//创建实例对象Student 属性school(可选)属性为nil
136 | //互相持有
137 | school?.student = student;
138 | student?.school = school;
139 |
140 | //在都设置为nil
141 | school = nil;
142 | student = nil;
143 |
144 | //结果
145 | //结果都没有释放
146 |
147 | //原因分析:
148 | //school 要释放必须先释放它的属性Student ,系统就去释放Student的内存空间,发现他有一个属性叫school 然后又去释放school,就这样构成死循环,谁都无法释放
149 |
150 | //遇到这类问题,原因就是*相互强引用*了,接下来,我们使用将student的属性school 使用weak修饰
151 | // 学生类
152 | class Student2 {
153 | weak var school : School2?
154 | deinit {
155 | print("学生对象释放了");
156 | }
157 | }
158 |
159 | //学校类
160 | class School2 {
161 | var student : Student2?;
162 | deinit {
163 | print("学校对象释放了");
164 | }
165 | }
166 | var school2 : School2? = School2();//创建实例对象School1 属性student(可选)属性为nil
167 | var student2 : Student2? = Student2();//创建实例对象Student 属性school(可选)属
168 | //互相持有
169 | school2?.student = student2;
170 | student2?.school = school2;
171 |
172 | //在都设置为nil
173 | school2 = nil;
174 | //结果学校释放了
175 | student2 = nil;
176 | //结果学生释放了
177 |
178 | //原理:就是找到按顺序类进属性 属性对应的类的属性弱引用释放 ,具体自己理解😀
179 |
180 | //选择unowned和weak问题
181 | //例子
182 | //使用unowned
183 |
184 | //学生类
185 | class Student3 {
186 | unowned var school : School3;
187 | init(school : School3) {
188 | self.school = school;
189 | }
190 | deinit {
191 | print("text3学生对象释放");
192 | }
193 | func describe() {
194 | print("学生在\(school.describe())")
195 | }
196 | }
197 |
198 | //学校类
199 | class School3 {
200 | var student : Student3? // 强引用
201 | deinit {
202 | print("text3学校对象释放了");
203 | }
204 | func describe() -> String {
205 | return "学校";
206 | }
207 | }
208 |
209 | var school3 : School3? = School3()
210 | var student3 : Student3? = Student3(school:school3!)
211 | school3!.student = student3
212 | // 学校不用了,把学校释放掉
213 | school3 = nil
214 | //student3?.describe()//崩溃
215 |
216 | //运行:
217 | //崩溃
218 |
219 | //原因:
220 | //释放掉school对象,然后在student的方法中调用了school的方法,方法已经不存在了,所以崩溃了
221 | //print("学生在\(school.describe())")这个就是school方法
222 |
223 | //使用weak
224 | //学生类
225 | class Student4 {
226 | weak var school : School4?;
227 | deinit {
228 | print("text4学生对象释放");
229 | }
230 |
231 | func describe() {
232 | if let school = self.school{
233 | print("学生在\(school.describe())上学");
234 | }
235 | }
236 | }
237 | //学校类
238 | class School4 {
239 | var student : Student4? // 强引用
240 | deinit {
241 | print("text4学校对象释放了");
242 | }
243 | func describe() -> String {
244 | return "上海学校";
245 | }
246 | }
247 | //互相持有
248 | var school4 : School4? = School4()
249 | var student4 : Student4? = Student4()
250 | school4!.student = student4
251 | student4!.school = school4
252 |
253 | //运行下面的代码
254 | school4 = nil;
255 | print(student4?.describe());
256 |
257 | //结果
258 | //学校对象释放了
259 |
260 | //分析:
261 | //由于school4被弱引用,计数器减一,school4对象就被释放了,
262 | //所以我们在调用的时候进行检测,如果对象存在再去执行方法,这样就避免了此类错误
263 |
264 | //总结
265 | //使用unowned 修饰属性时,必须保证自己的实体独享要比引用的对象先释放
266 | //如果循环引用中,弱引用的对象必须为非可选类型,这个时候,就可以考虑使用unowned
267 |
268 | //实例分析
269 | class HTMLElement {
270 | let name : String;
271 | let text : String?;
272 | // lazy var asHTML : () -> String = { //这里注意,如果要在存储属性的闭包中访问自己的属性,必须加Lazy
273 | // if let text = self.text {
274 | // return "<\(self.name)>\(text)\(self.name)>";
275 | // } else {
276 | // return "<\(self.name) />"
277 | // }
278 | // }
279 | //修改部分代码如下
280 | lazy var asHTML: () -> String = {
281 | [unowned self] in
282 | if let text = self.text {
283 | return "<\(self.name)>\(text)\(self.name)>"
284 | } else {
285 | return "<\(self.name) />"
286 | }
287 | }
288 |
289 | init(name : String,text : String? = nil) {
290 | self.name = name;
291 | self.text = text;
292 | }
293 |
294 | deinit {
295 | print("\(name) is being deinitialized");
296 | }
297 | }
298 |
299 | var x:HTMLElement? = HTMLElement(name: "title");
300 | x = nil;
301 | //分析:
302 | //眨眼一看,释放了,
303 | //为什么被释放了,因为我们使用lazy关键字,asHTML 闭包没有被创建,
304 | //这里注意,如果要在存储属性的闭包中访问自己的属性,必须加Lazy
305 |
306 | var x1:HTMLElement? = HTMLElement(name: "title");
307 | print(x1?.asHTML());
308 | x1 = nil;//没有释放
309 |
310 | //分析原因:
311 | //释放对象,首先释放属性,释放name发现name被闭包引用了,然后去释放闭包,发现释放self.name ,构成了死循环
312 |
313 | //分析:
314 | //释放name的时候, 是被弱引用的,引用计数没有加1,不用考虑,直接释放自己,释放asHTML 时发现,name 已经被释放了,
315 |
316 |
317 |
--------------------------------------------------------------------------------
/swift 4 demo/7流控制.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //1 if...else
6 | if true {
7 | //code
8 | }else{
9 | //code
10 | }
11 |
12 | let (x,y) = (1,2);
13 | //if x = y {//这是一个错误用法
14 | //
15 | //}
16 |
17 | //判断的条件必须为true或者false不能是1,2或者存在对象,不然系统会编译错误,这点是和OC是有区别的.
18 |
19 |
20 | //2.三目运算符(?)
21 | let contentHeight = 40;
22 | let hasHeader = true;
23 | let rowHeight = contentHeight + (hasHeader ? 50 : 20);
24 |
25 | //3.?? 的使用
26 | //先看下面的例子
27 | let textA : Int? = nil;
28 | let textB = 4;
29 | //如果a不为nil则对a进行解包,赋值将b的值付给c
30 | let textC = textA != nil ? textA! : textB ;
31 | print(textA);
32 | print(textB);
33 | print(textC);
34 |
35 | let textD = textA ?? textB // 如果a为nil则使用b的值替换a的值,如果a不为nil,则对a解包然后将值付给a
36 | print(textA);
37 | print(textB);
38 | print(textD);
39 |
40 |
41 | //我们在看一个例子
42 | let textA1 : Int??? = 3;
43 | let textA2 = 4;
44 | let textA3 = textA1 ?? textA2;
45 | print(textA1);
46 | print(textA3);
47 |
48 | //答案: Optional(Optional(Optional(3))) Optional(Optional(3))
49 | //首先a,有三个包Optional(Optional(Optional(3))),当我们解掉第一次包后,发现值为nil,此时,我们需要将b的值替换给a,Optional(Optional(3))
50 |
51 | //4.范围操作符 ... ..<
52 | for index in 1..<5 {
53 | print("\(index) times 5 is \(index * 5)");
54 | }
55 |
56 | //多种逻辑操作符的结合
57 | let enterCode = true;
58 | let passedCode = true;
59 | let hasDoorKey = true;
60 | if enterCode && passedCode || hasDoorKey {
61 | print("Welcome!");
62 | } else {
63 | print("ACCESS DENIED");
64 | }
65 |
66 | //for ... in
67 | //遍历数组
68 | let names = ["Anna","Alex","Brian","Jack"];
69 | for name in names {
70 | print("Hello, \(name)!");
71 | }
72 |
73 | //遍历字典
74 | let numberOfLegs = ["spider": 8, "ant": 6,"cat": 4];
75 | for (animalName,legCount) in numberOfLegs {
76 | print("\(animalName)s have \(legCount) legs");
77 | }
78 |
79 | //while
80 | var i = 100;
81 | while i > 0 {
82 | i -= 1;
83 | }
84 |
85 | //repeat ... while
86 | var j = 10;
87 | repeat {
88 | j -= 1;
89 | print(j);
90 | } while j >= 0;
91 |
92 | //switch
93 | //需求1:根据输入的成绩判断学生是否及格
94 | //a.间隔法
95 | enum TextResult{
96 | case Pass;
97 | case NoPass;
98 | }
99 | //b.条件判断法
100 | func inputStudentScore(score:Float) -> TextResult {
101 | switch score {
102 | case 0 ..< 59 :
103 | return .NoPass;
104 |
105 | default:
106 | return .Pass;
107 | }
108 | }
109 | //需求二: 输入一个顶点判断是否在x轴上,或者y轴上,或则既不在x轴,也不再y轴上
110 | //c.元祖法
111 | func inputPoint(point : (Float,Float)){
112 | switch point {
113 | case (_,0):
114 | print("在x轴上");
115 | case (0,_):
116 | print("在y轴上");
117 | case (_,_):
118 | print("既不在x轴,也不在y轴上");
119 |
120 | }
121 | }
122 |
123 | //d.值绑定法
124 | func inputPoint2(point : (Float,Float)) {
125 | switch point {
126 | case (let x, 0):
127 | print("在x轴上\(x)");
128 | case (0, let y):
129 | print("在x轴上\(y)");
130 | case let(x,y):
131 | print("即不再x轴,也不在y轴上\(x),\(y)");
132 | }
133 | }
134 | //需求:判断顶点是否在x,y轴上
135 |
136 | //e.混合法
137 | func inputPoint3(point : (Float,Float)) {
138 | switch point {
139 | case (let x , 0),(0, let x):// 注意必须每种模式类型相同
140 | print("在x轴或者y轴上\(x)");
141 | case let (x,y):
142 | print("即不在x轴,也不再y轴上\(x),\(y)");
143 | }
144 | }
145 | //控制转移声明
146 | //a.continue
147 | let puzzleInput = "great minds think alike";
148 | var puzzleOutput = "";
149 | for character in puzzleInput {
150 | switch character {
151 | case "a","e","i","o","u"," ":
152 | continue;
153 | default:
154 | puzzleOutput.append(character);
155 | }
156 | }
157 | //b. break //跳出
158 | let numberSymbol : Character = "三" // Chinese symbol for the number 3;
159 | var possibleIntegerValue : Int?
160 | switch numberSymbol {
161 | case "1","一":
162 | possibleIntegerValue = 1;
163 | case "2","二":
164 | possibleIntegerValue = 2;
165 | case "3","三":
166 | possibleIntegerValue = 3;
167 | case "4","四":
168 | possibleIntegerValue = 4;
169 | default:
170 | break;
171 | }
172 | //c. fallthrough //穿透
173 | let integerToDescribe = 5;
174 | var description = "The number \(integerToDescribe)"
175 | switch integerToDescribe {
176 | case 2,3,5,7,11,13,17,19:
177 | description += " a prime number, and also";
178 | fallthrough;//打穿继续向下执行
179 | default:
180 | description += " an integer.";
181 | }
182 | print(description);
183 |
184 | //d.return
185 | //需求: 在一个数组中查找是否存在一个整数,如果找到返回true没有返回false;
186 | let nums = [1,2,3,4,5,6,7,9,10];
187 | func findNum(num : Int) -> Bool{
188 | for number in nums {
189 | if num == number {
190 | return true;
191 | }
192 | }
193 | return false;
194 | }
195 |
196 | //e. throw
197 | //需求:让一个a数字从0开始,每次增加一个随机数,直到数字a=1000结束循环,如果大于1000则继续从0开始循环增加
198 | var start : Int = 0;
199 | var final = 1000;
200 | whileLabel : while start != final {
201 | start += Int(arc4random_uniform(100)) // 随机增加一个数字
202 | switch start {
203 | case 1000:
204 | break whileLabel;
205 | case let x where x > 1000:
206 | start = 0;
207 | continue whileLabel;
208 | default:
209 | break;
210 | }
211 | }
212 |
213 | //guard ...else
214 |
215 | //需求输入学生姓名,查询学生成绩
216 | //a.原始写法(if);
217 | let studentScoreDic = ["小明":30.0,"小摩纳哥":99.0];
218 | func getScoreByName(name : String!) -> Double {//先检测姓名是否为空
219 | if let n = name {
220 | if let score = studentScoreDic[n] {
221 | return score;
222 | }
223 | }
224 | return 0.0;
225 | }
226 |
227 | //b. guard...else
228 | let studentScoreDic2 = ["小明":30.0,"小摩纳哥":99.0];
229 | func getScoreByName2(name : String!) -> Double{
230 | //检测姓名是否为空
231 | guard let n = name else{
232 | return 0.0;
233 | }
234 | //检测学生是否存在
235 | guard let score = studentScoreDic2[n] else {
236 | return 0.0;
237 | }
238 | return score;
239 | }
240 | print(getScoreByName2(name: "小明"));
241 |
242 | //c.使用??简化写法
243 | let studentScoreDic3 = ["小明":30.0,"小摩纳哥":99.0];
244 | func getScoreByName3(name:String!) -> Double{
245 | guard let n = name else {
246 | return 0.0;
247 | }
248 | return studentScoreDic3[n] ?? 0.0;
249 | }
250 | print(getScoreByName3(name: "小明"));
251 |
252 | //d.多条件简化法-guard版
253 | let studentScoreDic4 = ["小明":30.0,"小摩纳哥":99.0];
254 | func getScoreByName4(name:String!) -> Double {
255 | guard let n = name , let score = studentScoreDic4[n] else {
256 | return 0.0;
257 | }
258 | return score;
259 | }
260 | getScoreByName3(name: "小明");
261 |
262 | //e.多条件简化法-if版
263 | let studentScoreDic5 = ["小明":30.0,"小摩纳哥":99.0];
264 | func getScoreByName5(name : String!) -> Double{
265 | if let str = name , let score = studentScoreDic5[str]{
266 | return score;
267 | }
268 | return 0.0;
269 | }
270 | print(getScoreByName5(name: "小明"));
271 |
272 | //中级
273 | //多条件判断
274 | //需求: 输入两个数字,如果两个数字都小于100并且第一个数字小于第二个数字,按照循序输出他们
275 | //方式一:
276 | func inputFirstNum(num1:String,num2:String){
277 | if let n1 = Int(num1){
278 | if let n2 = Int(num2) {
279 | if n1 < n2 && n2 < 100{
280 | print("按顺序输出\(n1)<\(n2)<\(100)");
281 | }
282 | }
283 | }
284 |
285 | }
286 | //方式二:
287 | func inputFirstNum2(num1:String,num2:String){
288 | if let n1 = Int(num1), let n2 = Int(num2), n1 < n2 && n2 < 100 {
289 | print("\(n1)<\(n2)<\(100)");
290 | }
291 | }
292 | //元祖判断
293 | if (1,"zebra") < (2, "apple"){
294 | print("结果为true");
295 | }
296 |
297 | // 你必须注意的几点
298 | //1.转换出现可选值
299 | //if let n = Int(3.3){
300 | //
301 | //}//编译错误
302 |
303 | if let n = Int("3.3") {
304 | print(n)
305 | }//编译通过
306 |
307 | //问题:为什么第一个if会报错?
308 | //答:Swift编译器要求我们右边必须为可选值类型Int(3.3)产生的结果不是可选值,我在swift3.0-数据类型中讲过数据类型之间转换不可能出现可选值,但是字符串转数字结果为可选值,所以第二个if是正确的.
309 |
310 | //我们修改上面的代码
311 | if let n:Int = Int(3.3) {
312 | print(n);
313 | }// 编译通过
314 |
315 | if let n:Int = Int("3.3") {
316 | print(n);
317 | }// 编译通过
318 |
319 | if let n:Int? = Int(3.3) {
320 | print(n);
321 | }// 编译警告-说总是成功的
322 |
323 | //if let n:Int! = Int(3.3) {
324 | // print(n);
325 | //}// 编译报错 - 非可选值不能解包
326 |
327 | //if let n :Int! = Int("3.3"){
328 | // print(n)
329 | //} // 编译报错-解包的时机不对
330 |
331 | //2.注意可选值的判断逻辑
332 | let name1 : String! = "酷走天涯"// 定义一个需要解封的可选值
333 | let name2 : String? = "酷走天涯"// 定义可选值
334 | let name3 : String = "酷走天涯"// 定义非可选值
335 |
336 | if name1 != nil {
337 |
338 | }//编译成功
339 |
340 | if let myName = name1 {
341 | print(name1);
342 | print(myName);
343 | }//编译成功
344 |
345 | if name2 != nil {
346 |
347 | }//编译成功
348 |
349 | if let myName = name2 {
350 | print(name2);
351 | print(myName);
352 | }//编译正确
353 |
354 | if name3 != nil {
355 |
356 | }// 编译警告 - 总是成功
357 |
358 | //if let myName = name3 {
359 | //
360 | //}//编译错误 - name3 不是可选值
361 |
362 |
363 |
364 |
--------------------------------------------------------------------------------
/swift 4 demo/11.初始化和释放.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | var str = "Hello, playground"
6 |
7 | //注意
8 | //1.系统要求存储属性必须初始化
9 | //2.可选值可以不用初始化,如果不初始化值,系统默认用nil初始化它
10 | //3.如果非可选类型存储属性不设置默认值,则必须在初始化方法中对其进行初始化
11 | //4.类必须自己写初始化方法,初始化没有默认值的非可选存储属性
12 | //5.结构体系统默认会添加初始化方法,当然自己也可以自定义
13 | //6..子类如果没有自己的初始化方法,系统默认使用父类的初始化方法,一旦有了自己的初始化方法,或者重写了父类的初始化方法,则父类的所有初始化不能被子类调用
14 | //7.你可以给子类添加和父类相同的初始化方法,但需要加上override 修饰
15 | //8.重写父类的convenience修饰的方便初始化方法,不需要加override 关键字
16 |
17 | //指定初始化(Designated)
18 | //1.可以有多个指定初始化方法
19 | class Person{
20 | var name : String;
21 | var age : Int = 0;
22 | var weight : Double = 0.0;
23 | var height : Double = 0.0;
24 | init(name: String,height : Double) {
25 | self.name = name;
26 | self.height = height;
27 | }
28 | init(name:String) {
29 | self.name = name;
30 | }
31 | }
32 |
33 | //方便初始化
34 | //1.在同一个类,使用convenience修饰的初始化方法必须调用一个其他初始化方法
35 | //2.convenience 必须最终调用一个指定的初始化方法
36 | //3.当子类继承父类时,子类的初始化方法,必须调用父类的指定初始化方法,不能调用使用convienience修饰的方便初始化方法
37 | //4.在swift4.0 初始化中,可以自己调用自己的初始化方法,系统不会检测出来,在创建convenience方便初始化方法的时候,需要小心,千万不要相互引用了
38 | //父类
39 | class Person1{
40 | var name : String;
41 | var age : Int = 0;
42 | var weight : Double = 0.0;
43 | var height : Double = 0.0;
44 |
45 | init(name : String, height : Double) {
46 | self.name = name;
47 | self.height = height;
48 | }
49 |
50 | init(name : String) {
51 | self.name = name;
52 | }
53 | //1.定义一个convenience修饰的初始化方法,如果在同一个类中必须调用其他没有convenience
54 | //修饰的初始化方法
55 | convenience init(name: String, age : Int){
56 | self.init(name: name);
57 | self.age = age;
58 | }
59 | //2.如果定义两个或者多个convenience修饰的初始化,只需要调用任意一个初始化方法即可满足语法要求
60 | convenience init(name : String, age : Int,weight : Double){
61 | self.init(name: name, age: age);
62 | self.weight = weight;
63 | }
64 |
65 | }
66 |
67 | //子类
68 | class Man : Person1 {
69 | var address : String = "";
70 | init(name : String, age : Int,weight : Double){
71 | //3.必须调用父类指定初始化方法,不能调用convenience修饰的方便初始化方法
72 | super.init(name: name);
73 | }
74 | }
75 |
76 | //类初始化的过程
77 | //第一阶段
78 | //1.调用指定初始化方法或者方便初始化
79 | //2.给新的实例分配内存,但内存还没有初始化
80 | //3.指定初始化方法确定所有存储属性都被初始化,内存这个时候被初始化.
81 | //4.然后去调用父类的指定初始化方法,任务和调用自己指定初始化方法相同
82 | //5.继续在类继承链中指定找父类初始化的过程,直到达到链的顶部位置.
83 | //6.当到完成基类的初始化的时候,实例的初始化算是完成了,我们的第一阶段完成
84 | //第二阶段
85 | //1.可以对属性值进行修改
86 | //2.可以调用对象方法
87 |
88 | //重写初始化方法
89 | //例子
90 |
91 | //父类
92 | class Person3{
93 | var name : String;
94 | var age : Int = 0;
95 | var weight : Double = 0.0;
96 | var height : Double = 0.0;
97 | init(name : String, height : Double) {
98 | self.name = name;
99 | self.height = height;
100 |
101 | }
102 |
103 | init(name : String) {
104 | self.name = name;
105 | }
106 |
107 | convenience init(name : String, age : Int, weight : Double){
108 | self.init(name: name);
109 | self.age = age;
110 | self.weight = weight;
111 | }
112 | }
113 |
114 | //子类
115 | class Man3 : Person3 {
116 | var address : String = "";
117 |
118 | //重写父类指定初始化方法
119 | override init(name: String) {
120 | super.init(name: name);
121 | }
122 | //重写父类convenience修饰的初始化方法 不需要添加override 关键字
123 | init(name : String, age : Int, weight : Double) {
124 | super.init(name: name);
125 | }
126 | //创建自己的初始化方法
127 | convenience init(name: String,age: Int,weight: Double,address: String) {
128 | self.init(name: name);
129 | self.address = address;
130 | self.age = age;
131 | self.weight = weight;
132 | }
133 |
134 | }
135 |
136 | //1.创建新的指定初始化方法,必须调用父类的指定初始化方法 (Designated)
137 | //2.创建新的方便初始化方法,必须调用自己的指定初始化方法,或者方便初始化方法(convenience)
138 | //3.重写父类的指定初始化方法,在方法名前加override ,然后调用父类的指定初始化方法
139 | //4.重写父类的方便初始化方法(convenience) 不需要加override 或者convenience 关键字,调用父类的指定初始化方法,如果加上convenice关键字,则必须调用自己的初始化方法
140 | //5.如果子类没有初始化方法,系统会自动继承父类的初始化方法
141 | //6.初始化调用父类初始化时,需要先初始化子类的存储属性,但是如果是convenience修饰的初始化方法,要先调用自己的其他初始化方法,然后再给自己的存储属性赋值
142 |
143 | //创建一个可能失败的初始化方法
144 | //注意 :
145 | //1.不能再重写的初始化方法改为可能失败的初始化方法
146 | //2.不能使用相同的参数定义一个可能失败的初始化方法和不会失败的初始化方法
147 | //3.可能失败的类型可以重写为不会失败的类型
148 | //4.init?可以被重写为init!.当然可逆也是可以的.
149 |
150 | //例子1
151 | class Man4 : Person3 {
152 | var address : String = "";
153 | convenience init?(name : String,age : Int,weight : Double,address : String){
154 | if name == "" {
155 | return nil;
156 | }
157 | self.init(name: name);
158 | self.address = address;
159 | self.age = age;
160 | self.weight = weight;
161 | }
162 | }
163 |
164 | //例子2
165 | enum TemperatureUnit{
166 | case kelvin, celsius, fahrenheit;
167 | init?(symbol : Character) {
168 | switch symbol {
169 | case "K":
170 | self = .kelvin;
171 | case "C":
172 | self = .celsius;
173 | case "F":
174 | self = .fahrenheit;
175 | default:
176 | return nil;
177 | }
178 | }
179 | }
180 | //例子2: 子类将父类可能失败的初始化方法,修改为不会失败的类型
181 | class Animal{
182 | var name : String;
183 | init?(name : String) {
184 | if name.isEmpty {
185 | print("字符串为空");
186 | return nil;
187 | }
188 | self.name = name;
189 |
190 | }
191 | }
192 |
193 | //Animal(name: nil); //会出错
194 | Animal(name: "");
195 |
196 | //对于可能出现空值的对象或者其他类型,使用之前必须进行验证
197 | enum TemperatureUnit1 : Character {
198 | case kelvin = "K", celsius = "C", fahrenheit = "F";
199 | }
200 |
201 | let fahrenheitUnit1 = TemperatureUnit1(rawValue: "F");
202 | if fahrenheitUnit1 != nil {
203 | print("This is a defined temperature unit, so initialization succeeded.")
204 | }
205 |
206 | //需求:创建一个文件类,文件名字可以为nil,但是不能为空即""
207 |
208 | //写法一:
209 | class Document {
210 | var name : String?;
211 | init?(name : String?){
212 | if name != nil || (name?.isEmpty)! {
213 | print("输出\(name!)");
214 | return nil;
215 | }
216 | print("输出\(name!)");
217 | self.name = name;
218 | }
219 | }
220 |
221 | //分析这种写法
222 | //只有一种初始化方法,也就是说,不管有没有名字,我们都需要给初始化传个参数,显然这样不合理,目标不明确
223 | Document(name: "");
224 |
225 | //Document(name: nil);// 不能传空
226 |
227 | //写法二:
228 | class Document2{
229 | var name : String?;
230 | init() {
231 | // 专门初始化name为nil的情况
232 | }
233 | init?(name : String) {//传入名字,肯定不为nil,只需要判断是否为空""即可
234 | if name.isEmpty {
235 | print("输出\(name)");
236 | return nil;
237 | }
238 |
239 | self.name = name;
240 | }
241 | }
242 |
243 | Document2();
244 | Document2(name: "");
245 | //Document2(name: nil); //报错不能为空 使用Document2();
246 |
247 | //疑问: init! 和init? 被重写为init 的意义何在?
248 | //基本没啥使用情况
249 |
250 | //需要的初始化方法(required)
251 | //注意
252 | //1.子类必须重写父类用required修饰的方法
253 | //2.可以和convenience组合使用
254 | //a-1.父类 要求一个初始化方法被重写
255 | class Person5{
256 | var name:String
257 | var age:Int = 0
258 | var weight:Double = 0.0
259 | var height:Double = 0.0
260 | init(name:String,height:Double) {
261 | self.name = name
262 | self.height = height
263 | }
264 | init(name:String) {
265 | self.name = name
266 | }
267 | // 要求子类必须重写这个方法
268 | required convenience init(name:String,age:Int,weight:Double){
269 | self.init(name:name)
270 | self.age = age
271 | self.weight = weight
272 | }
273 | }
274 |
275 | //a-2.子类重写父类要求的初始化方法
276 |
277 | class Man5: Person5 {
278 | var address:String = ""
279 | // 重写父类要求的初始化convenience 修饰的初始化方法 不需要添加override 关键字
280 | required init(name:String,age:Int,weight:Double){
281 | super.init(name: name)
282 | }
283 | }
284 |
285 | //反初始化(deinit)
286 | //注意:
287 | //1.deinit在对象被释放前调用
288 | //写法很简单
289 | //deinit{
290 | //
291 | //}
292 |
293 | //苹果文档有个例子简单讲解了一下它的重要性
294 | //例子: 有一个赌徒在银行存了10_000 元,赌徒从银行取钱然后去赌博,当赌徒对象释放了就将钱全部存到银行
295 | class Bank {
296 | static var coinsInBank = 10_000
297 |
298 | static func distribute(coins numberOfCoinsRequested: Int) -> Int {
299 | //去存在金额和输入金额最小值
300 | let numberOfCoinsToVend = min(numberOfCoinsRequested, coinsInBank)
301 | //减去取出金额
302 | coinsInBank -= numberOfCoinsToVend
303 | //返回取出金额
304 | return numberOfCoinsToVend
305 | }
306 |
307 | static func receive(coins: Int) {
308 | coinsInBank += coins
309 | }
310 | }
311 |
312 | //赌徒类
313 | class Player {
314 | var coinsInPurse: Int
315 | init(coins: Int) {//抛出
316 | coinsInPurse = Bank.distribute(coins: coins)
317 | }
318 | func win(coins: Int) {//赢了
319 | coinsInPurse += Bank.distribute(coins: coins)
320 | }
321 | deinit {
322 | Bank.receive(coins: coinsInPurse)
323 | }
324 | }
325 | //执行
326 | var playerOne: Player? = Player(coins: 100)
327 | playerOne!.win(coins: 2_000)
328 | playerOne = nil
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
--------------------------------------------------------------------------------
/swift 4 demo/2数据类型.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: Playground - noun: a place where people can play
2 |
3 | import UIKit
4 |
5 | //------- 数据类型 --------
6 |
7 | //1 常量定义
8 | let myConstant = 42;
9 | let myConstantInt : Int = 42 // 指定类型定义
10 | let name = "酷走天涯" // 类型推断定义
11 | var red,green,blue: Double //单行定义多个变量
12 | let cat = "🐱";print(cat) //如果有; 单行可以写过个语句
13 | let binaryInteger = 0b10001 //2进制数字定义 0b表示二进制
14 | let octalInteger = 0o21 //八进制数字定义 0o表示八进制
15 | let hexadecimalInteger = 0x11 //16进制数组的定义 0x表示16进制
16 | let exponentDouble = 0xC.3p0 //16进制科学技术发定义
17 | let oneMillion = 1_000_000 //可以使用_线将数字分开,便于人知
18 |
19 | //2 变量定义
20 | var myVaruable = 42;
21 |
22 | //3 类型转换
23 | let num1 = 12;
24 | let str = "\(num1)";
25 | let num2 = Int(num1);
26 | let num3 = Int(str);
27 | let num4 = Double(str);
28 |
29 | //4 数组的几种定义方式
30 | let list1 = ["你好","2","3","4"];
31 | var list2 : [String] = ["你好","2","3","4"];
32 | var list3 : [Any] = ["你好","2","3","4",UILabel(),"2","3"];
33 | let list5 : NSMutableArray = ["1","2","3","4"];
34 |
35 | //4_1 清空数组
36 | list2.removeAll() // 如果定义为var
37 | list2 = [] // 如果定义为var
38 | list5.removeAllObjects() // var 和 let 都可以
39 |
40 | //4_2 取代操作
41 | list3[4...6] = ["Bananas","Apples"] // 将数组4...6的范围用指定的数组取代
42 |
43 | //4_3 插入操作
44 | list3.insert("第一位", at: 0);
45 |
46 | //5 字典
47 | let dic1 : [String : Int] = [:];
48 | let dic2 = [String : Int]();
49 | let dic3 : NSDictionary = NSDictionary();
50 | let dic4 : NSMutableDictionary = [:];
51 |
52 | //5_1 获取数据类型的最大和最小值
53 | let minValue = UInt8.min;
54 | let maxValue = UInt8.max;
55 |
56 | //5_2 给数据类型设置别名
57 | typealias Code = Int32;// 给Int32 设置别名
58 | var tel :Code = 376823;
59 |
60 | //6 元祖类型
61 | //6_1 定义
62 | //第一种定义方法
63 | let http404Error = (404,"Not Found") // 定义元祖类型常量
64 | let code = http404Error.0;
65 | let error = http404Error.1;
66 | //第二种定义方法
67 | let http404Error2 = (code:404, error:"Not Found") // 定义元祖类型的常量
68 | let code2 = http404Error2.code;
69 | let error2 = http404Error2.error;
70 | //也可以像上面一个获取对应的数据
71 | //第三种定义方法
72 | let http404Error3 : (code : Int, error : String) = (404, "Not Found") //定义元祖类型的常量
73 | let code3 = http404Error3.code;
74 | let error3 = http404Error3.error;
75 | //第四种定义方法
76 | let http404Error4 : (code : Int, error : String) = (code:404,error:"Not Found") //定义元祖类型常量
77 | let code4 = http404Error4.code;
78 | let error4 = http404Error4.error;
79 | //第5种定义方法
80 | var http404Error5 : (code : Int, error : String) = (_:404,_:"Not Found");
81 |
82 | //提示
83 | //第四种定义的时候,等号坐标和右边的元素名称必须对应,不然系统会报错,建议不使用这种方式定义
84 |
85 | //6_2 分解变量
86 | let (statusCode, _) = http404Error; // 缺省不需要的值
87 | let (statusCode1,statusMessag2) = http404Error;
88 |
89 | //6_3 赋值
90 | var http404Error6 : (code: Int,error : String);//定义元祖
91 | http404Error6 = (code : 404,error : "Not Found");//部分名称缺省
92 | http404Error6 = (404,error : "Not Found");//部分名称缺省
93 | http404Error6 = (404,"NotFound");//全部名称缺省
94 | http404Error6 = (_:404,"Not Found"); //可以使用_代替名称
95 | http404Error6 = (code:404,"Not Found"); //不允许这样必须,名称必须和定义时保持一致
96 |
97 | //7 字符串和字符
98 | let string = "hello,"+"word" // Swift中终于可以这么方便处理字符串的拼接了
99 | var nameText = "swift";
100 | nameText.append("你好");//也可以这样拼接 name必须为var
101 | var anotherEmptyString = String() // 定义空字符串
102 | // 判断字符串是否为空
103 | if anotherEmptyString.isEmpty {
104 | print("Nothing to see here");
105 | }
106 | //获取字符串中每个字符
107 | for character in "Dog!🐶123456".characters {
108 | //弃用
109 | print(character)
110 | }
111 | //新版使用
112 | for character in "Dog!🐶123456" {
113 | print(character);
114 | }
115 |
116 | for index in "Dog!🐶123456".characters.indices {
117 | //弃用
118 | print("\("Dog!🐶123456"[index])", terminator: "////")
119 | }
120 | //新版使用
121 | for index in "Dog!🐶123456".indices {
122 | print("\("Dog!🐶123456"[index])", terminator: "////");
123 | }
124 |
125 | //定义字符
126 | let exclamationMark : Character = "!";
127 | //定义字符串数组
128 | let catCharacters : [Character] = ["C","A","T","!","🐱"];
129 | //字符数组转字符串
130 | let catString = String(catCharacters);
131 | //Unicode编码
132 | let precomposed : Character = "\u{D55C}";
133 | let regionalIndicatorForUS : Character = "\u{1F1FA}\u{1F1F8}"
134 |
135 | var dogName = "🐶a我";
136 | print(dogName.count);// 3 一个不管是什么都算一个字符
137 | //截取字符串
138 | let greeting = "Guten Tag!";
139 | //截取单个
140 | greeting[greeting.startIndex];
141 | //截取一段
142 | greeting[greeting.index(greeting.startIndex, offsetBy: 2).. String {
168 | return "我是一个Int类型的数字\(self)"
169 | }
170 | }
171 | print(3.description());
172 | //协议扩展的好处1
173 | //定义个协议
174 | protocol NumberProtocol{
175 | func numberDescription()->String
176 | }
177 | //扩展实现协议
178 | extension Int:NumberProtocol{
179 | func numberDescription()->String{
180 | return "我是一个Int类型的数字\(self)";
181 | }
182 | }
183 | extension Double:NumberProtocol{
184 | func numberDescription() -> String {
185 | return "我是一个Double类型的数字\(self)";
186 | }
187 | }
188 |
189 | // 定义一个协议类型
190 | var numberProtocol : NumberProtocol = 3;
191 | numberProtocol.numberDescription();
192 | numberProtocol = 3.4;
193 | numberProtocol.numberDescription();
194 | //通过这种方法,我们可以给同一个变量,赋值不同类型的值了,其实这个符合Swift的语法要求,只是我们利用它的灵活性,达到了我们目的.
195 |
196 |
197 |
198 | //协议的好处2
199 | //需求
200 | //给Int 类型和Double 类型增加一个方法,判断它的数据类型
201 | //定义一个协议
202 | protocol TextNumberProtocol{
203 |
204 | }
205 |
206 | //扩展实现协议
207 | extension Int:TextNumberProtocol{
208 |
209 | }
210 |
211 | extension Double:TextNumberProtocol{
212 |
213 | }
214 |
215 | //给协议扩展方法
216 | extension TextNumberProtocol {
217 | func description() -> String {
218 | if self is Int {
219 | return "我是一个Int类型的数字\(self)";
220 | }
221 | if self is Double {
222 | return "我是一个Double类型的数字\(self)";
223 | }
224 | return "我既不是Int也不是Double类型数字\(self)";
225 | }
226 | }
227 |
228 | print(3.44.description());
229 | print(3.description());
230 | //Self的好处
231 | //专门用于不确定数据类型
232 | // 需求: 给所有数字类型,扩展一个平方的函数,返回自己的操作.
233 | //定义个协议
234 | protocol TextOneNumberProtocol {
235 |
236 | }
237 |
238 | //扩展实现协议
239 | extension Int : TextOneNumberProtocol {
240 |
241 | }
242 |
243 | extension Double : TextOneNumberProtocol {
244 |
245 | }
246 |
247 | //给协议扩展方法
248 | extension TextOneNumberProtocol {
249 | // 我们不确定返回的self 到底是什么类型
250 | func squareValue() -> Self {
251 | if self is Int {
252 | let n = self as! Int;
253 | return n * n as! Self;
254 | }
255 | if self is Double {
256 | let n = self as! Double;
257 | return n * n as! Self;
258 | }
259 | return 0 as! Self;
260 | }
261 |
262 | }
263 | print(2.44.squareValue());//平方根
264 | print(3.squareValue());//平方根
265 | //我只想说这个用法只是Self的冰山一角更多神奇的东西,等待我们去探索。
266 |
267 | //你需要注意的
268 | //1.如果指出变量类型,赋值的值必须是和他类型相同的值,不然编译不通过.
269 | var num : Int = Int(43.0) //(编译错误)
270 | //2.浮点数类型推断出来的默认为Double类型
271 | let numDouble = 30.0 //Double
272 | //3.Float 类型的值 赋值给Double类型也必须转换
273 | let numFloat : Float = 30.0;
274 | let explicitDouble : Double = Double(numFloat);
275 | //4.数字之间的转换结果为非可选值,数字转字符串也是非可选值,但是字符串转数字就是可选值(因为它有可能转换失败)
276 | let number1 = 7;
277 | let strText1 = "测试";
278 | let strPut1 = "\(number1)";
279 | let strPut2 = String(number1);
280 | let numPut2 = Int(num1);
281 | let numPut3 = Int(strText1);
282 | let numPut4 = Double(strText1);
283 |
284 | //字符串33.0转Int类型只会是nil
285 | //不会是33因为字符串33.0不是Int类型转换失败,但是浮点数33.0可以转换为33
286 | //5.不能推断出下面的了类型
287 | //let listText1 = ["你好","2","3","3",3]//不能这样写,Swift 推断不出来
288 | //应该:
289 | let listText2 = ["你好","2","3",3] as! [Any];
290 | let listText3 : Any = ["你好","2","3",3];
291 | let listText4:NSArray = ["你好","3","2",3];
292 | let listText5:NSMutableArray = ["你好","3","2",3];
293 | //6.使用Dictionary定义字典必须指定数据类型
294 | //let dicText1 : Dictionary = [:]//错误
295 | let dicText2 : Dictionary = [String : Int]()//正确
296 | //无聊的测试
297 | //类型推断对性能的影响5000000
298 | for i in 0...50 {
299 |
300 | }
301 | //类型推断对性能没有影响
302 | //Array和NSArray,NSMutableArray的区别
303 | //1.测试类型
304 | var listA1:Array = ["你好","2","3","4"];
305 | let listA2 = listA1;
306 | listA1[1] = "哈哈"
307 | print(listA1);
308 | print(listA2);
309 |
310 | var listB1:NSMutableArray = ["你好","2","3","4"];
311 | let listB2 = listB1;
312 | listB1[1] = "哈哈";
313 | print(listB1);
314 | print(listB2);
315 |
316 | //2.测试内存占用
317 | func add(){
318 | var array : [Any] = [3];
319 | for _ in 0...1000 {//正常电脑好多写点
320 | array.append(3);//看内存变化
321 | }
322 | }
323 |
324 | //执行代码前 3.5M 执行代码后 3.8 M 内存占用最高 308.6M
325 |
326 | func add1() {
327 | let array : NSMutableArray = NSMutableArray();
328 | for i in 0...100000 {
329 | if i == 1000000 {
330 | //结束时执行内存
331 | }
332 | }
333 | }
334 |
335 | //执行代码前3.5 M ,代码执行完毕后 17.3M,内存占用最高为 422.3
336 | //3.我们使用Array放对象
337 | func add2(){
338 | var array: [Any] = [];
339 | for _ in 0...100 {//多了内存变化
340 | array.append([UILabel()]);
341 | }
342 | }
343 | //运行圈3.5运行后10.5最高占内存21.5,发现内存没有释放完毕.
344 | autoreleasepool {
345 | var array:[Any] = [];
346 | for i in 0...100 {
347 | array.append([UILabel()])
348 | }
349 | }
350 | //Swift 中新增的Array 存放非对象类型,内存清理的更及时,更彻底!
351 |
352 | //4.数据的上溢或者下溢系统都会报错
353 | //Int8.min - 1
354 | //Int8.max + 1
355 | //arithmetic operation '127 + 1' (on type 'Int8') results in an overflow
356 | //error: arithmetic operation '-128 - 1' (on type 'Int8') results in an overflow
357 |
358 | //5.浮点数取余运算
359 | let z = CGFloat(23.4).truncatingRemainder(dividingBy: 20)
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
--------------------------------------------------------------------------------