NSString tests not working while testing if string location is null
Okay I know there are like a thousand questions of this type, but none of those suggestions seem to work so I am forced to make yet another question about this since this is starting to break my b**ls. Now the question and context:
I get a location from the iPhone, but in the case that the location is not available or part of it is not available I reinitialize the string to @"" instead of a ugly @"(null)" because this location I get it uploaded into a server.
This is the allocation of the variables:
NSString *country = [NSString stringWithFormat:@"%@", placemarkFound.country];
NSString *postalCode = [NSString stringWithFormat:@"%@", placemarkFound.postalCode];
NSString *locality = [NSString stringWithFormat:@"%@", placemarkFound.locality];
NSString *thoroughfare = [NSString stringWithFormat:@"%@", placemarkFound.thoroughfare];
NSString *subThoroughfare = [NSString stringWithFormat:@"%@", placemarkFound.subThoroughfare];
These are the tests I'm currently making:
if (country == (id)[NSNull null] || country.length == 0) {
NSLog(@"Entered first if for (null)");
country = @"";
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (postalCode == (id)[NSNull null] || postalCode.length == 0) {
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (locality == (id)[NSNull null] || locality.length == 0) {
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (thoroughfare == (id)[NSNull null] || thoroughfare.length == 0) {
thoroughfare = @"";
subThoroughfare = @"";
} else if (subThoroughfare == (id)[NSNull null] || subThoroughfare.length == 0) {
subThoroughfare = @"";
}
Second:
if (country == @"(null)") {
NSLog(@"Entered first if for (null)");
country = @"";
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (postalCode == @"(null)") {
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (locality == @"(null)") {
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (thoroughfare == @"(null)") {
thoroughfare = @"";
subThoroughfare = @"";
} else if (subThoroughfare == @"(null)") {
subThoroughfare = @"";
}
Third:
if (!country) {
NSLog(@"Entered first if for (null)");
country = @"";
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (!postalCode) {
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (!locality) {
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (!thoroughfare) {
thoroughfare = @"";
subThoroughfare = @"";
} else if (!subThoroughfare) {
subThoroughfare = @"";
}
Fourth:
With a class:
static inline BOOL IsEmpty(id 开发者_如何转开发thing) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
if (IsEmpty(country)) {
NSLog(@"Entered first if for (null)");
country = @"";
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (IsEmpty(postalCode)) {
postalCode = @"";
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (IsEmpty(locality)) {
locality = @"";
thoroughfare = @"";
subThoroughfare = @"";
} else if (IsEmpty(thoroughfare)) {
thoroughfare = @"";
subThoroughfare = @"";
} else if (IsEmpty(subThoroughfare)) {
subThoroughfare = @"";
}
So there are two possibilities, I'm making something horribly wrong or there is a much simpler way to do this. I NSLogged the variables to this output:
Variables before testing, country: (null)
postalCode: (null)
Thank you for your help!!
Instead of country == @"(null)"
you need to do: [country isEqual:@"(null)"]
(or [country isEqualToString:@"(null)"]
). The ==
operator tests for pointer equality, but you want to check for object value equality.
Edit:
So, the second way is probably the way to go (with the fixed check), but I'd recommend to reverse the check:
if ([@"(null)" isEqual:myStringToTest]) { ... }
This way, the object on which isEqual:
is called is guaranteed to be always a valid object (the static object representing @"(null)"
).
Reading your question more carefully, I’ve noticed you’re using +[NSString stringWithFormat:]
when assigning the strings to your variables:
NSString *country = [NSString stringWithFormat:@"%@", placemarkFound.country];
NSString *postalCode = [NSString stringWithFormat:@"%@", placemarkFound.postalCode];
NSString *locality = [NSString stringWithFormat:@"%@", placemarkFound.locality];
NSString *thoroughfare = [NSString stringWithFormat:@"%@", placemarkFound.thoroughfare];
NSString *subThoroughfare = [NSString stringWithFormat:@"%@", placemarkFound.subThoroughfare];
This is the reason why a test like
if (! country)
or
if (country == nil)
doesn’t work: +[NSString stringWithFormat:]
always returns a non-nil
string. In your particular code, if placemarkFound.country == nil
, then your country
variable contains a string representation of nil
, namely (null)
.
Since you’ve said you have no particular reason to use +stringWithFormat:
, and assuming all properties/struct members are strings, here’s one solution to your problem:
NSString *country = @"";
NSString *postalCode = @"";
NSString *locality = @"";
NSString *thoroughfare = @"";
NSString *subThoroughfare = @"";
if (placemarkFound.country) country = placemarkFound.country;
if (placemarkFound.postalCode) postalCode = placemarkFound.postalCode;
if (placemarkFound.locality) locality = placemarkFound.locality;
if (placemarkFound.thoroughfare) thoroughfare = placemarkFound.thoroughfare;
if (placemarkFound.subThoroughfare) subThoroughfare = placemarkFound.subThoroughfare;
Note that the variables contain an empty string unless their corresponding properties/struct members are different from nil
.
There’s a handy shortcut for this situation. Instead of the code above, you can use the following code that employs the ternary conditional operator:
NSString *country = (placemarkFound.country ? : @"");
NSString *postalCode = (placemarkFound.postalCode ? : @"");
NSString *locality = (placemarkFound.locality ? : @"");
NSString *thoroughfare = (placemarkFound.thoroughfare ? : @"");
NSString *subThoroughfare = (placemarkFound.subThoroughfare ? : @"");
Explaining it:
NSString *country = (placemarkFound.country ? : @"");
means the following: if placemarkFound.country
is different from nil
, assign it to the country
variable. Otherwise, assign an empty string to the country
variable.
you can use this too... if( (NSNull *)country == [NSNull null] )
精彩评论