Cocoa 如何解决问题;将Objective-C指针隐式转换为';const void';是不允许的;使用[NSValue valueWithPointer:t]时出错?
如何修复使用以下消息时出现的错误“不允许对指向“const void”的Objective-C指针进行隐式转换”:Cocoa 如何解决问题;将Objective-C指针隐式转换为';const void';是不允许的;使用[NSValue valueWithPointer:t]时出错?,cocoa,automatic-ref-counting,Cocoa,Automatic Ref Counting,如何修复使用以下消息时出现的错误“不允许对指向“const void”的Objective-C指针进行隐式转换”: NSValue *key = [NSValue valueWithPointer:t]; 特别感谢你能提供为什么这段代码会抛出这个异常,如果是简单的新手术语 以下是完整的方法(如果有帮助): - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *t in touches)
NSValue *key = [NSValue valueWithPointer:t];
特别感谢你能提供为什么这段代码会抛出这个异常,如果是简单的新手术语
以下是完整的方法(如果有帮助):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *t in touches) {
// Is this a double tap?
if ([t tapCount] > 1) {
[self clearAll];
return;
}
// Use the touch object (packed in an NSValue) as the key
NSValue *key = [NSValue valueWithPointer:t]; // here is where the error is
// Create a line for the value
CGPoint loc = [t locationInView:self];
Line *newLine = [[Line alloc] init];
[newLine setBegin:loc];
[newLine setEnd:loc];
// Put pair in dictionary
[linesInProcess setObject:newLine forKey:key];
}
在《大书呆子射程指南》第二版的练习中,我遇到了同样的问题,该指南不包括ARC。这就是问题所在。他们已在此处更新了示例/解决方案代码: 在其中,您可以看到他们已将该行更改为:
NSValue *key = [NSValue valueWithNonretainedObject:t];
尝试简单地添加(_bridgevoid) 从技术上讲,关闭编译器的方法是将对象指针强制转换为
const void*
,即[NSValue valueWithPointer:(const void*)t]
,但是@Ole Begermann完全正确:您的代码是错误的。不要这样做,它不会抛出异常。这只能在代码运行时发生。这个代码并没有走那么远;编译失败。