ãiPhone/iOSã®éçºããã¦ããä¸ã§ãObjective-Cã§Private Methodã«ã¢ã¯ã»ã¹ããããªãã¨æããã¨ãããã¨æãã¾ãããããªãã¯ã¡ã½ããã«ã¯ãå¤é¨ããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ã¿ãå®è£ ãããã¸ãã¯é¨åã¯ãã©ã¤ãã¼ãã¡ã½ããã«é è½ããã¨ãã£ãã±ã¼ã¹ã¯å¤ã ããã¨æãã¾ãããã®å ´åãã¦ããããã¹ãã¯ãã©ã¤ãã¼ãã¡ã½ããã«å¯¾ãã¦è¡ãå¿ è¦ãããã¾ãã
ãããããObjective-Cã§ã®ãã©ã¤ãã¼ãã¡ã½ããã¯ãã©ãå®è£ ããã®ã§ããããï¼å®ã¯Objective-Cã§ã¯ãå³å¯ãªæå³ã§ã®ãã©ã¤ãã¼ãã¡ã½ããã¯åå¨ããªãã®ã§ããä¸è¬çã«ã¯ã«ãã´ãªã¼ã¨ããæ©è½ã使ã£ã¦ãå®è£ ãã¡ã¤ã«ï¼.mãã¡ã¤ã«ï¼ã«å®£è¨ãã¦ä»£ç¨ãã¾ãã次ã®ãµã³ãã«ã®ãããªæãã§ãã
#import "ExampleClass.h" @interface ExampleClass(private) - (void) privateMethod; - (void) privateMethod:(NSString *)aString; - (void) privateMethod:(NSString *)aString second:(NSString *)bString; @end @implementation ExampleClass @synthesize exampleProperty = _exampleProperty; - (void)publicMethod { _exampleProperty = @"a"; } - (void)privateMethod { _exampleProperty = @"b"; }
ãï¼è¡ç®ã®(private)ã¨ããé¨åããã«ãã´ãªã¼ã§ããæ
£ç¿çã«privateã«ãã¦ãã¾ãããhogehogeã§ãfugefugeã§ãä½ã§ãæ§ããªãã®ã§ãããããObjective-Cã®ããããã©ã¤ãã¼ãã¡ã½ããã¨å¼ãã§ãã¾ããã§ã¯ãããããã¹ãããã«ã¯ã©ããããããã®ã§ããããï¼
ãçããä½ã¨ãªãç¥ã£ã¦ããã¨æãã¾ãããObjectiveCã§ã¯å®è¡æã«ãã¤ãããã¯ã«çµåãããã®ã§ããããã¼ã§å®ç¾©ããã¦ããªãã¡ã½ããã«å¯¾ãã¦ãå¼ã³åºããåºæ¥ã¾ãããã ãwarningãåºãã ãã§ããä¸ã®å³ã®ãããªæãã«ãªãã¾ããã
ãããããWarningãæ¾ç½®ããã®ã¯ãç²¾ç¥è¡çä¸ãä¿å®æ§ãã®ä»ãèãã¦ãè¯ãç¿æ £ã§ã¯ãªãã§ããä½ã¨ããããã¨ããã§ãã主ã«3ã¤ã®æ¹æ³ãããã®ã§ãç°¡åã«ã¾ã¨ãã¦ã¿ã¾ãã
ï¼ï¼performSelectorã使ãæ¹æ³
ï¼ï¼Key-Value Codingã使ã£ã¦ãããããã£ã«ã¢ã¯ã»ã¹ããæ¹æ³
ï¼ï¼ã¯ã©ã¹ãæ¡å¼µãã¦ãã¡ã½ããã¨ããããã£ãå®ç¾©ããæ¹æ³
performSelectorã使ãæ¹æ³
ãperformSelectorã¨ããã¡ã½ããã使ããã»ã¬ã¯ã¿çµç±ã§ç´æ¥ã¡ã½ãããå¼ã³åºããã©ã¡ã¼ã¿ã渡ãæ¹æ³ãããã¾ããè¨èã«ãã¦ãä¼ããã«ããã®ã§ããµã³ãã«ãè¦ã¦ãã ããã
ãã©ã¤ãã¼ãã¡ã½ããã®å®ç¾©
- (void) privateMethod; - (void) privateMethod:(NSString *)aString; - (void) privateMethod:(NSString *)aString second:(NSString *)bString;
ãperformSelectorã«ããããªãã¼ãã¡ã½ããã¸ã®ã¢ã¯ã»ã¹ä¾ã§ãã
å¼æ°ã渡ãå ´åã¯ãwithObjectã§æ¸¡ãã¾ããå¼æ°ãï¼ã¤ããå ´åã¯withObjectã並ã¹ã¾ã
[exampleClass performSelector:@selector(privateMethod)]; //Test For privateMethod:(NSString *) aString [exampleClass performSelector:@selector(privateMethod:) withObject:@"c"]; //Test For privateMethod:(NSString *)aString second:(NSString *)bString [exampleClass performSelector:@selector(privateMethod:second:) withObject:@"c" withObject:@"d"];
ããã®æ¹æ³ã®åé¡ç¹ã¯ãå¼æ°ãï¼ã¤ã¾ã§ãã渡ããªããã¨ã§ããå¼æ°ãï¼ã¤ä»¥ä¸ããã¡ã½ããã«ã¯é©å¿ã§ãã¾ãããã¾ããå¼ã³æ¹ãæ¬æ¥ä½¿ãããè¨è¿°ã¨ã¯éãä¸ã«ãå¯èªæ§ãä½ãã®ã§ä½ããå§ãã§ãã¾ããã
Key-Value Codingã使ã£ã¦ãããããã£ã«ã¢ã¯ã»ã¹ããæ¹æ³
ã次ã®æ¹æ³ããKey-Value Codingãæ¥æ¬åã§ã¯ããã¼å¤ã³ã¼ãã£ã³ã°ã§ãããã®æ¹æ³ã¯ããã©ã¤ãã¼ãã¡ã½ããã§ã¯ãªããã©ã¤ãã¼ãããããã£ã«ç´æ¥ã¢ã¯ã»ã¹ããæ¹æ³ã§ããå¤ã®æ¤è¨¼ã«å©ç¨ã§ãã¾ããããã©ã¤ãã¼ãã¡ã½ãããç´æ¥å¼ã³åºããã¨ã¯åºæ¥ãªãã®ã§ãããåä½ã§ã¯ã¦ããããã¹ãã«ã¯ä½¿ãã¾ããã
ã¯ã©ã¹ãæ¡å¼µãã¦ãã¡ã½ããã¨ããããã£ãå®ç¾©ããæ¹æ³
ãæå¾ã«ã¯ã©ã¹ãæ¡å¼µãã¦ããã©ã¤ãã¼ãã¡ã½ããã¨ããããã£ããããªãã¯ã«å®ç¾©ããæ¹æ³ã§ããç°¡åã«èª¬æããã¨ãExampleClass.hã¨ExampleClass.mã«å¯¾ãã¦ãExampleClassExtension.hãå®ç¾©ãã¾ããExampleClassExtension.hã¯ã ExampleClassã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ExampleClass.mã«å®ç¾©ããã¦ãããã©ã¤ãã¼ãã¯ã©ã¹ã®ããããã£ã»ã¡ã½ãããæã¡ã¾ãã
ExampleClass.h
#import <Foundation/Foundation.h>
@interface ExampleClass : NSObject {
NSString *_exampleProperty;
}
- (void)publicMethod;
@property (nonatomic, retain) NSString * exampleProperty;
@end
ExampleClassExtension.h
@interface ExampleClass () - (void) privateMethod; - (void) privateMethod:(NSString *)aString; - (void) privateMethod:(NSString *)aString second:(NSString *)bString; @end
ããã©ã¤ãã¼ãã¡ã½ããã§å®£è¨ãã¦ãããã®ããExtensionãããã¼ã§å度ãããªãã¯ã§å®£è¨ãã¦ããã¨ããããã½ã«ãªãã¾ããæ¡å¼µã¯ã©ã¹ã¯ãã¹ããã¿ã¼ã²ããã«ä½æãã¦ããã°ãæå³ããªãã¢ã¯ã»ã¹ãé²ããã¨ãå¯è½ã«ãªãã¾ãããã©ã¤ãã¼ãã¡ã½ããã®å®£è¨ãï¼éã«ããå¿
è¦ãããã®ã§ãå¤å°åé¡ããããã®ã®å®ç¨çãªæ¹æ³ã§ã¯ãªããã¨æãã¾ãã
ããã以å¤ã§ãæ¹æ³ãããã°ããã²æãã¦ãã ããã
ãµã³ãã«ã½ã¼ã¹ã¯ãGitHubã«ããã¦ããã¾ãã
åç
§ï¼
Accessing private methods and properties in ObjC unit tests
Objective-C Key-Value Coding | YOHEI's BLOG
performSelectorとパラメタ数 - Kazzzの日記
Communicating With Objects: Key-Value Coding