objective-c - XCode 中的全局字符串问题

标签 objective-c cocoa nsstring global

我对 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)

如果您知道这意味着什么和/或如何解决它,请帮助我。谢谢

最佳答案

您需要为 hi 提供全局定义。移动您的声明:

NSString *const hi = @"Hello";

到任何方法之外的某个地方。我不太确定您想要 button1: 做什么,但它对于您的实现来说似乎根本没有必要。

关于objective-c - XCode 中的全局字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5438220/

相关文章:

iphone - 在 UITableView 中显示带有 JSON 内容的 NSArray

objective-c - 从 NSString 中删除标记字符

ios - NSString hasPrefix : vs hasSuffix: which is less expensive?

ios - 将 nsstring 添加到主包 ios 中的本地 html 文件

ios - UIViewController类的未知类型名称

ios - instantiateViewControllerWithIdentifier 和 initWithNibName 的区别?

Cocoa:对象在不应该自动释放的时候被自动释放

objective-c - cocoa - -removeObserver :forKeyPath: and -removeObserver:forKeyPath:context:? 之间的细微差别

php - 使用 PHP 下载 JSON 的移动应用程序

macos - NSIndexSet.enumerateIndexesUsingBlock 使用 Swift Closure