0

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.

johndpope
  • 5,035
  • 2
  • 41
  • 43

1 Answers1

0

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.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123