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

Posted by johndpope on Stack Overflow See other posts from Stack Overflow or by johndpope
Published on 2011-01-13T05:38:23Z Indexed on 2011/01/13 5:53 UTC
Read the original article Hit count: 296

Filed under:
|
|

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]]) [valistArray 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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about crash