Objective-C 如何检查字符串是否为空
所以 我想检查数组 [clientDataArray objectForKey:@"ClientCompany"]
中的项目是否为 nil
。
temp = [clientDataArray objectForKey:@"ClientCompany"];
if (temp != [NSNull null]) infofieldCompany.text = temp;
到目前为止,我已经能够通过上面的代码实现这一点,但它确实给了我警告
- warning:
NSArray
may not respond to-objectForKey:
- warning: Comparison of different Objective-C 类型
struct NSNull *
和 struct NSString * 缺少强制转换
我主要感兴趣的是第二个警告,但第一个警告也让我感兴趣。 我应该如何调整我的上述代码?
SO
I wish to check to see if the item in my array [clientDataArray objectForKey:@"ClientCompany"]
is nil
.
temp = [clientDataArray objectForKey:@"ClientCompany"];
if (temp != [NSNull null]) infofieldCompany.text = temp;
So far I have been able to achieve this through the above code, but it does give me the warnings
- warning:
NSArray
may not respond to-objectForKey:
- warning: comparison of distinct
Objective-C typesstruct NSNull *
andstruct NSString *
lacks a cast
My main interest is the second warning, but the first warning also interest me.
How should I adapt my above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的第一个警告看起来像是您试图在
NSArray
上调用objectForKey
。这是行不通的,因为NSArray
没有objectForKey
方法。至于第二个警告,你可以直接与 nil 进行比较,即:
或者因为 nil 相当于 0,你也可以这样做:
Your first warning looks like you're trying to call
objectForKey
on anNSArray
. Which isn't going to work, asNSArray
doesn't have anobjectForKey
method.As for the second warning you can just compare directly with nil, ie:
or since nil is equivalent to 0, you can also just do:
在尝试了所有选项之后,我认为这是 comapre NSString null 的最佳选择
After trying all the options, i think this is the best option to comapre NSString null
前面给出的两个答案都忽略了一个基本点:您不能将 nil 放入数组中,因此您永远无法从数组中获取 nil 。 使用
NSNull
作为数组中的占位符是正确的做法,但是您的变量temp
则不能声明为NSString *
,因为它可能不是一个。使用NSObject *
或id
作为变量的类型来抑制比较警告。Both of the answers previously given missed a fundamental point: you can't put
nil
into an array, so you'll never getnil
out of an array. UsingNSNull
as a placeholder in an array is the correct thing to do, however your variabletemp
then cannot be declared as anNSString *
, as it might not be one. Use eitherNSObject *
orid
as the type of the variable to suppress the comparison warning.测试 NULL 或空值的最佳解决方案:
The best solution to test NULL or Empty values:
安静简单
Quiet simple
我认为问题是(我的意思是第二个警告)是您正在比较 NSString 对象,该对象可以设置为 null 与 NSNull 对象。
您是否尝试过检查 null 的常用 C 方法?
我对此不是 100% 确定,但你可以尝试一下。如果您这样做了,很抱歉无法提供更多有用的信息。
祝你好运!
I think the problem is (I mean the second warning) is that you're comparing NSString object, which could be set to null to an NSNull object.
Have you tried the usual C way of checking for null?
I'm not 100% sure about this one, but you could try it. If you did, sorry that couldn't provide more useful information.
Good luck!
NSNull 单例可用于构建“交错”数组,例如 obj0、obj1、obj2、NSNull、obj3、NSNull、...、nil。
NSArray 未实现
objectForKey
。您的代码将在运行时崩溃(如果
clientDataArray
已分配并初始化)您可以通过索引访问数组元素(objectAtIndex:)。
如果您需要将对象与键关联起来,请查看 NSDictionary。
The
NSNull
singleton can be used to build "interleaved" arrays like obj0, obj1, obj2, NSNull, obj3, NSNull, ..., nil.NSArray does not implement
objectForKey
.Your code will crash at runtime (if
clientDataArray
has been allocated and initialized)You can access array elements by index (objectAtIndex:).
If you need to associate objects with keys, take a look at NSDictionary.
我不是一个 Obj-C 或 Cocoa 专家,在 C/C++ 中你可以使用一些东西就像:
if(somePtr != NULL)
并且,除非您明确地将其设置为 NULL 或更改它所指向的内容,
你可以确定,事实上,它不是 Null 或 Null(无论你在寻找什么......)
但我注意到(无论如何我的个人经验)在 Obj-C 中,
如果你做了类似的事情:
if(someObj != nil) 或者相反,
没有保证,它会告诉你它是真实状态...
所以(在处理了一堆崩溃之后)当我处理 Obj-C (BS) 时,为了安全起见,我总是设置一个 BOOL 或标志来跟踪它的状态,所以你最终不会释放,对不起......“释放”已经被释放的东西......
I am not by any stretch of the imagination a Obj-C or Cocoa expert, in C/C++ you can use something like:
if(somePtr != NULL)
And, unless you explicitly, set it to NULL or changed what it was pointing to,
you can be certain, that it is, in fact, Not Null or Null (what-ever you were looking for...)
But I have noticed (my personal experience anyway) that in Obj-C,
If you do something like:
if(someObj != nil) or the converse,
There's no Guarantee, it will tell you it's REAL status...
So (after dealing with a bunch of crashes) when I deal with Obj-C (BS), to be safe, I always have a BOOL or flag set up to track it's status, so you don't end up Free-ing, excuse me... "Release-ing" something that has already be deallocated...
尽管这个问题很老,但我希望我的回答对其他人有所帮助。
你可以试试这个
Even though this question is old, I hope My Answer will help some one else.
You can try this