Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 当测试字符串位置是否为空时,NSString测试不起作用_Iphone_Objective C_String - Fatal编程技术网

Iphone 当测试字符串位置是否为空时,NSString测试不起作用

Iphone 当测试字符串位置是否为空时,NSString测试不起作用,iphone,objective-c,string,Iphone,Objective C,String,好吧,我知道这类问题有上千个,但这些建议似乎都不起作用,所以我不得不提出另一个问题,因为这已经开始打破我的b**L。现在问题和背景: 我从iPhone获得一个位置,但如果该位置不可用或部分位置不可用,我会将字符串重新初始化为@“”,而不是丑陋的@“(null)”,因为我将该位置上载到服务器 这是变量的分配: NSString *country = [NSString stringWithFormat:@"%@", placemarkFound.country];

好吧,我知道这类问题有上千个,但这些建议似乎都不起作用,所以我不得不提出另一个问题,因为这已经开始打破我的b**L。现在问题和背景:

我从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];
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)

谢谢你的帮助

您需要做的不是
country==@”(null)
而是:
[country isEqual:@”(null)]
(或
[country isEqualToString:@”(null)]
)。
=
操作符测试指针是否相等,但您需要检查对象值是否相等

编辑:

因此,第二种方法可能是(使用固定支票),但我建议取消支票:

if ([@"(null)" isEqual:myStringToTest]) { ... }

这样,调用
isEqual:
的对象保证始终是有效的对象(表示
@”(null)
)的静态对象。

而不是
country==@”(null)
您需要做的是:
[country isEqual:@(null)]
(或
[country isEqualToString:@”(null)]
=
操作符测试指针是否相等,但您需要检查对象值是否相等

编辑:

因此,第二种方法可能是(使用固定支票),但我建议取消支票:

if ([@"(null)" isEqual:myStringToTest]) { ... }

这样,调用
isEqual:
的对象保证始终是有效的对象(表示
@(null)”
的静态对象)<代码>如果((NSNull*)国家==[NSNull-null])

您也可以使用这个
如果((NSNull*)country==[NSNull-null])

更仔细地阅读您的问题,我注意到您在为变量分配字符串时使用了
+[NSString stringWithFormat::

        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];
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)

不起作用:
+[NSString stringWithFormat:
始终返回非
nil
字符串。在特定代码中,如果
placemarkFound.country==nil
,则
country
变量包含
nil
的字符串表示,即
(null)

既然您已经说过您没有特别的理由使用
+stringWithFormat:
,并且假设所有属性/结构成员都是字符串,那么这里有一个解决方案:

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;
请注意,变量包含空字符串,除非其相应的属性/结构成员不同于
nil

对于这种情况,有一个方便的捷径。您可以使用以下使用三元条件运算符的代码,而不是上面的代码:

NSString *country = (placemarkFound.country ? : @"");
NSString *postalCode = (placemarkFound.postalCode ? : @"");
NSString *locality = (placemarkFound.locality ? : @"");
NSString *thoroughfare = (placemarkFound.thoroughfare ? : @"");
NSString *subThoroughfare = (placemarkFound.subThoroughfare ? : @"");
解释它:

NSString *country = (placemarkFound.country ? : @"");

表示以下内容:如果
placemarkFound.country
nil
不同,则将其分配给
country
变量。否则,将空字符串指定给
country
变量。

仔细阅读您的问题,我注意到您在将字符串指定给变量时使用了
+[NSString stringWithFormat:

        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];
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)

不起作用:
+[NSString stringWithFormat:
始终返回非
nil
字符串。在特定代码中,如果
placemarkFound.country==nil
,则
country
变量包含
nil
的字符串表示,即
(null)

既然您已经说过您没有特别的理由使用
+stringWithFormat:
,并且假设所有属性/结构成员都是字符串,那么这里有一个解决方案:

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;
请注意,变量包含空字符串,除非其相应的属性/结构成员不同于
nil

对于这种情况,有一个方便的捷径。您可以使用以下使用三元条件运算符的代码,而不是上面的代码:

NSString *country = (placemarkFound.country ? : @"");
NSString *postalCode = (placemarkFound.postalCode ? : @"");
NSString *locality = (placemarkFound.locality ? : @"");
NSString *thoroughfare = (placemarkFound.thoroughfare ? : @"");
NSString *subThoroughfare = (placemarkFound.subThoroughfare ? : @"");
解释它:

NSString *country = (placemarkFound.country ? : @"");

表示以下内容:如果
placemarkFound.country
nil
不同,则将其分配给
country
变量。否则,将空字符串分配给
country
变量。

我也尝试修剪字符串,但没有效果。我浏览了你的文章,但没有看到问题。测试空字符串就像
if(!s)…
一样简单。除此之外,我无法开始理解您试图实现的目标。问题是为什么这些测试不起作用,以及如何使它们起作用。@Joze:该测试不起作用,因为您正在将这些属性/结构成员转换为
+[NSString stringWithFormat:
,,它总是返回一个不同于
nil
的字符串对象。但是为什么要用
+stringWithFormat:
创建这些字符串?我也尝试过修剪字符串,但没有任何效果。我浏览了你的文章,但没有看到任何问题。测试空字符串就像
if(!s)
一样简单。除此之外,我无法开始理解您试图实现的目标。问题是为什么这些测试不起作用,以及如何使它们起作用。@Joze:该测试不起作用,因为您正在将这些属性/结构成员转换为
+[NSString stringWithFormat:
,,它总是返回一个不同于
nil
的字符串对象。但是为什么要用
+stringWithFormat:
创建这些字符串?我不理解b的理由