ãé¡:
ã»nil ã«å¯¾ããã¡ãã»ã¼ã¸éä¿¡ãä¾å¤ã«ãªããªã
ãã®ä»æ§ã£ã¦ãObjective-C以å¤ã§ã»ã¨ãã©è¦ããããã¨ããªãã®ã ãã©ãã¡ãªããã«æ¯ã¹ã¦ãã¡ãªããã大ãéããã¨æãããªãã¸ã§ã¯ãã®ã¡ã½ãããã§ã¤ã³ã§ãã®ä»æ§ããã¾ã«ä¾¿å©ãªãã¨ã¯ãããã©ãã»ã¨ãã©ã®å ´åããã°ãçºç¾ããã¿ã¤ãã³ã°ãé ããªãã ãã«çµããã¨ããã®ãçµé¨åããã®è¾ºãçç·´ã®Objective-Cerã¯ã©ãèãã¦ãã®ãä¸åº¦ç¥ãããã¨ããã
Objective-Cで不満に思うこと - kmizuの日記
ãã10年以ä¸ä»äºã§Objective-Cã¯ä½¿ã£ã¦ãªãã®ã§ãçç·´ã¨ããããã¯åªã®ãã¼ãã«ãªãã ãã©ãèªåãªãã«èããäºãæ¸ãã¦ã¿ããã¨æãã
éååã®äºãæãåºããªããæ¸ãã®ã¨ãæ¶æ¸¬ã交ãã¦ããã®ã§ããã¾ãæ£ç¢ºãªå
容ã§ãªãäºã¯ãããããæã£ã¦ããã¾ãã
Objective-Cã®ä¾å¤ã«ã¤ãã¦
ã¾ããããããObjective-Cã®ãããã¼ãªä¾å¤ã£ã¦åå¨ããªãããããªããã¨ãã話ããããã¨æãã
Mac OS XãiOSç¨ã«Objective-Cã§ããã°ã©ã ãæ¸ãéã«è¯ãåºã¦ããä¾å¤ã£ã¦ãã ãããNSExceptionã¯ã©ã¹ã£ã¦ãã¤ã ã¨æã*1ã NSExceptionã¨ããååããåããããã«ãããã¯Foundationãã¬ã¼ã ã¯ã¼ã¯ã®ä¸å¡ã ã£ããããã
Foundation(Foundation Kit)ã¯1995å¹´ã«NEXTSTEP 3.3ããªãªã¼ã¹ãããæã«è¿½å ããããã¬ã¼ã ã¯ã¼ã¯ã§ã(OpenStepã¨ããå ¬éãããä»æ§ã«åºã¥ãã¨ã¯ãã) NeXT社ãç¬èªæ¡å¼µããã¯ã©ã¹ã©ã¤ãã©ãªã§ããããã以åã®Objective-Cã«ã¯NSExceptionã¯åå¨ããªãã£ãã
ãããããã¾ã§Objective-Cã«ä¾å¤å¦çã¯ãªãã£ããã¨ããã¨ããããããã§ã¯ãªãã¦ãNXHandlerã¨ããæ§é ä½ã¨setjmp/longjmpã使ã£ã¦å®è£ ãããä¾å¤å¦çç¨ã®ãã¯ããåå¨ãããä»ã®NS_DURINGãã¨ã»ã¼åããããªæ§æã§ä½¿ããã¦ããã
NX_DURING /* code that may cause an error */ NX_HANDLER switch (NXLocalHandler.code) case NX_someErrorCode: /* code to execute for this type of error */ default: NX_RERAISE(); NX_ENDHANDLER
ããããã®NX_DURINGããObjective-Cã®ãããã¼ãªä¾å¤å¦çæ©æ§ã¨ããã¨ããã§ããªãã¦ãååããåããéãNeXT社ã®ç¬èªæ©è½ã ã¨æããããå°ãªãã¨ãListã¯ã©ã¹ãHashTableã¯ã©ã¹ã®ãããªObjective-Cã®åºæ¬ã¯ã©ã¹ã§ã¯æ示çã«ã¯ä½¿ããã¦ããªãã
NeXT/Apple以å¤ã®Objective-Cå¦çç³»ã§ã¯ã©ãã ããã¨ããã£ã¦ã¡ãã£ã¨èª¿ã¹ããã©ãããããã«å¥ã ã®ä¾å¤å¦çãæã£ã¦ããã£ã½ããä¾ãã°ãPortable Object Compilerã®ããã¥ã¢ã«ã«ã¯æ¬¡ã®ããã«è¨è¼ããã¦ãã
This chapter discusses exception handling for OBJECTIVE-C as outlined in [Cox, 1991].
An exception -- in response to some abnormal program condition -- is raised by sending a error: message to an Object.
if (!h) [ anObject error:"h can't be zero" ];
This is similar to how in Stepstone OBJECTIVE-C an error action (aborting the process) is performed.
However, in our case, the user might substitute a different Block (called exception handler) for the default handler (which aborts the process). This is done by using the method ifError: :
[ { c = [a foo]; } ifError: { :msg :rcv | printf("got an exception"); } ];
Instead of evaluating the default handler, error: will execute the exception handler specified by ifError:. The handler is invoked with a message object and with the receiver of the error: message.
User Manual Portable Object Compiler Version 3.3.10
è¦ãããã«ãããªãæ¯è²ãéã£ã¦ããããªãã¸ã§ã¯ãã®-error:ãå¼ã¶äºã§ä¾å¤ãèµ·ãããæ¨æºã®ãã³ãã©ã§ã¯ãªãBlockã使ã£ã¦å¦çããããªããããã
è¦ããã«è¨èªç³»ã§ç°ãªã£ã¦ãã¦ãæ¬æ¥ã®Objective-Cçãªä¾å¤ã¨ãããã®ã¯ãªãã¨æãããã*2
nilã®ä½¿ããæ¹ã«ã¤ãã¦
NEXTSTEP 3.3ã®ããã¥ã¢ã«ãè¦ã¦ããã¨ãCommonã¯ã©ã¹ãApplicationKitã®ã¯ã©ã¹ã§æ示çã«ä¾å¤ã使ããã¦ããã¨ããã¯ããã¾ããªã*3ãã¹ããªã¼ã ã®èªã¿æ¸ãç³»ããã©ã³ãå¨ããNXColorãªã©ã®idã§ã¯ãªãæ§é ä½ãè¿ãå¦çãDPSå¨ããªã©ã«å°ãããç¨åº¦ããã以å¤ã¯ãä¾å¤ãèµ·ãããã«nilãè¿ãã¦ããããã«è¦ããã
ä¾ãã°ãå¯å¤é·ã®ã³ã¬ã¯ã·ã§ã³ã¯ã©ã¹ã§ããListã¯ã©ã¹ã®ãæå®ããä½ç½®ã«ãªãã¸ã§ã¯ããæ¿å ¥ããããã®ã¡ã½ããã¯ãããã¥ã¢ã«ã«æ¬¡ã®ããã«æ¸ããã¦ããã
insertObject:at:
- insertObject:anObject at:(unsigned int)index
Inserts anObject into the List at index, moving objects down one slot to make room. If index equals the value returned by the count method, anObject is inserted at the end of the List. However, the insertion fails if index is greater than the value returned by count or anObject is nil.
If anObject is successfully inserted into the List, this method returns self. If not, it returns nil.
List - Common Classes and Functions - NEXTSTEP General Reference (c) 1995 by NeXT Computer, Inc.
è¦ç´ ãnilã®å ´åããæ¿å ¥ç®æã«ãªã¹ãã®è¦ç´ æ°ãã大ããæ°å¤ãæå®ããå ´åãä¾å¤ãèµ·ãããã«nilãè¿ããæ¿å ¥ãæåããå ´åã¯selfãè¿ãã
ãã®ããã«ãnilã¯åãªã空å¤ã§ã¯ãªããæããã«å¤±æç³»ã¨ãã¦ä½¿ããã¦ããã¨æãããã
ãã®ãã¨ããæ¨æ¸¬ããã«ãnilã«ã¡ãã»ã¼ã¸ãéãã¨nilãè¿ãã®ã¯ã失æç³»ã®ä¼æã¨ããæå³åããæ ã£ã¦ããã®ã§ã¯ãªãããä¾ãã°ã次ã®ãããªæ¶ç©ºã®ã³ã¼ãããã£ãã¨ãã¦ã
[[self itemList] addObject:[[Item createNewItem] prepareFor:self]];
å¦çä¸ã«ã+createNewItemã-prepareFor:ã失æããæã«ãnilãè¿ãäºã§ããã®è¡ã®å¦çå ¨ä½ãä¸æãã¦æãåºããããã«ä½ããã¨ãåºæ¥ãã
ã¤ã¾ãã便å©ã ããã¡ã½ãããã§ã¼ã³ã¨ããã®ã§ã¯ãªãã¦ãå¶å¾¡ããã¼ã¨ãã¦ä½¿ãçºã®ãã®ã ã£ãã®ã§ã¯ãªããã¨ããæ³åã§ãã
Foundationãã¬ã¼ã ã¯ã¼ã¯ãå°å ¥ãããããããããreturn selfããã¡ã½ãããæ¸ã£ã¦ãã£ã¦ãä¾ãã°ç¾å¨ã®ã³ã¬ã¯ã·ã§ã³ã¯ã©ã¹ã§ããNSMutableArrayã®æ¿å ¥ã¡ã½ããã¯ã次ã®ããã«æ»ãå¤ã®åã¯voidã«ãªããç°å¸¸æã«ã¯ä¾å¤ãèµ·ããããã«ãªã£ã¦ããã
insertObject:atIndex:
Inserts a given object into the array's contents at a given index.
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index(cut)
NSMutableArray Class Reference
Important Raises an NSInvalidArgumentException if anObject is nil.
(cut)
Important Raises an NSRangeException if index is greater than the number of elements in the array.
å人çã«ã¯ãFoundationã«åãæ¿ãã£ã段éã§ãnilã«ã¡ãã»ã¼ã¸éãã¨ä¾å¤ãã¨ããä»æ§ã«å¤ãã£ã¦ãè¯ãã£ããããªæ°ããããå°ãªãã¨ã[ [ [Hoge alloc] init] autorelease]ãªã©ã®ç¹å®ã®ã¤ãã£ãªã 以å¤ã§ã¯ãèªåã¯ç©æ¥µçã«ã¯ä½¿ããªãããã«ãªã£ã¦ãã£ãã
追è¨: Portable Object Compilerã§ã®nilã®æ±ã
ãªãã·ã§ã³ã§æåãå¤ããããã¿ããã§ããªã
Sending Messages to Nil
The -noNilRcvr option can be used to prevent messages being sent to nil (the NULL pointer). It is in fact a runtime option, since the only effect of this option is that, when the main() of the program is compiled with this option, a nilHandler() function will be registered that stops the process, instead of simply returning nil (as the default handler does).
objc -noNilRcvr main.m
User Manual Portable Object Compiler Version 3.3.10
*1:ä»ã¯NSExceptionã§ãªãã¦ãã¹ãã¼ãã£ããã§ããããããã©ã
*2:åºèªçã«Stepstoneã®ã³ã³ãã¤ã©ã®ä»æ§ãæ£çµ±ã¨ããã¹ããããããªããã©ãå°ãªãã¨ãAppleã®ãã®ã«ã¯åãå ¥ãããã¦ããªãã¨æãã
*3:å é¨çã«ã¯ä½¿ããã¦ãããããããªããã©