Swift Language
Total Page:16
File Type:pdf, Size:1020Kb
Swift Language #swift 1 1: Swift 2 2 2 2 Examples 2 Swift 2 Swift 4 MacSwift 4 iPadSwift Playgrounds 9 10 2: 11 11 11 Examples 11 UnsafeMutablePointer 11 12 3: AES 14 Examples 14 IVSwift 3.0CBCAES 14 CBCAESIVSwift 2.3 16 PKCS7ECBAES 18 4: KituraSwift HTTP 20 20 Examples 20 20 5: OptionSet 23 Examples 23 OptionSet 23 6: PBKDF2 24 Examples 24 2Swift 3 24 2Swift 2.3 25 Swift 2.3 26 Swift 3 26 7: RxSwift 28 Examples 28 RxSwift 28 28 29 30 RxCocoaControlEvents 30 8: Swift Advance 32 32 Examples 32 32 33 9: SwiftNSRegularExpression 34 34 Examples 34 String 34 35 35 36 36 NSRegularExpression 36 10: Swift 38 Examples 38 38 38 38 39 40 11: Swift 42 Examples 42 Swift 42 12: Swift 44 44 Examples 44 44 44 47 47 - On log n 47 48 48 56 58 13: Typealias 62 Examples 62 62 62 62 14: StringUIImage 63 63 Examples 63 InitialsImageFactory 63 15: Swift 64 64 Examples 64 64 64 65 16: CObjective-C 66 66 Examples 66 Objective-CSwift 66 66 67 SwiftObjective-C 67 67 68 swiftc 68 C 69 Objective-CSwift 69 C 70 17: 72 Examples 72 72 Dependenct 72 DI 72 73 75 DI 75 76 77 77 18: 78 78 78 Examples 78 78 78 79 typealias 79 79 2 80 4 80 Switch 80 19: 82 82 82 Examples 82 82 83 83 83 @noescape 84 3 84 throwsrethrows 84 / 85 86 86 87 20: 88 88 88 weak-keyword 88 unowned-keyword 88 88 Examples 88 88 89 89 21: 91 Examples 91 91 91 92 94 init 94 initotherStringString 94 94 init 95 95 22: 96 Examples 96 96 96 97 97 98 98 98 Inout 98 98 99 99 100 100 100 101 101 23: 103 103 103 Examples 103 103 103 104 106 107 RawRepresentable 108 108 109 109 Hashable 109 24: 111 111 111 Examples 111 111 112 25: 115 115 Examples 115 115 115 115 116 116 117 117 26: 119 119 Examples 119 119 119 27: 120 120 Examples 120 120 120 120 Key 121 121 121 28: 123 123 123 Examples 123 123 123 124 124 125 125 125 126 126 126 126 127 127 String 127 129 Set 129 129 129 129 129 130 Swift 130 130 WhiteSpaceNewLine 132 Data / NSData 132 133 29: 134 134 Examples 134 134 134 30: 136 Examples 136 MD2MD4MD5SHA1SHA224SHA256SHA384SHA512Swift 3 136 HMACMD5SHA1SHA224SHA256SHA384SHA512Swift 3 137 31: 140 Examples 140 Bool 140 Bool 140 140 141 32: 142 142 Examples 142 Grand Central DispatchGCD 142 Grand Central DispatchGCD 143 144 OperationQueue 145 146 33: 149 Examples 149 149 149 34: 150 150 150 Examples 150 150 150 150 where 151 151 151 152 153 153 153 - prepareForSegue 154 35: 156 156 Examples 156 For-in 156 156 156 157 157 158 158 158 159 For-in 159 160 36: 161 Examples 161 161 161 37: 162 162 Examples 162 162 162 162 163 163 163 164 38: 166 Examples 166 166 166 166 166 167 167 167 167 168 168 168 168 168 169 169 39: 170 170 170 170 Examples 170 170 170 170 170 171 171 171 171 172 172 172 172 173 173 map_ :) 174 flatMap_ :)Array 174 174 flatMap_ :)nil 175 175 176 flatMap_ :) 176 176 177 177 flatten 178 Arrayreduce_combine :) 178 178 Swift3 178 179 179 180 2 180 40: 181 Examples 181 181 181 41: 186 186 186 Examples 186 UIViewControllerSwizzling viewDidLoad 186 Swift Swizzling 187 Swizzling - Objective-C 188 42: 189 189 189 Examples 189 Guard 189 if 189 AND 190 OR 190 NOT 190 “where” 190 192 Nil-Coalescing 192 43: 194 194 Examples 194 194 195 195 196 197 198 199 44: 200 200 Examples 200 200 200 200 201 201 201 202 203 203 204 204 45: Swift 206 206 Examples 206 206 206 207 printvs dump 208 vs NSLog 208 46: 210 Examples 210 210 47: 213 213 Examples 213 213 213 214 214 DEINIT 215 48: 216 216 Examples 216 216 216 217 217 Swift 217 217 218 IntFloat - 218 218 218 218 String 218 StringInt 219 JSON 219 Optional JSON 219 JSON 219 219 Empty Dictionary 220 49: 221 Examples 221 221 221 221 221 222 222 struct 222 50: 223 223 Examples 223 223 223 51: - 224 224 Examples 224 224 224 225 226 227 228 228 231 52: - 235 235 Examples 235 235 235 53: 237 237 237 Examples 237 Struct 237 Car.make 238 Car.model 238 Car.otherNamefileprivate 238 Car.fullName 238 238 GettersSetters 238 54: 240 240 240 240 Examples 240 240 241 241 242 - 243 55: 244 244 Examples 244 244 245 246 247 247 56: JSON 249 249 Examples 249 Apple FoundationSwiftJSON 249 JSON 249 JSON 249 250 JSON 250 JSON 251 251 251 SwiftyJSON 251 253 JSON 253 254 254 254 255 255 JSON 256 JSONSwift 3 257 57: 260 Examples 260 260 260 260 260 Set 261 CountedSet 261 58: 263 263 Examples 263 263 264 59: 268 268 Examples 268 268 268 268 268 269 269 269 269 269 269 269 270 270 270 270 270 ...... 271 271 271 60: 272 Examples 272 272 + 272 273 274 275 Swift 275 277 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: swift-language It is an unofficial and free Swift Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Swift Language. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/zh-CN/home 1 1: Swift SwiftApple 。 SwiftApplemacOSiOStvOSwatchOSObjective-CCocoa / CocoaAPI。 SwiftmacOS Linux。AndroidWindows。 SwiftGitHub ;。 bugs.swift.org。 SwiftSwift 。 • Swift • Swift • Swift • API • Swift iBooks • ...developer.apple.com 。 Swift Xcode - 2010-07-17 1.0 Xcode 6 201462 1.1 Xcode 6.1 20141016 1.2 Xcode 6.3 201529 2.0 Xcode 7 2015-06-08 2.1 Xcode 7.1 2015923 - 2015123 2.2 Xcode 7.3 2016321 2.3 Xcode 8 2016913 3.0 Xcode 8 2016913 3.1 Xcode 8.3 2017327 Examples https://riptutorial.com/zh-CN/home 2 Swift hello.swift print("Hello, world!") • swift LinuxCTRL + ALT + T macOS Launchpad。cd directory_name cd .. $ swift hello.swift Hello, world! 。 • swiftc $ swiftc hello.swift hello。./ 。 $ ./hello Hello, world! • swift REPLRead-Eval-Print-Loopswift func greet(name: String, surname: String) { print("Greetings \(name) \(surname)") } let myName = "Homer" let mySurname = "Simpson" greet(name: myName, surname: mySurname) • func greet(name: String, surname: String) { // function body } - namesurname 。 • print("Greetings \(name) \(surname)") - “Greetings”name surname 。\( variable_name )。 • let myName = "Homer"let mySurname = "Simpson" - let myName mySurnamevalues "Homer" "Simpson" 。 • greet(name: myName, surname: mySurname) - myName mySurname。 REPL $ swift Welcome to Apple Swift. Type :help for assistance. 1> func greet(name: String, surname: String) { https://riptutorial.com/zh-CN/home 3 2. print("Greetings \(name) \(surname)") 3. } 4> 5> let myName = "Homer" myName: String = "Homer" 6> let mySurname = "Simpson" mySurname: String = "Simpson" 7> greet(name: myName, surname: mySurname) Greetings Homer Simpson 8> ^D CTRL + DREPL。 Swift 。 Swift。macOS/ Library / Developer / Toolchains。 export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}" Linuxclang $ sudo apt-get install clang SwiftSwift $ export PATH=/path/to/Swift/usr/bin:"${PATH}" Swift $ swift --version MacSwift MacMac App StoreXcode。 XcodePlayground https://riptutorial.com/zh-CN/home 4 PlaygroundMyPlayground “ ” https://riptutorial.com/zh-CN/home 5 PlaygroundCreate https://riptutorial.com/zh-CN/home 6 Playground https://riptutorial.com/zh-CN/home 7 Playground + cmd + Y 。 Playground print("Hello world") “Hello world”“Hello world \ n” https://riptutorial.com/zh-CN/home 8 Swift iPadSwift Playgrounds Swift PlaygroundsSwift。 1-App StoreiPadSwift Playgrounds 。 https://riptutorial.com/zh-CN/home 9 https://riptutorial.com/zh-CN/home2-。 10 3-“ ”+ 。 4-。 5-。 6-。。 7-“”。 。 Optionals“x”“”。 OptionalSwift。 NoneSome(T) TSwift。 let x: String? = "Hello World" print(x.dynamicType) Optional<String> OptionalOptional。 OptionalXcodeOptional enum Optional<Wrapped> { Wrapped。 .none.someWrapped。 String?StringOptional<String> 。OptionalmapflatMap 。 Swift https://riptutorial.com/zh-CN/swift/topic/202/swift 2: “。。“ Apple Inc.“SwiftCocoaObjective-CSwift 3.1。”iBooks。 https://itun.es/us/utTW7.l 。 BufferPointers。 • MemoryLayout 。 • Unmanaged 。 • UnsafeBufferPointer 。 • UnsafeBufferPointerIterator UnsafeBufferPointerUnsafeMutableBufferPointer 。 • UnsafeMutableBufferPointer 。 • UnsafeMutablePointer 。 • UnsafeMutableRawBufferPointer nonowning。 • UnsafeMutableRawBufferPointer.Iterator 。 • UnsafeMutableRawPointer 。 • UnsafePointer 。 • UnsafeRawBufferPointer 。 • UnsafeRawBufferPointer.Iterator 。 • UnsafeRawPointer 。 Swiftdoc.org Examples UnsafeMutablePointer struct UnsafeMutablePointer<Pointee> 。 UnsafeMutablePointer。Pointee。 UnsafeMutablePointer。。 。UnsafeMutablePointer。 import Foundation let arr = [1,5,7,8] let pointer = UnsafeMutablePointer<[Int]>.allocate(capacity: 4) pointer.initialize(to: arr) let x = pointer.pointee[3] https://riptutorial.com/zh-CN/home 11 print(x) pointer.deinitialize() pointer.deallocate(capacity: 4) class A { var x: String? convenience init (_ x: String) { self.init() self.x = x } func description() -> String { return x ?? "" } } let arr2 = [A("OK"), A("OK 2")] let pointer2 = UnsafeMutablePointer<[A]>.allocate(capacity: 2) pointer2.initialize(to: arr2) pointer2.pointee let y = pointer2.pointee[1] print(y) pointer2.deinitialize() pointer2.deallocate(capacity: 2) Swift 3.0 Swift; public init?(validatingUTF8 cString: UnsafePointer<CChar>) nullUTF-8。 UTF-8。nil 。CChar - UTF-8。 Source Apple Inc.Swift 3 For header accessIn PlaygroundCmd +Swift