简体   繁体   中英

In Xcode and IOS projects I can't use constants. Got linker build errors

I started a new iOS project in Xcode. Added two files: "constants.h" and "constants.m" with this content I saw here in other articles:

Constants.h

#import <Foundation/Foundation.h>

/// Holds the username for connection to the API
extern NSString * const PSUsername_preferences;

/// Holds the password for connection to the API
extern NSString *const PSPassword_preferences; 

///Holds the (hopefully) secure server URL. (Defaults to https://API.SomewhatURL.de)
extern NSString *const PSServername_preferences;

And the constants.m:

#import "Constants.h" 

NSString *const PSUsername_preference = @"username_preferences";
NSString *const PSPassword_preference = @"password_preferences";
NSString *const PSServer_preference = @"servername_preferences";

I am sure, the .m file is assigned to build for my ios-target. I am also sure, the file is added under Build Configurations- Compile Sources.

Next I added the "Constats.h" to my pch - file:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
#endif

and I can assign one of these in let say AddDelegate :

 NSString * value = PSUsername_preferences;

Nice. But: If I try to build this, I get a strange linker error:

Undefined symbols for architecture i386: "_PSUsername_preferences", referenced from: -[AppDelegate applicationWillResignActive:] in AppDelegate.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

This happens with a new created project just for this case. My build settings seem OK and I already checked 'Architectures' path under Build-Settings. All of they show 'ArmV7' but not i386, so whats going on?

I tested it on Xcode 4.6.3 as well as under Xcode 5 .

Read your constant names letter by letter. Or add some whitespace for easier readability.

extern NSString * const PSUsername_preferences;
       NSString * const PSUsername_preference

You see the difference? Add the letter s at the end of your definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM