用于将整数添加到 nsmuatablearray FMDB/EGODB 的编译器指令防御性编程

发布于 2024-10-11 18:14:14 字数 1001 浏览 3 评论 0原文

当用户尝试将 int 添加到 nsmutablearray 时,我想抛出一条警告消息,

基本上任何包含非 nsstring / nsnumber 值的插入语句都会导致运行时崩溃。当你输入 %@ 而不是 %d NSLog(int); 时,这与你得到的崩溃完全相同。 崩溃没问题,但我想向用户抛出一条友好的“致命”消息。

到目前为止,我已经用 isKindOfClass NSObject 进行了尝试捕获,但整数正在溜走。

#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)); }

它时调用它

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

// 然后当我调用 [自测试:@"str",2,nil];

抛出错误消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2024-10-18 18:14:14

使用 isKindOfClass 来测试它是否是 NSObject 是行不通的。崩溃的原因是它将 int 视为指向对象的指针,但它是一个无效指针。调用 isKindOfClass 会导致相同的错误。由于应用程序因无效指针而崩溃,因此不会引发异常,因此 @try-@catch 语句也不会捕获它。基本上,您必须相信用户会听取编译器的指令,并且不会在需要对象的地方使用 int 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文