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

Swift 週報 5/3

Swift 週報 5/3

Swift 週報 5/3

Johnlin

May 03, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. ෦෼ deprecation ܯࠂঋڃ੒ࡨ ޡ print(__FILE__) @warn_unused_result(message="oh no") func F1() ->

    Int{ return 42 } for var a = 0; a < 3; a+=1 { a } • Ṝࠣࡏ։ᚙ൛తswift ቮៃኺܯࠂঋڃ੒ࡨޡɻҎޙ။࿈compile౎ ෆߦɻ
  2. Evolution • SE-0036 Enum ҰఆཁՃ։಄తᴍ • SE-0048 Generic typealias •

    SE-0049 ሡ @noescape ᢛ @autoclosure Ҡࢸ type એࠂ • SE-0057 import Obj-C generics
  3. SE-0036 Enum ҰఆཁՃ։಄త ᴍ enum Coin : CustomStringConvertible { case

    heads, tails var description: String { switch self { case .heads: return “heads" //༗Ճᴍ case tails: return “tails" //ᔒՃᴍ } } } print(Coin.heads) • ҎޙҰఆཁՃ։಄తᴍɼ֬อҰகੑ
  4. SE-0048 Generic typealias typealias StringDic = Dictionary<String, String> let dic:StringDic

    = ["key" : "value"] • typealias ሡ။෦෼ࢧԉ Generic typealias StringDictionary<T> = Dictionary<String, T> typealias DictionaryOfStrings<T : Hashable> = Dictionary<T, String>
  5. SE-0049 ሡ @noescape ᢛ @autoclosure Ҡࢸ type એࠂ • ݱࡏ

    func fn(@noescape f : () -> () ) {} func fa(@autoclosure f : () -> () ) {} • ະိ func fn(f : @noescape () -> () ) {} func fa(f : @autoclosure () -> () ) {} • ௥ᬋ inout త䳾㑊
  6. SE-0057 import Obj-C generics • @interface MySet<T : id<NSCopying>> :

    NSObject
 -(MySet<T> *)unionWithSet:(MySet<T> *)otherSet;
 @end
 
 @interface MySomething : NSObject
 - (MySet<NSValue *> *)valueSet;
 @end • ။੒ҝ • class MySet<T : NSCopying> : NSObject {
 func unionWithSet(otherSet: MySet<T>) -> MySet<T>
 }
 
 class MySomething : NSObject {
 func valueSet() -> MySet<NSValue>
 }
  7. Evolution • SE-0062 Obj-C key path syntax • SE-0063 Swift

    PM ܥ౷ module search path • SE-0064 ༻ #selector ိࢦ޲ getter & setter • SE-0065 Collections & Indices ৽໛ܕ • SE-0068 ༻ Self ိऔ୅ dynamicType
  8. SE-0062 Obj-C key path syntax class Person: NSObject { dynamic

    var name:String = "" init(name: String){ self.name = name } } let john = Person(name: "John") john.valueForKey("name") //Optional(John) john.valueForKey(#keypath(Person.name)) //Optional(John) • ᩋฤᩄث㢨๩ᒾҰ༗ᔒ༗ଧࡨࣈ
  9. SE-0063 Swift PM ܥ౷ module search path • ໨લSwift ՄҎ

    import C Libraryɼୠੋधཁፙ౸ include 䈕(*.h) Ґஔ • ະိሡՄҎࣗಈᩇऔ .pc 䈕ိፙ౸ include 䈕Ґஔ • ໵။ࢧԉܥ౷ package manager( Homebrew & Apt)
  10. SE-0064 ༻ #selector ိࢦ޲ getter & setter • #selector ՄҎ༻ိऔ୅

    Obj-C త @selector ɻୠੋ ؐແ๏ࢧԉ getter & setter • ະိՄҎऔಘgetter & setter త selector • let firstNameGetter = #selector(getter: Person.firstName)
 let firstNameSetter = #selector(setter: Person.firstName)
  11. SE-0065 Collections & Indices ৽໛ܕ • Collection ඃॏ৽ઃܭɼIndex( ForwardIndex, BidirectionalIndex,

    RandomAccessIndex) ඃফ໓ ྃɻ • Index ૬᮫తޭೳ။ඃҠਐ Collection தɻ૾ੋ idx.successor() ။ඃ collection.index(after: idx) औ୅ • ଖଞෆडӨڹ( index(of:), min(), sort() ౳౳ )
  12. SE-0068 ༻ Self ိऔ୅ dynamicType • ݱࡏ Self ᮫伴ࣈੋڅ Protocol

    ༻త • protocol Copyable {
 init(copy:Self)
 } • ະိՄҎࡏ class ཫᙛ੒ self.dynamicType ༻ • struct MyStruct {
 static func staticMethod() { ... }
 func instanceMethod() {
 self.dynamicType.staticMethod() //ݱࡏ
 Self.staticMethod() //ະိ
 }
 }