开发者

Problem with global strings in XCode

开发者 https://www.devze.com 2023-02-20 01:31 出处:网络
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

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);
}

How开发者_StackOverflow社区ever 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


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号