Хорошо, я знаю, что существует около тысячи вопросов такого типа, но ни одно из этих предложений, похоже, не работает, поэтому я вынужден задать еще один вопрос по этому поводу, поскольку это начинает ломать мои мысли.Теперь вопрос и контекст:
Я получаю местоположение от iPhone, но в случае, если местоположение недоступно или его часть недоступна, я переинициализирую строку в @ "" вместо уродливого @"(null)", потому что это местоположение, которое я загружаю на сервер.
Это распределение переменных:
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];
Это тесты, которые я сейчас делаю:
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 = @"";
}
Второй:
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 = @"";
}
Третий:
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 = @"";
}
Четвертый:
С классом:
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 = @"";
}
Так что есть две возможности, я делаю что-то ужасно неправильно или есть гораздо более простой способ сделать это.Я переместил переменные в этот вывод:
Variables before testing, country: (null)
postalCode: (null)
Спасибо за вашу помощь !!