XCode 中的全局字符串问题

发布于 2024-10-26 00:56:21 字数 784 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夢归不見 2024-11-02 00:56:21

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

NSString *const hi = @"Hello";

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

You need to provide a global definition for hi. Move your declaration:

NSString *const hi = @"Hello";

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.

挽清梦 2024-11-02 00:56:21

我想卢克喜欢:
单击按钮一后将字符串设置为特定值,
单击按钮二后再次检索它。

AppController.h

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject{

    NSString * string;

}

-(IBAction)button1:(id)sender;
-(IBAction)button2:(id)sender;

@end

AppController.m

#import "AppController.h"

@implementation AppController

-(IBAction)button1:(id)sender
{
    string = @"Hello";
}

-(IBAction)button2:(id)sender;
{
    NSLog (@"%@", string);
}

@end

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

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject{

    NSString * string;

}

-(IBAction)button1:(id)sender;
-(IBAction)button2:(id)sender;

@end

AppController.m

#import "AppController.h"

@implementation AppController

-(IBAction)button1:(id)sender
{
    string = @"Hello";
}

-(IBAction)button2:(id)sender;
{
    NSLog (@"%@", string);
}

@end
爱,才寂寞 2024-11-02 00:56:21

当定义全局变量和常量字符串等时,我通常是这样做的:

MDAppController.h

#import <Cocoa/Cocoa.h>

extern NSString * const MDShouldShowInspectorKey;
extern NSString * const MDShouldShowViewOptionsKey;
extern BOOL MDShouldShowInspector;
extern BOOL MDShouldShowViewOptions;

@interface MDAppController : NSObject <NSApplicationDelegate> {
   IBOutlet NSWindow *window;
}
- (IBAction)hideInspector:(id)sender;
@end

MDAppController.m

#import "MDAppController.h"
NSString * const MDShouldShowInspectorKey   = @"MDShouldShowInspector";
NSString * const MDShouldShowViewOptionsKey = @"MDShouldShowViewOptions";
BOOL MDShouldShowInspector = NO; // default value
BOOL MDShouldShowViewOptions = YES;  // default value

@implementation MDAppController
+ (void)initialize {
    NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
    [defaultValues setObject:
        [NSNumber numberWithBool:MDShouldShowInspector]
                      forKey:MDShouldShowInspectorKey];

    [defaultValues setObject:
         [NSNumber numberWithBool:MDShouldShowViewOptions] 
                      forKey:MDShouldShowViewOptionsKey];

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
    MDShouldShowInspector = [[uD objectForKey:MDShouldShowInspectorKey] boolValue];
    MDShouldShowViewOptions = [[uD objectForKey:MDShouldShowViewOptionsKey] boolValue];
}
- (IBAction)hideInspector:(id)sender {
    NSLog(@"MDShouldShowViewOptionsKey == %@", MDShouldShowViewOptionsKey);
    MDShouldShowInspector = NO;
    [[NSUserDefaults standardUserDefaults]
               setObject:[NSNumber numberWithBool:MDShouldShowInspector]
                    forKey:MDShouldShowInspectorKey];
}
@end

When defining global variables and constant strings, etc., this is usually how I do it:

MDAppController.h:

#import <Cocoa/Cocoa.h>

extern NSString * const MDShouldShowInspectorKey;
extern NSString * const MDShouldShowViewOptionsKey;
extern BOOL MDShouldShowInspector;
extern BOOL MDShouldShowViewOptions;

@interface MDAppController : NSObject <NSApplicationDelegate> {
   IBOutlet NSWindow *window;
}
- (IBAction)hideInspector:(id)sender;
@end

MDAppController.m:

#import "MDAppController.h"
NSString * const MDShouldShowInspectorKey   = @"MDShouldShowInspector";
NSString * const MDShouldShowViewOptionsKey = @"MDShouldShowViewOptions";
BOOL MDShouldShowInspector = NO; // default value
BOOL MDShouldShowViewOptions = YES;  // default value

@implementation MDAppController
+ (void)initialize {
    NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
    [defaultValues setObject:
        [NSNumber numberWithBool:MDShouldShowInspector]
                      forKey:MDShouldShowInspectorKey];

    [defaultValues setObject:
         [NSNumber numberWithBool:MDShouldShowViewOptions] 
                      forKey:MDShouldShowViewOptionsKey];

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
    MDShouldShowInspector = [[uD objectForKey:MDShouldShowInspectorKey] boolValue];
    MDShouldShowViewOptions = [[uD objectForKey:MDShouldShowViewOptionsKey] boolValue];
}
- (IBAction)hideInspector:(id)sender {
    NSLog(@"MDShouldShowViewOptionsKey == %@", MDShouldShowViewOptionsKey);
    MDShouldShowInspector = NO;
    [[NSUserDefaults standardUserDefaults]
               setObject:[NSNumber numberWithBool:MDShouldShowInspector]
                    forKey:MDShouldShowInspectorKey];
}
@end
笛声青案梦长安 2024-11-02 00:56:21

我的问题是你为什么想成为外部人士?这里最好的方法是创建一个单例,您应该将所有成员作为类的一部分并避免任何全局成员。
希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文