开发者

compiler directive defensive programming for adding ints to nsmuatablearray FMDB/EGODB

I would like to throw a warning message when users try to add an int to an nsmutablearray

basically any insert statement that includes values that are not nsstring / nsnumber cause run time crashes. It's exactly the same crash you get when you type %@ instead of %d NSLog(int); The crash is ok, but I want to throw a friendly 'FATAL' message to user.

so far I have this try catch with isKindOfClass NSObject but ints are slipping through.

#define FATAL_MSG "FATAL: object is not an NSObject subclass. Are you using int? use [NSNumber numberWithInt:1] \n"
#define VAToArray(firstarg) ({\
NSMutableArray* valistArray = [NSMutableArray array];\
id obj = nil;\
va_list arguments;\
va_start(arguments, sql);\
@try { \
while ((obj = va_arg(arguments, id))) {\
if([obj isKindOfClass:[NSObject class]]) [v开发者_开发问答alistArray addObject:obj];\
else printf(FATAL_MSG); \
}\
}   \
@catch(NSException *exception){ \
  printf(FATAL_MSG); \
} \
va_end(arguments);\
valistArray;\
})


- (void)test:(NSString*)sql,... {

NSLog(@"VAToArray :%@",VAToArray(sql)); }

// then call this

[self test:@"str",@"test",nil];

when I call this [self test:@"str",2,nil];

throw the error message.


Using isKindOfClass to test if it is an NSObject won't work. The reason for the crash is that it is treating the int as a pointer to a object, but it is an invalid pointer. Calling isKindOfClass would cause the same error. Since the application crashes from an invalid pointer, there is no exception thrown, so the @try-@catch statement won't catch it either. Basically, you have to trust that the user will listen to the compiler and not use an int where an object is expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜