这太荒谬了,我试图创建一个声音布尔来关闭应用程序中的声音。我不断得到
Undefined symbols for architecture i386:
"_kPlaySoundPrefsKey", referenced from:
-[AppDelegate application:didFinishLaunchingWithOptions:] in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我已经检查了我的所有文件是否在构建阶段链接,我已经删除了 appdelegate .m,在我什至在任何视图控制器中调用 bool 之前就收到错误,并在构建阶段重新导入它。检查我是否有相关的框架。我什至检查了我之前用相同代码制作的应用程序,代码看起来完全一样,没有错误(使用以前版本的 xcode 构建)。回到基础知识,将以下代码添加到我的应用程序代理后,我立即收到错误,
.h
#import <UIKit/UIKit.h>
extern NSString *kPlaySoundPrefsKey;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
.m
#import "AppDelegate.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
NSDictionary *defaultDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:kPlaySoundPrefsKey];
return YES;
}
如果我改变extern NSString *kPlaySoundPrefsKey;
to NSString *kPlaySoundPrefsKey;
它构建然后崩溃......我现在没有想法了
当你声明某事为extern
您正在告诉编译器类型并且您将在其他地方定义它。在你的情况下,你永远不会定义你的变量。
所以在你的 .h 中你会有:
extern NSString* kPlaySoundPrefsKey;
但在某些 .m 中你还必须有
NSString* kPlaySoundPrefsKey = @"play_sounds"; // or some value
由于在您的情况下这些是常量,您还可以指定:
extern NSString* const kPlaySoundPrefsKey;
and
NSString* const kPlaySoundPrefsKey = @"play_sounds";
如果有人写过类似的内容,添加 const 限定符将导致编译器错误
kPlaySoundPrefsKey = @"some_other_value";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)