Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift weekly 5/24

swift weekly 5/24

Johnlin

May 24, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. Swift 3.0 ᚙ෍ྲྀఔ • Swift 3.0 ᔒ༗޲Լ૬༰ Swift 2.2 •

    ሡ။ੋୈҰݸแؚ Package Manager త൛ຊ • ༬ܭ2016Լ൒೥ਖ਼ࣜެ䆋 • preview1 branchቮࡏ5/12૑ཱ ɼ༬ܭ4-6पޙ releaseʢWWDC 2016?)
  2. ᔟܕએݴ (Generic Manifesto) • Swift తᔟܕؐ༗ਐ㑊తۭؒɼᔟܕએݴྻग़ྃҰࠣ ڞಉత໨ඪ • Ҡআෆඞཁతݶ੍: recursive

    protocol, nested generics • ᩋߋଟతએࠂՄҎ༗ᔟܕჩᏐ: typealiases, subscripts, constant, extension
  3. ᔟܕએݴ (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<…>
  4. Evolution • SE-0017 ኑআ Unmanaged ɼվ༻ UnsafePointer • SE-0032 Sequence

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

    ሡ။ඃҠ আɼվ༻ UnsafePointer औ୅ func fromOpaque(value: UnsafePointer<Void>) -> Unmanaged func toOpaque() -> UnsafeMutablePointer<Void>
  6. 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}!
  7. 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]
  8. 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 }
  9. SE-0061 autoreleasepool() ሡ။༗ճၚᆴ࿨ࡨޡ႔ཧ • ݱࡏతautoreleasepool ᔒ༗ճၚᆴ࿨ࡨޡ႔ཧ • ະိ။༗ճၚᆴ࿨ࡨޡ႔ཧ func doWork()

    throws -> Result { return try autoreleasepool { ... actual computation which either returns or throws ... } }
  10. SE-0066 വᏐܕผჩᏐҰఆཁ༗ ׅᥒ • ݱࡏവᏐతჩᏐՄҎᔒ༗ׅᥒ (String) -> Int String ->

    Int • ୠ༗࣌။ࠞᔿ (Int, Float) -> Int // ੋၷݸჩᏐ䏆ʁؐੋҰݸ༗ၷݸݩૉత Tuple ჩ Ꮠ? • ະိҰఆཁ༗ׅᥒ
  11. SE-0067 ڧԽ FloatingPoint protocol • Float ݱ༗త function ૬ᙛ༗ݶ •

    ⃧Ճ add, negate, subtract, multiply, divide, remainder, squareRoot ౳౳
  12. SE-0069 Immutable Foundation Types • Foundation Typeฒᔒ༗޷޷త࢖༻ Swift త Value

    Type ޭೳ • ڐଟ NS* త class ሡ။༗Ұݸ Struct ൛తძ๔଻ɼ ൺํ㘸 NSURL ሡ။༗Ұݸ URL త Struct ൛ɻ
  13. Evolution • SE-0070 Optional protocol method લҰఆཁՃ @objc • SE-0071

    enum ՄҎ༻زݷॴ༗త keyword ိ໋໊ • SE-0072 ׬શҠআ implicit bridging • SE-0060 ༗༬ઃᆴతჩᏐؐੋཁ҈রॱং
  14. 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' }
  15. 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)
  16. SE-0072 ׬શҠআ implicit bridging • ݱࡏࡏ෦෼తfunction தɼSwift type ။ࣗಈ᫚੒ ObjC

    type
 let s : String = "hello" let nss : NSString = NSString(format: "%@", s) • ະိҰఆཁखಈ༻ as ိ᫚ܕ
  17. Evolution • SE-0076 ኺ UnsafePointer ෳ੡ࢿྉ౸ UnsafeMutablePointer • SE-0080 failable

    Numeric initializer • SE-0082 Package manager Մमվత dependency • SE-0075 Build Configuration Import Test
  18. SE-0076 ኺ UnsafePointer ෳ੡ࢿ ྉ౸ UnsafeMutablePointer • ݱࡏ୞ೳࡏUnsafeMutablePointer ೭ؒෳ੡ࢿྉɼ ৽⃧ኺ

    UnsafePointer ෳ੡ࢿྉ౸ UnsafeMutablePointer తޭೳ let source: UnsafePointer<Int> = ... let destination: UnsafeMutablePointer<Int> = ... // ݱࡏ: destination.assignFrom(UnsafeMutablePointer(source), count: count) // ະိ: destination.assignFrom(source, count: count)
  19. SE-0082 Package manager Մमվ త dependency • dependency త source

    ሡ။ඃ์౸Ұݸᯅ᤽ࢿྉᇄ • ࢖༻ swift build —edit <PACKAGE> ိબᎩཁमվ త dependencyɼPM ။࢖༻ Packages ࢿྉᇄཫత source ိ࡞ҝdependencyɼ׬શࠌུଞతgit 㐫ଶ ౳౳ɻ
  20. 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
  21. SE-0092 typealias in protocol protocol Sequence { associatedtype Iterator :

    IteratorProtocol typealias Element = Iterator.Element }
  22. SE-0081 ೺ where Ҡ౸એࠂ࠷ޙ • ݱࡏ func anyCommonElements<T : SequenceType,

    U : SequenceType where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element>(lhs: T, _ rhs: U) -> Bool • ະိ func anyCommonElements<T : SequenceType, U : SequenceType>(lhs: T, _ rhs: U) -> Bool where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element
  23. 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") }