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

Swift with apps by Hatena

yashigani
February 07, 2015

Swift with apps by Hatena

Case of swift in Hatena

yashigani

February 07, 2015
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. ͸ͯͳͷ iOSΞϓϦͱSwift @Hatena Engineer Seminar #4

  2. id:yashigani_w @yashigani • http://yashigani.hatenablog.com • 2014/7ೖࣾ • ΞϓϦέʔγϣϯΤϯδχΞ • iOS/Android

    ࣌ʑ Perl
  3. None
  4. ͸ͯͳͰͷࣄྫ

  5. ͸ͯͳϒοΫϚʔΫShare Extension • ΄΅શͯSwiftͰ࣮૷ • Swiftͷ࣮ݧϓϩδΣΫτͱ͍ ͏Ґஔ͚ͮ

  6. ͳͥShare ExtensionͰࢼ͔ͨ͠ • App Extension͸ΞϓϦʹ΋͏Ұͭখ͍͞ΞϓϦΛ όϯυϧ͢Δܗଶ • ΞϓϦͷن໛ͱͯ͠খ͘͞ɼࣦഊͯ͠΋վमָ͕ • SwiftΛࢼ͢ϓϩδΣΫτʹ࠷ద

  7. ݁Ռ • ࠓޙͷϓϩδΣΫτͰ͸ඪ४తʹSwiftΛ࠾༻͢Δ ͜ͱʹܾఆ • طʹࣾ಺޲͚ϥΠϒϥϦͷ͍͔ͭ͘͸SwiftʹҠߦ • Objective-Cॻ͖͍ͨͬͯਓ͸͍ͳ͘ͳͬͨ

  8. ͳͥɼ͋͑ͯखʹೃછΜͩ Objective-CΛغͯSwiftΛ࠾༻͢Δͷ͔

  9. - (Bookmark *)latestHotEntry:(NSArray *)bookmarks { NSPredicate *predicate = [NSPredicate predicateWithBlock:^(Bookmark

    *b, NSDictionary *bindings) { return b.isHotentry; }]; NSArray *hotentries = [bookmarks filteredArrayUsingPredicate:predicate]; NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"bookmarked" ascending:NO]; NSArray *sortedHotentries = [hotentries sortedArrayUsingDescriptors:@[descriptor]]; return sortedHotentries.firstObject; }
  10. func latestHotentry(bookmarks bs: [Bookmark]) -> Bookmark? { return bs.filter {

    $0.isHotentry } .sorted { (b1: Bookmark, b2: Bookmark) in b1.bookmarked.compare(b2.bookmarked) == .OrderedDescending }.first }
  11. Generics • ΑΓ҆શʹίʔυΛॻ͘͜ͱ͕Ͱ͖Δ • Objective-CͰ๷ޚతͳίʔυΛॻ͘ͱ৑௕ • (࣍ཉ͔ͬͨ͠ͷ͸ܕΞϊςʔγϣϯ) • ΑΓॊೈͳίʔυ͕ॻ͚Δ

  12. - (Bookmark *)latestHotEntry:(NSArray *)bookmarks { for (id obj in bookmarks)

    { if (![obj isKindOfClass:Bookmark.class]) { return nil; } } NSPredicate *predicate = [NSPredicate predicateWithBlock:^(Bookmark *b, NSDictionary *bindings) { return b.isHotentry; }]; NSArray *hotentries = [bookmarks filteredArrayUsingPredicate:predicate]; NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"bookmarked" ascending:NO]; NSArray *sortedHotentries = [hotentries sortedArrayUsingDescriptors:@[descriptor]]; return sortedHotentries.firstObject; }
  13. Functional Programming • map, filter, sort etc. • ෳࡶͳ͜ͱΛ׬݁ʹදݱͰ͖Δ •

    ैདྷ͸ख࡞Γײ͕͋ͬͨ • NSPredicate, NSSortDescriptor
  14. Closures • Objective-CͷΤϯδχΞ͸ΈΜͳBlocks͍͖ͩ͢ • (׳ΕΕ͹)ಡΊΔ • (׳Εͯ΋)ͦΒͰ͸ॻ͚ͳ͍ • ແ͔ͬͨ࣌୅ʹ૝͍Λ஘ͤͳ͕Β͋Γ͕ͨ͘࢖͏

  15. int (^add)(int, int) = ^(int x, int y) { return

    x + y; }; add(1, 2); @interface Hoge : NSObject @property (copy) int (^block)(int, int); @property (copy) NSString *title; - (void)fuga:(int(^)(int, int))block; @end Objective-C
  16. fuckingblocksyntax.com

  17. Swift let add = { (x: Int, y: Int) ->

    Int in return x + y } add(1, 2) class Hoge: NSObject { var closure: (Int, Int) -> Int var title: String func fuga(closure: (Int, Int) -> Int) { // ... } }
  18. ΄͔ʹ΋ • Tuples • Optional • Playground(REPL) • namespace(classͷதʹclass/enum/struct͕ॻ͚Δ)

  19. Objective-C͔ΒSwift΁ • ΑΓݫີͳ੩తܕ෇͚ • ख࡞Γ͔Βݴޠαϙʔτ΁ • Cͱͷޓ׵ΛغͯΔ͜ͱʹΑͬͯಘͨϞμϯͳػೳ

  20. ͍͚ͯͳ͍ͱ͜Ζ΋͋Δ • աڈͷࢿ࢈Λͦͷ··࢖͑ͳ͍͜ͱ͕͋Δ • CͰॻ͔Εͨ΋ͷͷҰ෦(ex: CommonCrypto) • C++Ͱॻ͔Εͨ΋ͷ • ࠷దԽͰҊ֎յΕΔ

    • ։ൃπʔϧ
  21. ࠷దԽͰյΕΔྫ(1) var query: [String: AnyObject] = makeKeychainQuery(account: account, type: type)

    var status: OSStatus = errSecSuccess var result: AnyObject? status = withUnsafeMutablePointer(&result, { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }) query[kSecValueData] = passwordData
  22. ࠷దԽͰյΕΔྫ(2) var query: [String: AnyObject] = makeKeychainQuery(account: account, type: type)

    var status: OSStatus = errSecSuccess var result: AnyObject? status = withUnsafeMutablePointer(&result, { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }) query[kSecValueData] = passwordData ࠷దԽ༗ޮ࣌ɼͳ͔ͥ։์͞Εͯ͠·͍ ͦͷޙΞΫηε͢ΔͱΫϥογϡ͢Δ
  23. ࠷దԽͰյΕΔྫ(3) var query: [String: AnyObject] = makeKeychainQuery(account: account, type: type)

    var status: OSStatus = errSecSuccess var result: AnyObject? let temp = query status = withUnsafeMutablePointer(&result, { SecItemCopyMatching(temp, UnsafeMutablePointer($0)) }) query[kSecValueData] = passwordData յΕͯ΋͍͍Α͏ʹҰ࣌ม਺ʹୀආ͢ΔͱճආͰ͖Δ
  24. ࠷దԽ஫ҙϙΠϯτ • TestFlightͳͲͰϕʔλΛ഑෍ͨ͠ͱ͖ʹॳΊͯ
 ؾ͕ͭ͘͜ͱ͕ଟ͍ • σϑΥϧτͰ͸σόοά࣌ʹ࠷దԽΛ͠ͳ͍ • Ͳ͏ߟ͑ͯ΋͓͔͍͠ͱ͜ΖͰΫϥογϡͯͨ͠Β ेதീ۝࠷దԽͰյΕ͍ͯΔ •

    ߄ͯͳ͍Α͏ʹϦϦʔεʹ༨༟Λ࣋ͬͨQAܭը
  25. ϋϚͬͨͱ͖ͷΞυόΠε • σόοά࣮ߦͰ΋࠷దԽϨϕϧΛ্͛Ε͹࠶ݱՄೳ • Ұ࣌ม਺Λ࢖͏(ࠓճͷྫͷΑ͏ͳ৔߹) • Swift෼Λഉআ͢Δ(structΛ࢖Θͳ͍ɼNSObjectΛ ܧঝ͢Δ) • ࠶ݱ͸؆୯ͳͷͰɼఘΊͳ͍ڧ͍৺Λ࣋ͭ

  26. ։ൃπʔϧ͕μϝ • IDEͷػೳ͕΄΅࢖͑ͳ͍ • ΍ͨΒΫϥογϡ͢ΔγϯλοΫεϋΠϥΠτ+ ίʔυิ׬ • ϦϑΝΫλϦϯάػೳ͸ະαϙʔτ • શ͘ಈ͔ͳ͍Θ͚Ͱ͸ͳ͍ͷͰզຫ͢Ε͹࢖͑Δ

  27. ΄͔ʹ΋ • ܕਪ࿦͕͓΋ͬͨΑΓϔϘ͍ • ϦϑϨΫγϣϯ͕·ͩແ͍ • Objective-C͸ٯʹ͜Ε͕ΊͬͪΌڧ͔ͬͨ

  28. SwiftͰiOSΞϓϦΛ ࡞ΔΦεεϝߏ੒

  29. CocoaPods vs Carthage • CocoaPods͸0.36(preview൛)ΑΓSwiftʹରԠ • Carthage͸࠷ۙग़͖ͯͨ΍ͭ • iOS 8͔Β࢖͑Δɼdynamic

    frameworkΛϏϧυ͠ projectʹຒΊΔ • աڈͷࢿ࢈͕࢖͑Δ͜ͱ΍ɼiOS 7ͷαϙʔτ΋ߟ͑Δ ͱ·ͩࠓ͸CocoaPodsΛ࢖͏ͷ͕ແ೉
  30. Alamofire vs AFNetworking • ඒ͍͠SwiftͷੈքͰด͍ͯ͡Δͷ͕Alamofire • ࡉ͔͍͜ͱ(ಛʹmultipartͰ௨৴)͕͍ͨ͠৔߹͸
 ໎ΘͣAFNetworking

  31. JSON • Optionalͱͱʹ͔͘૬ੑ͕ѱ͍ • ૉ๿ʹ΍ΔͱΊͪΌͪ͘Όωετ͕ਂ͘ͳΔ • swift-json΍SwiftyJSONΛ࢖͏͜ͱ΋ݕ౼Ͱ͖Δ ͕ɼMantleΛ࢖͏ͷ͕Φεεϝ

  32. ૉ๿ʹ΍Δͱ͜͏ͳΔ let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(dataFromTwitter, options: NSJSONReadingOptions.MutableContainers, error:

    nil) if let statusesArray = jsonObject as? NSArray { if let aStatus = statusesArray[0] as? NSDictionary { if let user = aStatus["user"] as? NSDictionary { if let userName = user["name"] as? NSString { //Finally We Got The Name } } } }
  33. MantleͳΒ͜͏ let entries = MTLJSONAdapter.modelsOfClass(BookmarkEntry.self, fromJSONArray: JSONEntries, error: nil) return

    entries as [BookmarkEntry]
  34. ͸ͯͳͷ ϞόΠϧΞϓϦΤϯδχΞ

  35. εϚʔτձ • ٕज़ڞ༗/εΩϧΞοϓ + ϞόΠϧΞϓϦ։ൃ؀ڥ ੔උɼ޲্Λ໨తͱͨ͠νʔϜԣஅ૊৫ • ٕज़ڞ༗͸ඇৗʹ׆ൃ • ։ൃ؀ڥʹ͍ͭͯ͸େ͖ͳݖݶ͕༩͑ΒΕ͍ͯΔͷ

    Ͱ΍Δؾ͍ͩ͠Ͱ΍Γ͍ͨΑ͏ʹͰ͖Δ
  36. ͋Γ͕ͱ͏͍͟͝·ͨ͠