简体   繁体   中英

xcode 4 complaining that a key is an 'undeclared identifier' for no reason

Please help - this is driving me crazy... I have a number of lines of code very similar but all of a sudden Xcode has started to think that some of the lines are errors and I can't build my app. Here is some extract code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    cycleSettingsDate = [defaults objectForKey:kCycleSinceDateKey];
    cycleUnitFactor = [defaults boolForKey:kCycleUseMilesKey]? @"1.6" : @"1";
    runUnitFactor = [defaults boolForKey:kRunUseMilesKey]? @"1.6" : @"1";

    swimSettingsDate = [defaults objectForKey:kSwimSinceDateKey];

The item kCycleSinceDateKey is coloured pale blue as if it were a declared property in my header file like cycleSettingsDate - which it is not. The other three are coloured brown as you would expect a key to be colour-coded and they work perfectly. Only kCycleSinceDateKey does not work as expected.

I have the following in my header file which is an exact match with the plist identifier that it relates to:

#define kCycleSinceDate @"cyclesincedate"
#define kSwimSinceDateKey @"swimsincedate"
#define kRunUseMilesKey    @"runusemiles"
#define kCycleUseMilesKey  @"cycleusemiles"

I have exited Xcode and even restarted my mac for good measure but kCycleSinceDateKey is still underlined with the dotted red line and the same issue. Does anybody have any ideas?

Many thanks in advance...

In your code. you refer to it as kCycleSinceDateKey and in the define you refer to it as kCycleSinceDate.

Beyond the answers that pointed out the Key missing, I usually define my constants like this in .m. It's more compact. See When to use static string vs. #define

NSString * const kDefaultKeysCycleSinceDate = @"cyclesincedate";

and if I need to expose it elsewhere (usually why you define constants), do this in your header:

extern NSString* const kDefaultKeysCycleSinceDate;

Also:

Defining a globally accessible string in Objective-C and #define vs const and linking against frameworks

I would also start each with the same prefix (kDefaultKeys...) so autocomplete shows you all the possible keys after you type a few chars ...

You declared a constant kCycleSinceDate and use kCycleSinceDateKey instead. Note that the declared constant name doesn't end with Key .

So either change de delcaration into:

#define kCycleSinceDateKey @"cyclesincedate" // Add Key

Or change the line where it is used into:

cycleSettingsDate = [defaults objectForKey:kCycleSinceDate]; // Remove Key

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