2011-03-25 102 views
1

我對可可和Xcode非常陌生,我已經完成了一些基本的C編碼,但是我非常吮吸Objective-C和可可,所以請原諒我犯的任何愚蠢的錯誤。我的問題是我正在使用這些全局變量。 我在頭文件中聲明一個全局的NSString變量,它的主文件中使用像這樣:XCode中的全局字符串問題

//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) 

如果你知道這意味着什麼和/或如何解決它,請幫助我。謝謝

+1

你的問題不在於你使用編譯的代碼是什麼;它是與代碼。 – 2011-03-26 00:33:54

+0

對不起,您是對的。編輯它 – Luke 2011-03-27 12:15:10

回答

2

我認爲盧克喜歡:
設置單擊該字符串後按鈕一個特定的值,
和按鈕被點擊後兩位再次找回它。

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 
+0

謝謝!它現在完美的工作:) – Luke 2011-03-26 16:36:19

4

您需要提供hi的全球定義。移動你的宣言:

NSString *const hi = @"Hello"; 

在某處之外的任何方法。我不確定你想要什麼button1:要做,但它似乎並不需要你的實施。

0

我的問題是爲什麼你想成爲extern?這裏最好的辦法是創建一個單例,你應該把所有成員都作爲一個類的一部分,並避免任何全局。 希望這有助於

+1

單身人士是全球性的。從全局變量到全局對象的變化如何解決提問者的問題? – 2011-03-26 00:34:42

2

當定義全局變量和常量字符串,等等,這通常是我如何做到這一點:

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