Slide 1

Slide 1 text

Swift पใ 5/24 John Lin @johnlinvc

Slide 2

Slide 2 text

From the core • Swift 3.0 ᚙ෍ྲྀఔ • ᔟܕએݴ (Generic Manifesto)

Slide 3

Slide 3 text

Swift 3.0 ᚙ෍ྲྀఔ • Swift 3.0 ᔒ༗޲Լ૬༰ Swift 2.2 • ሡ။ੋୈҰݸแؚ Package Manager త൛ຊ • ༬ܭ2016Լ൒೥ਖ਼ࣜެ䆋 • preview1 branchቮࡏ5/12૑ཱ ɼ༬ܭ4-6पޙ releaseʢWWDC 2016?)

Slide 4

Slide 4 text

ᔟܕએݴ (Generic Manifesto) • Swift తᔟܕؐ༗ਐ㑊తۭؒɼᔟܕએݴྻग़ྃҰࠣ ڞಉత໨ඪ • Ҡআෆඞཁతݶ੍: recursive protocol, nested generics • ᩋߋଟతએࠂՄҎ༗ᔟܕჩᏐ: typealiases, subscripts, constant, extension

Slide 5

Slide 5 text

ᔟܕએݴ (Generic Manifesto) • ሣᔟܕతখᎷॆ: default generic, override protocol extension • ሣᔟܕతେᎷॆ: conditional conformances, variadic generics, tuple extension • ޠ๏վਐ: protocol default implementation, moving there where clause outside of the angle brackets, rename protocol<…> to Any<…>

Slide 6

Slide 6 text

Evolution • SE-0017 ኑআ Unmanaged ɼվ༻ UnsafePointer • SE-0032 Sequence ৽⃧ first(where:) ํ๏ • SE-0045 Sequence ৽⃧ prefix(while:) ᢛ drop(while:) • SE-0052 IteratorType ሡ။อᨽ௒աඌ୺ޙɼnext() Ӭԕճၚ nil

Slide 7

Slide 7 text

SE-0017 ኑআ Unmanaged ɼվ༻ UnsafePointer • Unmanaged API தత COpaquePointer ሡ။ඃҠ আɼվ༻ UnsafePointer औ୅ func fromOpaque(value: UnsafePointer) -> Unmanaged func toOpaque() -> UnsafeMutablePointer

Slide 8

Slide 8 text

SE-0032 Sequence ৽⃧ first(where:) ํ๏ Old let ary = [1,2,3,5,7] let two = ary[ary.indexOf{$0 == 2}!]
 New let ary = [1,2,3,5,7]
 let two = ary.first{$0 == 2}!

Slide 9

Slide 9 text

SE-0045 Sequence ৽⃧ prefix(while:) ᢛ drop(while:) let ary = [1,2,3,5,7]
 let lessThan5 = ary.prefix{$0 < 5} //[1,2,3] let greaterThan4 = ary.drop{$0 < 4} //[5,7]

Slide 10

Slide 10 text

SE-0052 IteratorType ሡ။อᨽ௒ա ඌ୺ޙɼnext() Ӭԕճၚ nil • ݱࡏతจ݅نఆᙛ next() ճၚ nil ޙबෆ֘࠶ճၚ nil , ጯ֘ཁ raise preconditionFailure() ɻෆաݱ༗త IteratorType શ෦౎။Ұ௚ճၚ nilɻ • मվจ݅ိอᨽݱ༗తߦҝɻ while let element = iterator.next() { if condition(element) { foo(element) // call foo on first element satisfying condition break } } while let element = iterator.next() { bar(element) // call bar on remaining elements }

Slide 11

Slide 11 text

Evolution • SE-0061 autoreleasepool() ሡ။༗ճၚᆴ࿨ࡨޡ႔ ཧ • SE-0066 വᏐܕผჩᏐҰఆཁ༗ׅᥒ • SE-0067 ڧԽ FloatingPoint protocol • SE-0069 Immutable Foundation Types

Slide 12

Slide 12 text

SE-0061 autoreleasepool() ሡ။༗ճၚᆴ࿨ࡨޡ႔ཧ • ݱࡏతautoreleasepool ᔒ༗ճၚᆴ࿨ࡨޡ႔ཧ • ະိ။༗ճၚᆴ࿨ࡨޡ႔ཧ func doWork() throws -> Result { return try autoreleasepool { ... actual computation which either returns or throws ... } }

Slide 13

Slide 13 text

SE-0066 വᏐܕผჩᏐҰఆཁ༗ ׅᥒ • ݱࡏവᏐతჩᏐՄҎᔒ༗ׅᥒ (String) -> Int String -> Int • ୠ༗࣌။ࠞᔿ (Int, Float) -> Int // ੋၷݸჩᏐ䏆ʁؐੋҰݸ༗ၷݸݩૉత Tuple ჩ Ꮠ? • ະိҰఆཁ༗ׅᥒ

Slide 14

Slide 14 text

SE-0067 ڧԽ FloatingPoint protocol • Float ݱ༗త function ૬ᙛ༗ݶ • ⃧Ճ add, negate, subtract, multiply, divide, remainder, squareRoot ౳౳

Slide 15

Slide 15 text

SE-0069 Immutable Foundation Types • Foundation Typeฒᔒ༗޷޷త࢖༻ Swift త Value Type ޭೳ • ڐଟ NS* త class ሡ။༗Ұݸ Struct ൛తძ๔଻ɼ ൺํ㘸 NSURL ሡ။༗Ұݸ URL త Struct ൛ɻ

Slide 16

Slide 16 text

Evolution • SE-0070 Optional protocol method લҰఆཁՃ @objc • SE-0071 enum ՄҎ༻زݷॴ༗త keyword ိ໋໊ • SE-0072 ׬શҠআ implicit bridging • SE-0060 ༗༬ઃᆴతჩᏐؐੋཁ҈রॱং

Slide 17

Slide 17 text

SE-0070 Optional protocol method લҰఆཁՃ @objc • Swift protocol ฒᔒ༗ optional తޭೳɼ୞༗ objc protocol ࠽༗ • ҝྃᩋṜ݅ࣄߋ໌ᰖɼoptional method લཁՃ @objc
 @objc protocol NSTableViewDelegate { @objc optional func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -> NSView? // correct optional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat // error: 'optional' requirements are an Objective-C compatibility feature; add '@objc' }

Slide 18

Slide 18 text

SE-0071 enum ՄҎ༻زݷॴ༗త keyword ိ໋໊ • ݱࡏՄҎ༻backstick ိ໋໊enum case enum UITableViewCellStyle : Int { case \`default\` case subtitle } let cell = UITableViewCell(style: .`default`, reuseIdentifier: nil) • ະိՄҎলུ let cell = UITableViewCell(style: .default, reuseIdentifier: nil)

Slide 19

Slide 19 text

SE-0072 ׬શҠআ implicit bridging • ݱࡏࡏ෦෼తfunction தɼSwift type ။ࣗಈ᫚੒ ObjC type
 let s : String = "hello" let nss : NSString = NSString(format: "%@", s) • ະိҰఆཁखಈ༻ as ိ᫚ܕ

Slide 20

Slide 20 text

SE-0060 ༗༬ઃᆴతჩᏐؐੋཁ ҈রॱং • ݱࡏ༗༬ઃᆴతჩᏐݺڣ࣌ՄҎෆ༻҈রॱংɼୠੋ ᔒ༗༬ઃᆴతཁ҈রॱংɻ func defaultArg(a a :Int=1, b:Int=2) -> Int{ return b/a } defaultArg(a: 3, b: 12) //4 defaultArg(b: 6, a:2) //3 • ະိҰఆཁ҈রॱং

Slide 21

Slide 21 text

Evolution • SE-0076 ኺ UnsafePointer ෳ੡ࢿྉ౸ UnsafeMutablePointer • SE-0080 failable Numeric initializer • SE-0082 Package manager Մमվత dependency • SE-0075 Build Configuration Import Test

Slide 22

Slide 22 text

SE-0076 ኺ UnsafePointer ෳ੡ࢿ ྉ౸ UnsafeMutablePointer • ݱࡏ୞ೳࡏUnsafeMutablePointer ೭ؒෳ੡ࢿྉɼ ৽⃧ኺ UnsafePointer ෳ੡ࢿྉ౸ UnsafeMutablePointer తޭೳ let source: UnsafePointer = ... let destination: UnsafeMutablePointer = ... // ݱࡏ: destination.assignFrom(UnsafeMutablePointer(source), count: count) // ະိ: destination.assignFrom(source, count: count)

Slide 23

Slide 23 text

SE-0080 failable Numeric initializer • ݱࡏࡏᏐࣈ֨ࣜؒ᫚׵ࣦഊ࣌୞။༗runtime error • ະိ။༗ճၚ nil ൛త᫚׵ɼ ೗Ռෆೳ᫚׵࣌୞။ճ ၚnilɼࣕෆੋࡨޡ

Slide 24

Slide 24 text

SE-0082 Package manager Մमվ త dependency • dependency త source ሡ။ඃ์౸Ұݸᯅ᤽ࢿྉᇄ • ࢖༻ swift build —edit ိબᎩཁमվ త dependencyɼPM ။࢖༻ Packages ࢿྉᇄཫత source ိ࡞ҝdependencyɼ׬શࠌུଞతgit 㐫ଶ ౳౳ɻ

Slide 25

Slide 25 text

SE-0075 Build Configuration Import Test • ৽⃧ՄҎଌࢼ module ೳෆೳඃimport త ޭೳɼݱ ࡏ୞ೳଌࢼ os, arch ࿨ swift ൛ຊ #if canImport(UIKit) // UIKit-based code #elseif canImport(Cocoa) // OSX code #elseif // Workaround/text, whatever #endif

Slide 26

Slide 26 text

Evolution • SE-0081 ೺ where Ҡ౸એࠂ࠷ޙ • SE-0088 ݱ୅Խ libdispatch • SE-0092 typealias in protocol

Slide 27

Slide 27 text

SE-0092 typealias in protocol protocol Sequence { associatedtype Iterator : IteratorProtocol typealias Element = Iterator.Element }

Slide 28

Slide 28 text

SE-0081 ೺ where Ҡ౸એࠂ࠷ޙ • ݱࡏ func anyCommonElements(lhs: T, _ rhs: U) -> Bool • ະိ func anyCommonElements(lhs: T, _ rhs: U) -> Bool where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element

Slide 29

Slide 29 text

SE-0088 ݱ୅Խ libdispatch • ݱࡏ let queue = dispatch_queue_create("com.test.myqueue", nil) dispatch_async(queue) { print("Hello World") } • ະိ let queue = DispatchQueue(label: "com.test.myqueue") queue.asynchronously { print("Hello World") }