You forgot the @
-sign before the strings in NSLocalizedString
.
Replace "Heads up re foobar code %d."
with @"Heads up re foobar code %d."
.
Question
I have an enum property:
typedef enum syncCodeTypes {
kCodeNull,
kCodeFoo,
kCodeBar,
kCodeDone
} syncCodeType;
//...
@property syncCodeType syncCode;
I use it in a stringWithFormat:
:
[self showAlertWithMessage:NSLocalizedString(@"Sync Error", @"Sync Error") andInfo:[NSString stringWithFormat:NSLocalizedString("Heads up re foobar code %d.", "Heads up re foobar code %d."), self.syncCode]];
…and get this warning:
Passing argument 1 of localizedStringForKey:value:table from incompatible pointer type.
Same thing happens if I substitue the unsigned conversion specifier (%u
instead of %d
).
The compiler doesn’t like %lu
, %ld
, %llu
, or %lld
either.
Other posts regarding a related language advise that enums are neither signed nor unsigned, so I tried explicitly casting the enum to a signed and to an unsigned integer — and got exactly the same error message:
NSInteger iSyncCode = self.syncCode;
[self showAlertWithMessage:NSLocalizedString(@"Sync Error", @"Sync Error") andInfo:[NSString stringWithFormat:NSLocalizedString(“Heads up re foobar code %d.", “Heads up re foobar code %d."), iSyncCode]];
// compiler still annoyed
NSUInteger uSyncCode = self.syncCode;
[self showAlertWithMessage:NSLocalizedString(@"Sync Error", @"Sync Error") andInfo:[NSString stringWithFormat:NSLocalizedString(“Heads up re foobar code %u.”, “Heads up re foobar code %u.”), uSyncCode]];
// compiler still annoyed
In runtime there’s no problem — for now. But I'd like to be kosher. Any suggestions?
Solution
You forgot the @
-sign before the strings in NSLocalizedString
.
Replace "Heads up re foobar code %d."
with @"Heads up re foobar code %d."
.
OTHER TIPS
The %d
format specifier is for int
variables. But self.syncCode
is not an int
, it is an syncCodeType
.
You need to cast the value to an int
:
(int)self.syncCode
or the whole line:
[self showAlertWithMessage:NSLocalizedString(@"Sync Error", @"Sync Error") andInfo:[NSString stringWithFormat:NSLocalizedString(@"Heads up re foobar code %d.", @"Heads up re foobar code %d."), (int)self.syncCode]];
This will make the compiler happy.
P.S. And as phix23 points out, you need to pass NSString
literals, not C-string literals to NSLocalizedString
.