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. - (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; }
  2. func latestHotentry(bookmarks bs: [Bookmark]) -> Bookmark? { return bs.filter {

    $0.isHotentry } .sorted { (b1: Bookmark, b2: Bookmark) in b1.bookmarked.compare(b2.bookmarked) == .OrderedDescending }.first }
  3. - (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; }
  4. Functional Programming • map, filter, sort etc. • ෳࡶͳ͜ͱΛ׬݁ʹදݱͰ͖Δ •

    ैདྷ͸ख࡞Γײ͕͋ͬͨ • NSPredicate, NSSortDescriptor
  5. 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
  6. 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) { // ... } }
  7. ࠷దԽͰյΕΔྫ(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
  8. ࠷దԽͰյΕΔྫ(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 ࠷దԽ༗ޮ࣌ɼͳ͔ͥ։์͞Εͯ͠·͍ ͦͷޙΞΫηε͢ΔͱΫϥογϡ͢Δ
  9. ࠷దԽͰյΕΔྫ(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 յΕͯ΋͍͍Α͏ʹҰ࣌ม਺ʹୀආ͢ΔͱճආͰ͖Δ
  10. CocoaPods vs Carthage • CocoaPods͸0.36(preview൛)ΑΓSwiftʹରԠ • Carthage͸࠷ۙग़͖ͯͨ΍ͭ • iOS 8͔Β࢖͑Δɼdynamic

    frameworkΛϏϧυ͠ projectʹຒΊΔ • աڈͷࢿ࢈͕࢖͑Δ͜ͱ΍ɼiOS 7ͷαϙʔτ΋ߟ͑Δ ͱ·ͩࠓ͸CocoaPodsΛ࢖͏ͷ͕ແ೉
  11. ૉ๿ʹ΍Δͱ͜͏ͳΔ 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 } } } }