Sunday, 20 April 2014

Crash storing block in mutable array.


what can people tell me why program crashing:

code:
  #import <foundation/foundation.h>    @interface action : nsobject   + (void)execute;  @end    @implementation action  + (void)execute  {  	nslog(@"the action executed.");  }  @end    @interface actionperformer : nsobject {  @private  	nsmutabledictionary* _namedactions;  }    + (actionperformer*) performer;  - (void)addaction:(nsstring*)action withclass:(class)class andselector:(sel)selector;  - (void)performaction:(nsstring*)action;  @end    @implementation actionperformer    typedef void(^actionblock)();    + (actionperformer*) performer  {  	actionperformer* performer = [[actionperformer alloc] init];  	performer->_namedactions = [nsmutabledictionary dictionary];  	return performer;  }    - (void)addaction:(nsstring*)actionname withclass:(class)class andselector:(sel)selector  {  	actionblock block = ^(){ [class performselector:selector]; };  	// verify things work  	block();  	[_namedactions setobject:block forkey:actionname];  }    - (void)performaction:(nsstring*)actionname  {  	actionblock block = [_namedactions objectforkey:actionname];  	// crash happens here!!!  	block();  }    @end    int main()  {  	@autoreleasepool {  		actionperformer* performer = [actionperformer performer];  		  		[performer addaction:@"action" withclass:[action class]  			andselector:@selector(execute)];  			  		[performer performaction:@"action"];	      	}      return 0;  }  
i building on mac os x 10.6.8 using xcode 4.2 apple llvm compiler 3.0
 

since didn't post crash log or stack trace, i'm not going bother guessing.

instead, i'm going "use nsinvocation". seriously, trying you're doing blocks silly. when nsinvocation exists , well-tested (hint: it's instrumental in distributed objects).

, i'm going suggest not using class-methods. use instance methods, if means targeting instance of action class instead of targeting action class itself. so:
code:
@interface action : nsobject  - (void)execute; @end  @implementation action - (void)execute { 	nslog(@"the action executed."); } @end 
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment