MRCã¨ARCã®ä¸¡æ¹ã«å¯¾å¿ããã³ã¼ããã·ã³ãã«ã«æ¸ãæ¹æ³
MRCï¼Manual Reference Countingï¼ã¨ARCï¼Automatic Reference Countingï¼ã®ä¸¡æ¹ã«å¯¾å¿ããã³ã¼ããæ¸ãããã«ã¯
#if __has_feature(objc_arc)
#if __has_feature(objc_arc_weak)
ã¨ããåå²ã«ããæ¡ä»¶ã¤ãã³ã³ãã¤ã«ãããªããã°ãªããªãã
ä»ã¨ãªã£ã¦ã¯å°ã
å¤ã話é¡ã§ã¯ããã*1ãæ°ã«ãªã£ãã®ã§ãªãã¹ãç°¡åã«å¯¾å¿ããæ¹æ³ã調ã¹ã¦ã¿ããç°ãªããã«ãç°å¢ã«ã½ã¼ã¹ã³ã¼ãå´ã§å¯¾å¿ããå ´åãã®ãããªæ¹æ³ããããã¨èªä½ãè¦ãã¦ããã¦æã¯ãªãã ããã¨æãã
MRCã¨ARCã®ä¸¡æ¹ã«å¯¾å¿ããä¸ã§æ°ãã¤ããªããã°ãªããªãç¹ã¯ä¸»ã«æ¬¡ã®2ç¹ã ã
- 使ç¨å¯è½ãªã©ã¤ãã¿ã¤ã 修飾åã®éã
- æ示çãªretain/releaseã®ã¡ãã»ã¼ã¸éä¿¡ã®å¿ è¦æç¡
ç°å¢ã«å¿ãã¦ã©ã¤ãã¿ã¤ã 修飾åãé©åã«ä½¿ãåãã
ãããã¯ãªãã¸ã§ã¯ãã®ä¸ã§å¤æ°ã«ã¢ã¯ã»ã¹ããå ´åã解æ¾æ¸ãªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ããã循ç°åç §ãçãã¦ãã¾ããããããããããã§ãåå¤æ°ã«é©åãªã©ã¤ãã¿ã¤ã 修飾åãè¨å®ãã¦ããå¿ è¦ããããããã§åé¡ã¨ãªãã®ã¯ããã«ãæã«ä½¿ç¨ããSDKãDeployment Targetã¨ãã¦æå®ããiOSã®ãã¼ã¸ã§ã³ã«ãã£ã¦ã使ç¨å¯è½ãªã©ã¤ãã¿ã¤ã 修飾åãç°ãªãã¨ããç¹ã§ããã
次ã®ãã¼ã¸ã§ã¯ãMRCã¨ARCã®åæ¹ã®ç°å¢ã«ããã¦ãããã¯ãªãã¸ã§ã¯ããå®å ¨ãã¤å®¹æã«æ±ãããã®æ¹æ³ã説æããã¦ããã
ãã®ãã¼ã¸ã®ä¸ã§ãã©ã¤ãã¿ã¤ã 修飾åã«ã¤ãã¦ã®ãã¯ããå®ç¾©ããæ¹æ³ã示ããã¦ãããiOS4.3ã¨iOS5以éã®éãã¯ä»ããªãæèãã¹ããã¤ã³ãã§ããããã大å¤åèã«ãªãã該å½ç®æã®ã¿ã以ä¸ã«æç²ããã
// ARC & memory management // Use these prefixes to be compatible with ARC on iOS 5/ ARC on iOS 4.X / non-ARC // #if __has_feature(objc_arc_weak) // iOS 5 or above #define __my_block_weak __weak #define __my_block_weak_unsafe __weak #elif __has_feature(objc_arc) // iOS 4.X #define __my_block_weak __strong #define __my_block_weak_unsafe __unsafe_unretained #else // iOS 3.X or non-ARC projects #define __my_block_weak __strong #define __my_block_weak_unsafe __block #endif
retain/releaseã¡ãã»ã¼ã¸ããã¯ãå
retainãreleaseãautoreleaseãªã©ã®ä¿æã»è§£æ¾ã«é¢ããã¡ãã»ã¼ã¸ãéä¿¡ããé¨åã®ã³ã¼ãããã§ãããã¨ãªãæ¯å__has_feature(objc_arc)ã§åå²ãããã«ã·ã³ãã«ã«æ¸ãããããã®ç¹ã«ã¤ãã¦ã¯æ¬¡ã®ãã¼ã¸ã§ãã¯ãã®å®ç¾©ã®ä¾ãæ²è¼ããã¦ããã
ãã¯ãé¨åã®ã¿ãæç²ããã¨ä»¥ä¸ã®ã¨ããã
#ifndef AH_RETAIN #if __has_feature(objc_arc) #define AH_RETAIN(x) x #define AH_RELEASE(x) #define AH_AUTORELEASE(x) x #define AH_SUPER_DEALLOC #else #define __AH_WEAK #define AH_WEAK assign #define AH_RETAIN(x) [x retain] #define AH_RELEASE(x) [x release] #define AH_AUTORELEASE(x) [x autorelease] #define AH_SUPER_DEALLOC [super dealloc] #endif #endif