Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Xcode 确定NSString是否包含NULL/NIL以外的值_Xcode_String_Null - Fatal编程技术网

Xcode 确定NSString是否包含NULL/NIL以外的值

Xcode 确定NSString是否包含NULL/NIL以外的值,xcode,string,null,Xcode,String,Null,我试图确定NSString的值是否包含NULL/nil以外的值,但我做得不正确: NSString *strWord; strWord = [NSString stringWithFormat: @"%@", [someArray objectAtIndex:x]]; // THIS IS WRONG: if (strWord != nil) { // do something } else { // don't do something } 感谢您的帮助! lq我怀疑您的问

我试图确定NSString的值是否包含NULL/nil以外的值,但我做得不正确:

NSString *strWord;
strWord = [NSString stringWithFormat: @"%@", [someArray objectAtIndex:x]];

// THIS IS WRONG:

if (strWord != nil) {
    // do something
} else {
   // don't do something
}
感谢您的帮助!
lq

我怀疑您的问题是
strWord
实际上包含
@(null)
,如果您将
nil
传递给
+stringWithFormat:
中的
%
格式标记,就会发生这种情况。相反,您需要检查
[someArray objectAtIndex:x]
是否为
nil
。幸运的是,有一条捷径。用这个代替:

NSString *strWord = [[someArray objectAtIndex:x] description];

这与
[NSString stringWithFormat:@“%@”,“someArray objectAtIndex:x]
相同,除非
[someArray objectAtIndex:x]
nil
,那么
strWord
将包含
nil
而不是
@(null)
。这是因为
%@
格式标记仅对传递的参数调用
-description
,但在特殊情况下
为nil
并将其转换为
@(null)
。但是,如果在对象上直接调用
-description
,将跳过
nil
检查,如果在
nil
上调用,只需返回
nil
,我怀疑您的问题是
strWord
实际上包含
@(null)
,这就是如果将
nil
传递给
%@
格式令牌(位于
+stringWithFormat:
中)时发生的情况。相反,您需要检查
[someArray objectAtIndex:x]
是否为
nil
。幸运的是,有一条捷径。用这个代替:

NSString *strWord = [[someArray objectAtIndex:x] description];

这与
[NSString stringWithFormat:@“%@”,“someArray objectAtIndex:x]
相同,除非
[someArray objectAtIndex:x]
nil
,那么
strWord
将包含
nil
而不是
@(null)
。这是因为
%@
格式标记仅对传递的参数调用
-description
,但在特殊情况下
为nil
并将其转换为
@(null)
。但是,直接调用对象上的
-description
将跳过
nil
检查,如果调用
nil
[someArray objectAtIndex:x]不能返回nil,除非
someArray
为nil(您不能将nil放入
NSArray
)中。它能返回的最近值是
+[NSNull null]
,这不是一回事。因此,如果您在这里得到“(null)”,这表明您的数组实际上是nil。我可能会在您的方法中更早地检查它,而不是查找@“(null)”的特例在字符串中。

@Kevin Ballard的技术很有用,但请记住,
[someArray objectAtIndex:x]
不能返回nil,除非
someArray
为nil(您不能将nil放入
NSArray
)。它能返回的最近值是
+[NSNull null]
,这不是一回事。因此如果您得到“(null)”这里,这表明您的数组实际上是nil。我可能会在您的方法前面检查它,而不是查找@“(null)”的特例在字符串中。

为什么这是错误的?对!=nil的检查在我看来完全合理。您可能希望在声明时设置strWord=nil。@斯蒂芬:在声明时设置
strWord=nil
根本不起作用,因为值将立即被
[nsstringwithformat:…]的结果覆盖
您正在通过调用
+stringWithFormat:
创建一个NSString。它永远不会返回nil。您打算测试什么?为什么这是错误的?对!=nil的检查在我看来完全合理。您可能希望在声明它时设置strWord=nil。@斯蒂芬:声明时设置strWord=nil根本没有任何作用,b因为该值将立即被
[NSString stringWithFormat:…]的结果覆盖
您正在通过调用
+stringWithFormat:
创建一个NSString。它永远不会返回nil。您打算测试什么?是的,它返回@“(null)”,但我无法找到检查@“(null)”的方法值,因为这不起作用:if(strWord!=null)…非常感谢!很抱歉报告使用:NSString*strWord=[[someArray objectAtIndex:x]description];仍然包含(null)并正在通过:if(strWord!=nil)检查。这意味着
[someArray objectAtIndex:x]
本身正在返回
@(null)
。或者
strWord
实际上有
@“
,这意味着你有
[NSNull null]
。你应该仔细看看
[someArray objectAtIndex:x]
正在返回。我首先通过阻止将nil值插入数组来解决此问题。不幸的是,这需要在我的方案中构造第三个数组。是的,它返回@“(null)”,但我无法找到检查@“(null)”值的方法,因为这不起作用:if(strWord!=null)…非常感谢!很抱歉报告使用:NSString*strWord=[[someArray objectAtIndex:x]description];仍然包含(null)并正在通过:if(strWord!=nil)检查。这意味着
[someArray objectAtIndex:x]
本身正在返回
@(null)
。或者
strWord
实际上有
@
,这意味着您使用了
[NSNull null]
。您应该仔细查看
[someArray objectAtIndex:x]
返回的内容。我通过首先防止将nil值插入数组来解决此问题。不幸的是,这需要在我的方案中构造第三个数组。