XCode 中的全局字符串问题
我对 cocoa 和 Xcode 还很陌生,我已经完成了一些基本的 C 编码,但是我对 Objective-C 和 cocoa 很不熟悉,所以请原谅我犯的任何愚蠢的错误。我的问题是我正在使用的这些全局变量。 我在头文件中声明了一个全局 NSString 变量,它在主文件中使用,如下所示:
//AppController.h
-(IBAction)button1:(id)sender;
-(IBAction)button2:(id)sender;
extern NSString *hi
//AppController.m
-(IBAction)button1:(id)sender
{
NSString *const hi = @"Hello";
}
-(IBAction)button2:(id)sender;
{
NSLog (@"%@", hi);
}
但是,当我单击运行时,构建失败,并收到错误消息:
“_hi”,引用自:
一些额外信息:
Undefined symbols for architecture x86_64: "_hi", referenced from: -[AppController gallery:] in AppController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果您知道这意味着什么和/或如何解决它请帮助我。谢谢
I'm pretty new to cocoa and Xcode, I've done some basic C coding, but I pretty much suck at objective-c and cocoa, so please excuse me for any stupid mistakes I make. My problem is with these global variables I'm using.
I have a global NSString variable declared in the header file, and it's used in the main file like so:
//AppController.h
-(IBAction)button1:(id)sender;
-(IBAction)button2:(id)sender;
extern NSString *hi
//AppController.m
-(IBAction)button1:(id)sender
{
NSString *const hi = @"Hello";
}
-(IBAction)button2:(id)sender;
{
NSLog (@"%@", hi);
}
However when I click run the build fails and I get the error message:
"_hi", referenced from:
Some extra info:
Undefined symbols for architecture x86_64: "_hi", referenced from: -[AppController gallery:] in AppController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If you know what this means and/or how to fix it please help me. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要为
hi
提供全局定义。将您的声明:移动到任何方法外部的某个地方。我不太确定您想要
button1:
做什么,但它对于您的实现来说似乎根本没有必要。You need to provide a global definition for
hi
. Move your declaration:to someplace outside of any method. I'm not really sure what you want
button1:
to do, but it doesn't seem necessary at all for your implementation.我想卢克喜欢:
单击按钮一后将字符串设置为特定值,
单击按钮二后再次检索它。
AppController.h
AppController.m
I assume Luke likes to:
Set the string to a specific value after button one is clicked,
and retrieve it again after button two is clicked.
AppController.h
AppController.m
当定义全局变量和常量字符串等时,我通常是这样做的:
MDAppController.h
:MDAppController.m
:When defining global variables and constant strings, etc., this is usually how I do it:
MDAppController.h
:MDAppController.m
:我的问题是你为什么想成为外部人士?这里最好的方法是创建一个单例,您应该将所有成员作为类的一部分并避免任何全局成员。
希望这有帮助
My question is why do you want to be extern? The best way here is to create a singleton, you should have all members as part of a class and avoid any global.
Hope this helps