iPhone SDKã«ãããautoreleaseã®æ¯é
Interface Builderãæ¨ã¦ã¦å®å ¨ã«Xcodeã ãã§ä½æããããã«ãªã£ã¦ãã¡ã¢ãªæä½ã«æ°ã使ãã±ã¼ã¹ãçµæ§å¤ããªã£ã¦ããã®ã§ãæ°ãã¤ãã¦ããã¨ãæ¸ãã¦ã¿ããã¨ã«ããã
ç¹ã«ç¥èããªãã£ãããã¯é¦¬é¹¿æ£ç´ã«autoreleaseã使ã£ã¦æ¾ç½®ãã¦ããã ãã©ãmain.mã«è¨è¿°ããã¦ããNSAutoreleasePoolã®åå¨ãç¥ã£ã¦ãä¸ã¤ NSAutoreleasePoolã®åä½ãç¥ã£ã¦ä»¥æ¥å¾®å¦ã ããªãã¨æã£ã¦ãã
autoreleaseã®åä½
main.mã«è¨è¿°ããã¦ããNSAutoreleasePoolã®å½¹å²ããããã£ã±ã«æ¸ãã¨ãã½ã¼ã¹ã³ã¼ãä¸ã§autoreleaseã®ã¡ãã»ã¼ã¸ãéããããªãã¸ã§ã¯ãã®ãã¡ã¢ãªä¸ããã®è§£æ¾å¦çãã¾ã¨ãã¦æ ãã¨ãã代ç©ã§ãã¡ã¢ãªä¸ã§ã®è§£æ¾å¦çã«ç´°ããæ°ã使ããªãã¦ãããªãã®ãç¹å¾´ããã 解æ¾ã®ã¿ã¤ãã³ã°ã¨ãæå®ããã«ããï¼ã§ããªãï¼ï¼ãããçµæ§ã¡ã¢ãªãé£ããã¡ã«ãªãã
使ã£ã¦ãªãã®ã«ã¡ã¢ãªå æã¨ããã¡ãã£ãããããããäºç´°ããæä½ãããã¨ãã«ã¯ãã£ã±åå¥ã«releaseããããã ãã©ãautoreleaseæå®ãã¦ããå ´åã¯releaseã使ããªãã
autoreleaseã®åé¡ç¹
autoreleaseã§åé¡ãªã®ã¯ããªãã¸ã§ã¯ãçææã«alloc,initï¼ããã³initXXXã¡ã½ãã)ã®çµã¿åããã«ã¦çæãã¦ããªãçã®ãæèããã«ä½¿ã£ã¦ãã¾ã£ã¦ããæã
ä¾ãã°
NSArray *array = [NSArray array];
ã¿ãããªå ·åããã®ã±ã¼ã¹ã§ã¯ãªãã¸ã§ã¯ãçææã«å é¨ã§èªåçã«autoreleaseãããã£ã¦ãã¾ããã®ã ããããªãªã¼ã¹ã®ã¿ã¤ãã³ã°ãç´°ããæå®ã§ããªãããç¡é§ã«æ±ãã¦ãã¾ããã¨ãããã¨ã«ãªãããã
ãªããç°æ§ã«å¦çãéãã¢ããªãªããã¯å ¨é¨autoreleaseã«ä»»ãã£ãããªã±ã¼ã¹ãªãã¦å¯è½æ§ããããã¡ãªã¿ã«ãä¸åº¦å ¨é¨autoreleaseä»»ãã«ãããå¦çã®ãããã使ãç©ã«ãªããªãã¬ãã«ã«çºå±ãã¦ãã°ããªã£ããã¨ãããã
代æ¿æ¡
代æ¿æ¡ã¨ãã£ã¦ããçæã«ã¯ä¸è²«ãã¦alloc, initãç¨ãã¦ãviewã«å ããå ´åãªãã¯å¾ã§å¿ããã«releaseã使ããã¨ããããªæããNSStringã¨ãstringWithFormatã§ãããªãæ¸ã¾ããã¿ã¤ãã®äººããããã ãããã©ãããã
NSString *str = [[NSString alloc] initWithFormat:@"hoge%@", @"hoge"];
ãªå ·åã§ã²ã¨æéãããã¨ãreleaseãå¿ããªããã°å¹¸ãã«ãªããæ°ããã¦ãã
autoreleaseæ¨å¥¨ã£ã¦å ·åã«æ¸ãã¦ã人ãå¤ããã©ããããªãã«å¦çãè¦ãã¦ãããææãããã«è¡ãã¾ãããã¨ãããã¨ã§ã
ãã®ç¹ããã¿ãã¨ãå¤åã¤ã³ãã©ã·ãã£ã®BB2Cã¯ã¡ã¢ãªç®¡çãè¶ åªç§ãªãã ãããªã