3

我想创建一个简单的项目来研究xcode4.2上的ios5.0编程。这是原始代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"];
NSURL* url = [NSURL fileURLWithPath:path];

AudioServicesCreateSystemSoundID( ( CFURLRef)url, &winSoundID);

编译错误:

file://localhost/Users/Hikari/Documents/Projects/Pickers/Pickers/CustomPickerViewController.m: error: Automatic Reference Counting Issue: Cast to 'CFURLRef' (aka 'const struct __CFURL *') of an Objective-C pointer to自动引用计数不允许使用非 Objective-C 指针

所以我在google上搜索过,有朋友说我必须添加宏'__bridge'来解决它,但这不起作用。

代码:

AudioServicesCreateSystemSoundID( ( __bridge CFURLRef)url, &winSoundID);

错误:

file://localhost/Users/Hikari/Documents/Projects/Pickers/Pickers/CustomPickerViewController.m:错误:语义问题:使用未声明的标识符“__bridge”

自动引用计数已启用!如何解决这个问题呢???

4

3 回答 3

1

尝试你的项目设置,键入“自动引用”,并将 Objective-C 自动引用计数设置为 NO,禁用它并重新编译你仍然会收到警告消息,但它确实编译并运行。

于 2011-09-28T05:35:55.663 回答
1

objc_unretainedPointer 答案是这样的。代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"];
NSURL* url = [NSURL fileURLWithPath:path];

AudioServicesCreateSystemSoundID( (CFURLRef)objc_unretainedPointer(url), &winSoundID);

path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)objc_unretainedPointer([NSURL fileURLWithPath:path]), &crunchSoundID);

它工作正常。

ps:感谢 Macmade 帮我编辑代码。我想我已经学会了。

于 2011-09-17T13:42:33.787 回答
0

你确定你总是用 ARC 编译那个文件吗?这看起来更像是文件在启用 ARC 的情况下编译一次,给你第一个错误,第二次在禁用 ARC 的情况下,在你修复第一个错误后给你第二个错误。

于 2011-09-16T16:19:32.260 回答