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

8889da6a67db3667b0694d993c9a962c?s=128

Yosuke Ishikawa

February 01, 2014
Tweet

Transcript

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

  2. ࣗݾ঺հ

  3. None
  4. GHFeed

  5. IS ISDiskCache ISMemoryCache ISHTTPOperation ISRemoveNull ISPersistentStack ISMethodSwizzling ISBackGesture ISRefreshControl ISColumnsController

    ISRevealController ISCyclicPagesView ISFakeInterfaceOrientation ISInteractiveEdgesNavigationBar ISAlternativeRefreshControl
  6. None
  7. 8 Pods 551 Stars

  8. τʔΫ֓ཁ

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

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

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

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

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

  14. 1.ΞϓϦͱͷিಥ

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

  16. 1.1.γϯϘϧͷিಥ

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

  18. Duplicated Symbol !!!

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

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

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

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

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

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

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

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

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

  29. App Library methodA ish_methodA

  30. 1.1.3.ఆ਺ͷ໋໊نଇ

  31. ✓ ؔ࿈͢ΔΫϥε໊Λ಄ʹ͚ͭΔ extern NSString *const ISHObjectGlobalConstant; ! @interface ISHObject :

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

    @implementation ISHObject ! @end
  33. 1.1.4.Α͋͘ΔγϯϘϧͷিಥ

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

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

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

  37. None
  38. 1.1.Ϧιʔεͷিಥ

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

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

  41. NSBundle *bundle = [NSBundle mainBundle]; NSString *filePath = [bundle pathForResource:@"foo"

    ofType:@"png"]; [UIImage imageWithContentsOfFile:filePath]; NSBundle *bundle = [NSBundle mainBundle]; [bundle localizedStringForKey:@"foo" value:@"" table:nil]; ϝΠϯόϯυϧͷϦιʔεΛಡΜͰ͍Δ
  42. main bundle foo.png foo.png foo.png͸main bundleʹ1ݸ͔͠ଘࡏͰ͖ͳ͍ ? App Library

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

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

  45. None
  46. 2.ྑ͍ @Interface

  47. ✓ Θ͔Γ΍͍͢

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

  49. @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 ✗
  50. @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 ✓
  51. ✓ ྑ͍ @property ✓ ྑ͍௨஌ύλʔϯ τϐοΫ͸ແݶ͋Δ͕ಛʹॏཁͳ2ͭ

  52. 2.1.ྑ͍ @property

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

  54. 1.1.3.readwrite vs readonly

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

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

  57. @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
  58. - (void)setSizeLimit:(NSUInteger)sizeLimit { _sizeLimit = sizeLimit; ! if (self.currentSize >

    sizeLimit) { [self removeAllObjects]; } } currentSize < sizeLimit ΛอͭͨΊʹ͜ͷΑ͏ͳ࣮૷͕ظ଴͞ΕΔ NSCache͸͜͏͍͏ڍಈʹͳ͍ͬͯΔ
  59. readwrite ͍͔ͳΔλΠϛϯάʹมߋ͞Εͯ΋ ΦϒδΣΫτͷ੔߹ੑΛอͯΔ͜ͱ͕๬·͍͠

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

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

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

  63. @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
  64. - (instancetype)initWithSizeLimit:(NSUInteger)sizeLimit { self = [super init]; if (self) {

    _sizeLimit = sizeLimit; } return self; } มߋͰ͖ͳ͍୅ΘΓʹॳظԽ࣌ʹ஋Λ౉͢
  65. ·ͱΊΔͱ

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

  67. None
  68. 2.2.ྑ͍௨஌ύλʔϯ

  69. ✓ blocks ✓ delegate ✓ NSNotification ✓ KVO

  70. 2.2.1.blocks

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

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

    NSError *error) { ; }];
  73. blocksͷߏจ͸ҟ༷ʹෳࡶ

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

  76. 2.2.2.delegate

  77. ✓ ௨஌͕ಛఆͷΦϒδΣΫτʹ݁ͼͭ͘ ✓ ௨஌ͷछྨ͕ଟ͍ ✓ ௨஌ͷड͚ख͕1͔ͭ͠ͳ͍ ద͍ͯ͠Δέʔε @optional ! -

    (void)scrollViewDidScroll:(UIScrollView *)scrollView; - (void)scrollViewDidZoom:(UIScrollView *)scrollView; - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; ໋໊نଇʹ͍ͭͯ͸Cocoa޲͚ίʔσΟϯάΨΠυϥΠϯ, p.15Λࢀর
  78. 2.2.3.NSNotification

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

  80. 2.3.ྑ͍@interface·ͱΊ

  81. ✓ దࡐదॴ readwrite readonly ✓ blocks ✓ delegate ✓ NSNotification

    ✓ KVO
  82. None
  83. 3.Ϣχοτςετ

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

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

  86. @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
  87. - (void)testSetObjectForKey { id object = [[NSObject alloc] init]; id

    key = @"foo"; ! [cache setObject:object forKey:key]; ! XCTAssertEqual(cache.currentSize, 1U); XCTAssertEqualObjects([cache objectForKey:key], object); }
  88. ✓ setObject:forKey:ͷ࣮ߦ࣌ʹsizeLimitΛ௒͑Δ৔߹͸ʁ ✓ setObject:forKey:ͷ࣮ߦޙʹsizeLimitΛখͨ͘͞͠৔߹͸ʁ ✓ removeAllObjectsͷ࣮ߦޙͷcurrentSize͸ʁ ͱ͍͏৚݅ΛσϞΞϓϦ্Ͱຖճσόοά͢Δ͸ඇৗʹͭΒ͍͕ɺ Ϣχοτςετ͸1౓ॻ͚͹܁Γฦ࣮͠ߦͰ͖Δɻ

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

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

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

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

  93. 3.2.CIΛಋೖ͢Δ

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

  95. Git repository CI server Developer git push git hook Build

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

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

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

  99. xcodebuild clean test \ -sdk iphonesimulator \ -workspace ISDiskCache.xcworkspace \

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

  101. CI αʔϏε

  102. Git repository CI server Developer git push git hook Build

    report
  103. GitHub Developer git push git hook Build report

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

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

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

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

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

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

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

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

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

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

  114. None
  115. ·ͱΊ

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