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

Objective-C Programming Guide for Open Source Software

Objective-C Programming Guide for Open Source Software

2014/02/01 @ GREE

Yosuke Ishikawa

February 01, 2014
Tweet

More Decks by Yosuke Ishikawa

Other Decks in Technology

Transcript

  1. ެ։༻ͷObjective-Cͷॻ͖ํ
    @_ishkawa

    View full-size slide

  2. IS
    ISDiskCache
    ISMemoryCache
    ISHTTPOperation
    ISRemoveNull
    ISPersistentStack
    ISMethodSwizzling
    ISBackGesture
    ISRefreshControl
    ISColumnsController
    ISRevealController
    ISCyclicPagesView
    ISFakeInterfaceOrientation
    ISInteractiveEdgesNavigationBar
    ISAlternativeRefreshControl

    View full-size slide

  3. 8 Pods
    551 Stars

    View full-size slide

  4. “ެ։༻”
    | |
    ଞਓ΋࢖͏ίʔυ

    View full-size slide

  5. ࣗ෼͔͠৮Δ͜ͱͷͳ͍ΞϓϦͷ
    ίʔυ͸޷͖ͳΑ͏ʹॻ͚͹͍͍
    (ࣗݾ੹೚Ͱ)

    View full-size slide

  6. ͔͠͠ɺଞਓ΋৮ΔՄೳੑ͕͋Δ৔߹͸

    View full-size slide

  7. ✓ ୭Ͱ΋؆୯ʹಋೖͰ͖Δ
    ✓ ୭Ͱ΋ਖ਼͘͠ѻ͏͜ͱ͕Ͱ͖Δ
    ✓ ଞͷίʔυ΁ͷ෭࡞༻͕ͳ͍
    ✓ γϯϘϧ΍Ϧιʔε͕িಥ͠ͳ͍
    ͱ͍ͬͨ͜ͱ͕ٻΊΒΕΔͷͰɺࠓ೔͸ͦͷ࿩Λ͠·͢ɻ

    View full-size slide

  8. 1.ΞϓϦͱͷিಥ
    2.ྑ͍@interface
    3.Ϣχοτςετ
    τʔΫ֓ཁ

    View full-size slide

  9. 1.ΞϓϦͱͷিಥ

    View full-size slide

  10. 2छྨͷিಥ
    ✗ γϯϘϧͷিಥ
    ✗ Ϧιʔεͷিಥ

    View full-size slide

  11. 1.1.γϯϘϧͷিಥ

    View full-size slide

  12. ✗ Ϋϥεͷিಥ
    ✗ ΧςΰϦͷϝιουͷিಥ
    ✗ ఆ਺ͷিಥ

    View full-size slide

  13. Duplicated Symbol !!!

    View full-size slide

  14. γϯϘϧͷিಥ͸໋໊نଇͰ๷͙͜ͱ͕Ͱ͖Δ
    ʢେ఍ͷ৔߹ʣ

    View full-size slide

  15. 1.1.1.Ϋϥεͷ໋໊نଇ

    View full-size slide

  16. ✓ 3จࣈͷେจࣈprefixΛ͚ͭΔ
    @interface ISHObject : NSObject
    !
    @end

    View full-size slide

  17. Objective-CʹΑΔϓϩάϥϛϯά (p.127)
    “ಠࣗͷΫϥεʹ͸3จࣈͷϓϨϑΟοΫεΛ͚͍ͭͯͩ͘͞ɻ ձ໊ࣾ
    ΍ΞϓϦέʔγϣϯ໊ʹͪͳΜͩ΋ͷͰ΋ɺॏཁͳίϯϙʔωϯτʹ
    ༝དྷ͢Δ΋ͷͰ΋ߏ͍·ͤΜɻ”
    “ͨͱ͑͹ɺձ໊͕ࣾWhispering Oakɺ։ൃ͢ΔήʔϜͷ໊લ͕Zebra
    SurpriseͰ͋Ε͹ɺ WZS΍WOZͱ͍ͬͨϓϨϑΟοΫεʹ͢ΔͱΑ͍
    ͔΋͠Ε·ͤΜɻ”

    View full-size slide

  18. Objective-CʹΑΔϓϩάϥϛϯά (p.126)
    “2จࣈͷϓϨϑΟοΫε͸ɺϑϨʔϜϫʔΫΫϥεʹ࢖͏ͨΊɺ
    Apple͕༧໿͍ͯ͠·͢ɻ”
    ✗ IS
    ✓ NJK

    View full-size slide

  19. 1.1.2.ΧςΰϦͷϝιουͷ໋໊نଇ

    View full-size slide

  20. ✓ 3จࣈͷখจࣈprefixΛ͚ͭΔ
    @interface NSObject (ISHAdditions)
    !
    - (void)ish_additionalMethod;
    !
    @end

    View full-size slide

  21. Objective-CʹΑΔϓϩάϥϛϯά (p.74)
    “Ϋϥε໊ͷϓϨϑΟοΫεͱಉ͡3จࣈͷจࣈྻΛɺϝιου໊
    ͷن໿ʹैͬͯখจࣈʹ͠ɺΞϯμʔείΞΛ͸͞ΜͰϝιου
    ໊ͱ࿈͍݁ͯͩ͘͠͞ɻ”
    “ಈ࡞͕ෆఆʹͳΔ͜ͱΛճආ͢ΔͨΊɺϑϨʔϜϫʔΫΫϥε
    ʹΧςΰϦΛఆٛͯ͠ϝιουΛ௥Ճ ͢Δࡍ͸ɺ໊લʹϓϨ
    ϑΟοΫεΛ͚ͭΔΑ͏ʹ͢ΔͱΑ͍Ͱ͠ΐ͏ɻ”

    View full-size slide

  22. App Library
    methodA methodA
    ͲͪΒͷmethodAͷ࣮૷͕࣮ߦ͞ΕΔͷ͔ෆఆ

    View full-size slide

  23. App Library
    methodA ish_methodA

    View full-size slide

  24. 1.1.3.ఆ਺ͷ໋໊نଇ

    View full-size slide

  25. ✓ ؔ࿈͢ΔΫϥε໊Λ಄ʹ͚ͭΔ
    extern NSString *const ISHObjectGlobalConstant;
    !
    @interface ISHObject : NSObject
    !
    @end
    ISHObject.h
    ISHObject.m
    NSString *const ISHObjectGlobalConstant = @"Foo";
    !
    @implementation ISHObject
    !
    @end

    View full-size slide

  26. ✓ ֎෦͔Βෆཁͳఆ਺͸ඇެ։ʹ͢Δ
    ISHObject.m
    static NSString *const ISHLocalConstant = @"Foo";
    !
    @implementation ISHObject
    !
    @end

    View full-size slide

  27. 1.1.4.Α͋͘ΔγϯϘϧͷিಥ

    View full-size slide

  28. ϥΠϒϥϦ͕ґଘ͢ΔϥΠϒϥϦ͕2ॏʹϏϧυλʔήοτʹ௥Ճ͞Ε͍ͯΔ
    Library A Library B
    Library C Library C

    View full-size slide

  29. ϥΠϒϥϦ͕ϥΠϒϥϦʹґଘ͢Δ͜ͱѱ͍͜ͱͰ͸
    ͳ͍͕ɺར༻ଆͰিಥ͕ى͖ͳ͍Α͏ʹཹҙ͢Δ΂͖

    View full-size slide

  30. ✓ ֎෦ͷϥΠϒϥϦʹґଘ͢Δ͜ͱΛ໌ه͢Δ
    ✓ CocoaPodsͳͲͷґଘ؅ཧΛਪ঑͢Δ

    View full-size slide

  31. 1.1.Ϧιʔεͷিಥ

    View full-size slide

  32. ద౰ʹϥΠϒϥϦͷϦιʔεΛѻ͏ͱ
    ར༻͢ΔΞϓϦͷϦιʔεͱিಥ͢Δ

    View full-size slide

  33. [UIImage imageNamed:@"Foo"];
    NSLocalizedString(@"Foo", nil);

    View full-size slide

  34. NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"foo" ofType:@"png"];
    [UIImage imageWithContentsOfFile:filePath];
    NSBundle *bundle = [NSBundle mainBundle];
    [bundle localizedStringForKey:@"foo" value:@"" table:nil];
    ϝΠϯόϯυϧͷϦιʔεΛಡΜͰ͍Δ

    View full-size slide

  35. main bundle
    foo.png foo.png
    foo.png͸main bundleʹ1ݸ͔͠ଘࡏͰ͖ͳ͍
    ?
    App Library

    View full-size slide

  36. Main bundle
    foo.png
    foo.png
    ϥΠϒϥϦ༻ʹbundleΛͭ͘Ε͹ղܾ
    Library bundle

    View full-size slide

  37. ✓ ϝΠϯόϯυϧʹΞΫηε͢Δ΋ͷʹ஫ҙ
    ✓ Ϧιʔε͸ϥΠϒϥϦઐ༻ͷNSBundleʹೖΕΔ

    View full-size slide

  38. 2.ྑ͍ @Interface

    View full-size slide

  39. ✓ Θ͔Γ΍͍͢

    View full-size slide

  40. ✓ ࣮૷ΛਪଌͰ͖Δ
    ✓ ޡ༻Λͤ͞ͳ͍

    View full-size slide

  41. @interface ISHCache : NSObject
    !
    @property (nonatomic) NSUInteger size;
    @property (nonatomic) NSUInteger limit;
    !
    - (id)get:(id)key;
    - (void)set:(id)object key:(id)key;
    - (void)remove:(id)key;
    - (void)removeAll;
    !
    @end

    View full-size slide

  42. @interface ISHCache : NSObject
    !
    @property (nonatomic, readonly) NSUInteger currentSize;
    @property (nonatomic, readwrite) NSUInteger sizeLimit;
    !
    - (id)objectForKey:(id)key;
    - (void)setObject:(id)object forKey:(id)key;
    - (void)removeObjectForKey:(id)key;
    - (void)removeAllObjects;
    !
    @end

    View full-size slide

  43. ✓ ྑ͍ @property
    ✓ ྑ͍௨஌ύλʔϯ
    τϐοΫ͸ແݶ͋Δ͕ಛʹॏཁͳ2ͭ

    View full-size slide

  44. 2.1.ྑ͍ @property

    View full-size slide

  45. ✓ มߋͷՄ൱Λ໌֬ʹ͢Δ
    ✓ ϥΠϒϥϦͷར༻ʹඞཁͳ৘ใ͸༩͑Δ
    ✓ ϥΠϒϥϦͷར༻ʹෆཁͳ৘ใΛ༩͑ͳ͍

    View full-size slide

  46. 1.1.3.readwrite vs readonly

    View full-size slide

  47. readwrite
    ✓ มߋ͢Δඞཁ͕͋ΔϓϩύςΟ

    View full-size slide

  48. readwrite
    ͍͔ͳΔλΠϛϯάʹมߋ͞Εͯ΋
    ΦϒδΣΫτͷ੔߹ੑΛอͯΔ͜ͱ͕๬·͍͠

    View full-size slide

  49. @interface ISHCache : NSObject
    !
    @property (nonatomic, readonly) NSUInteger currentSize;
    @property (nonatomic, readwrite) NSUInteger sizeLimit;
    !
    - (id)objectForKey:(id)key;
    - (void)setObject:(id)object forKey:(id)key;
    - (void)removeObjectForKey:(id)key;
    - (void)removeAllObjects;
    !
    @end
    currentSize < sizeLimit

    View full-size slide

  50. - (void)setSizeLimit:(NSUInteger)sizeLimit
    {
    _sizeLimit = sizeLimit;
    !
    if (self.currentSize > sizeLimit) {
    [self removeAllObjects];
    }
    }
    currentSize < sizeLimit ΛอͭͨΊʹ͜ͷΑ͏ͳ࣮૷͕ظ଴͞ΕΔ
    NSCache͸͜͏͍͏ڍಈʹͳ͍ͬͯΔ

    View full-size slide

  51. readwrite
    ͍͔ͳΔλΠϛϯάʹมߋ͞Εͯ΋
    ΦϒδΣΫτͷ੔߹ੑΛอͯΔ͜ͱ͕๬·͍͠

    View full-size slide

  52. ͜͏͍࣮ͬͨ૷͕໘౗Ͱɺ
    ͦ΋ͦ΋มߋ͢Δඞཁ΋ͳ͍৔߹͸?

    View full-size slide

  53. readonly
    ✓ มߋ͢Δඞཁ͕ͳ͍ϓϩύςΟ
    ✓ มߋΛېࢭ͍ͨ͠ϓϩύςΟ

    View full-size slide

  54. readonly
    มߋΛؾʹ͠ͳͯ͘ࡁΉ

    View full-size slide

  55. @interface ISHCache : NSObject
    !
    @property (nonatomic, readonly) NSUInteger currentSize;
    @property (nonatomic, readonly) NSUInteger sizeLimit;
    !
    - (instancetype)initWithSizeLimit:(NSUInteger)sizeLimit;
    !
    - (id)objectForKey:(id)key;
    - (void)setObject:(id)object forKey:(id)key;
    - (void)removeObjectForKey:(id)key;
    - (void)removeAllObjects;
    !
    @end

    View full-size slide

  56. - (instancetype)initWithSizeLimit:(NSUInteger)sizeLimit
    {
    self = [super init];
    if (self) {
    _sizeLimit = sizeLimit;
    }
    return self;
    }
    มߋͰ͖ͳ͍୅ΘΓʹॳظԽ࣌ʹ஋Λ౉͢

    View full-size slide

  57. readwrite
    ✓ มߋ͢Δඞཁ͕͋ΔϓϩύςΟ
    ✓ มߋ͞Εͯ΋੔߹ੑ͕อͨΕΔ
    readonly
    ✓ มߋ͢Δඞཁ͕ͳ͍ϓϩύςΟ
    ✓ มߋΛېࢭ͍ͨ͠ϓϩύςΟ

    View full-size slide

  58. 2.2.ྑ͍௨஌ύλʔϯ

    View full-size slide

  59. ✓ blocks
    ✓ delegate
    ✓ NSNotification
    ✓ KVO

    View full-size slide

  60. 2.2.1.blocks

    View full-size slide

  61. ✓ ௨஌͕ಛఆͷϝιουʹ݁ͼͭ͘
    ✓ ௨஌ͷछྨ͕গͳ͍৔߹
    ద͍ͯ͠Δέʔε
    UIDocument *document;
    [document openWithCompletionHandler:^(BOOL success) {
    // completed
    }];

    View full-size slide

  62. ✓ Ҿ਺ʹ͸݁ՌΛ౉͢
    ✓ Τϥʔ͕૝ఆ͞ΕΔ৔߹͸NSError΋౉͢
    ࡞๏
    GKTurnBasedMatch *match;
    [match acceptInviteWithCompletionHandler:^(GKTurnBasedMatch *match, NSError *error) {
    ;
    }];

    View full-size slide

  63. blocksͷߏจ͸ҟ༷ʹෳࡶ

    View full-size slide

  64. ✓ http://fuckingblocksyntax.com
    ✓ Ѫ͕͋Ε͹໰୊ͳ͍

    View full-size slide

  65. 2.2.2.delegate

    View full-size slide

  66. ✓ ௨஌͕ಛఆͷΦϒδΣΫτʹ݁ͼͭ͘
    ✓ ௨஌ͷछྨ͕ଟ͍
    ✓ ௨஌ͷड͚ख͕1͔ͭ͠ͳ͍
    ద͍ͯ͠Δέʔε
    @optional
    !
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView;
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
    ໋໊نଇʹ͍ͭͯ͸Cocoa޲͚ίʔσΟϯάΨΠυϥΠϯ, p.15Λࢀর

    View full-size slide

  67. 2.2.3.NSNotification

    View full-size slide

  68. ✓ ௨஌ͷड͚ख͕ଟ͍
    ✓ ௨஌ͷૹ৴ݩͱड͚ख͕ԕ͍ͱ͖
    ద͍ͯ͠Δέʔε
    άϩʔόϧͳ௨஌ʹͳΔͷͰ҆қʹ࠾༻ͤͣɺ
    ઌʹdelegate, BlocksͰ࣮ݱͰ͖ͳ͍͔ݕ౼͢Δɻ

    View full-size slide

  69. 2.3.ྑ͍@interface·ͱΊ

    View full-size slide

  70. ✓ దࡐదॴ
    readwrite
    readonly
    ✓ blocks
    ✓ delegate
    ✓ NSNotification
    ✓ KVO

    View full-size slide

  71. 3.Ϣχοτςετ

    View full-size slide

  72. 3.1.ςετΛॻ͘ཧ༝

    View full-size slide

  73. ςετΛॻ͍ͨํ͕։ൃ͕଎͍Օॴ͕͋Δ
    (σϞΞϓϦͰσόοά͢ΔΑΓ΋)

    View full-size slide

  74. @interface ISHCache : NSObject
    !
    @property (nonatomic, readonly) NSUInteger currentSize;
    @property (nonatomic, readwrite) NSUInteger sizeLimit;
    !
    - (id)objectForKey:(id)key;
    - (void)setObject:(id)object forKey:(id)key;
    - (void)removeObjectForKey:(id)key;
    - (void)removeAllObjects;
    !
    @end

    View full-size slide

  75. - (void)testSetObjectForKey
    {
    id object = [[NSObject alloc] init];
    id key = @"foo";
    !
    [cache setObject:object forKey:key];
    !
    XCTAssertEqual(cache.currentSize, 1U);
    XCTAssertEqualObjects([cache objectForKey:key], object);
    }

    View full-size slide

  76. ✓ setObject:forKey:ͷ࣮ߦ࣌ʹsizeLimitΛ௒͑Δ৔߹͸ʁ
    ✓ setObject:forKey:ͷ࣮ߦޙʹsizeLimitΛখͨ͘͞͠৔߹͸ʁ
    ✓ removeAllObjectsͷ࣮ߦޙͷcurrentSize͸ʁ
    ͱ͍͏৚݅ΛσϞΞϓϦ্Ͱຖճσόοά͢Δ͸ඇৗʹͭΒ͍͕ɺ
    Ϣχοτςετ͸1౓ॻ͚͹܁Γฦ࣮͠ߦͰ͖Δɻ

    View full-size slide

  77. ίʔυ͕յΕ͍ͯΔ͜ͱΛ͙͢ʹൃݟͰ͖Δ
    ௕ظʹΘͨͬͯෳ਺ਓ͕ϝϯςφϯε͢ΔϓϩάϥϜ͸յΕ΍͍͢
    ςετ͸ϓϩάϥϜ͕ਖ਼͍͜͠ͱͷূ໌Ͱ͸ͳ͍

    View full-size slide

  78. ✓ setObject:forKey:ͷ࣮ߦ࣌ʹsizeLimitΛ௒͑Δ৔߹͸ʁ
    ✓ setObject:forKey:ͷ࣮ߦޙʹsizeLimitΛখͨ͘͞͠৔߹͸ʁ
    ✓ removeAllObjectsͷ࣮ߦޙͷcurrentSize͸ʁ
    ͲΕ͔1͚ͭͩյΕͨ͜ͱΛσϞΞϓϦ্Ͱ
    ݕ஌͢Δ͜ͱ͸೉͍͕͠ɺϢχοτςετͳΒ؆୯

    View full-size slide

  79. ϥΠϒϥϦͷςετͷϝϦοτ͸
    ίετʹݟ߹͏͜ͱ͕ଟ͍
    ΞϓϦͱൺ΂ͯ࢓༷มߋ͕͘͢ͳ͍ͷͰɺ
    ςετίʔυ͕ΰϛʹͳΔ֬཰͕௿͍

    View full-size slide

  80. ϥΠϒϥϦʹର͢ΔϢχοτςετΛॻ͘ཧ༝͸े෼

    View full-size slide

  81. 3.2.CIΛಋೖ͢Δ

    View full-size slide

  82. ͔ͤͬ͘ςετΛॻ͍ͨͷͰ࣮֬ʹ࣮ߦ͍ͨ͠
    git pushͷલʹඞͣςετΛ࣮ߦͰ͖Δਓؒ͸গͳ͍

    View full-size slide

  83. Git repository CI server
    Developer
    git push git hook
    Build report
    CI

    View full-size slide

  84. git push͢Δͱ΋Εͳ͘Ϩϙʔτ͕΋Β͑Δ࢓૊Έ
    ✓ ϏϧυͰ͖Δ͔ʁ
    ✓ ςετ͸௨͔ͬͨʁ
    ✓ ίʔυΧόϨοδ͸ʁ

    View full-size slide

  85. 3.2.1.CIͷηοτΞοϓ

    View full-size slide

  86. ࠷୹ϧʔτ: λʔϛφϧ͔ΒϏϧυͰ͖ΔΑ͏ʹ

    View full-size slide

  87. xcodebuild clean test \
    -sdk iphonesimulator \
    -workspace ISDiskCache.xcworkspace \
    -scheme ISDiskCache \
    -configuration Debug \
    -destination "name=iPhone Retina (4-inch),OS=7.0"
    ཁ͢ΔʹΫϦʔϯͯ͠ςετͯ͠ͶͬͯίϚϯυ
    ͋ͱ͸͜ΕΛCIαʔόʔʹ఻͑Δ͚ͩͰOK

    View full-size slide

  88. 3.2.2.WebαʔϏε

    View full-size slide

  89. Git repository CI server
    Developer
    git push git hook
    Build report

    View full-size slide

  90. GitHub
    Developer
    git push git hook
    Build report

    View full-size slide

  91. ✓ public repository͸ແྉ
    ✓ GitHub࿈ܞ
    ✓ status badge

    View full-size slide

  92. ֘౰ίϛοτͷϏϧυͷঢ়ଶ͕Θ͔Δ !!!

    View full-size slide

  93. ίʔυΧόϨοδදࣔαʔϏε

    View full-size slide

  94. ֘౰ίϛοτͷίʔυΧόϨοδ͕Θ͔Δ

    View full-size slide

  95. GitHub
    Developer
    git push git hook
    Build report
    Coverage report

    View full-size slide

  96. ✓ public repository͸ແྉ
    ✓ GitHub࿈ܞ
    ✓ status badge

    View full-size slide

  97. ྆ํηοτΞοϓ͢Δͱ͜Μͳײ͡

    View full-size slide

  98. https://github.com/ishkawa/ISDiskCache/

    View full-size slide

  99. 3.3.Ϣχοτςετ·ͱΊ

    View full-size slide

  100. ✓ ςετͷಋೖͰ։ൃ͕଎͘ͳΔՕॴ΋͋Δ
    ✓ յΕͨՕॴΛൃݟ͠΍͘͢ͳΔ
    ✓ ϥΠϒϥϦͷςετ͸ΞϓϦͷςετΑΓߴCP
    ✓ CIͰৗʹςετ͞Εͨঢ়ଶΛอͭ͜ͱ͕Ͱ͖Δ

    View full-size slide

  101. 1.ಋೖઌͷΞϓϦͱͷিಥʹؾΛ͚ͭΔ
    2.ଞਓʹΘ͔Γ΍͍͢ΠϯλʔϑΣʔεʹ͢Δ
    3.඼࣭͕མͪʹ͍͘࢓૊ΈΛಋೖ͢Δ
    ϥΠϒϥϦΛެ։͢Δͱ͖͸….

    View full-size slide