本文介绍了Objective-C 如何检查字符串是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO我想检查我的数组 [clientDataArray objectForKey:@"ClientCompany"] 中的项目是否为 nil.

SOI 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

  • 警告:NSArray 可能不会响应 -objectForKey:
  • 警告:不同的比较Objective-C 类型 struct NSNull *并且 struct NSString * 缺少强制转换
  • warning: NSArray may not respond to -objectForKey:
  • warning: comparison of distinctObjective-C types struct NSNull *and struct 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?

推荐答案

您的第一个警告看起来像是您试图在 NSArray 上调用 objectForKey.这是行不通的,因为 NSArray 没有 objectForKey 方法.

Your first warning looks like you're trying to call objectForKey on an NSArray. Which isn't going to work, as NSArray doesn't have an objectForKey method.

至于第二个警告,您可以直接与 nil 进行比较,即:

As for the second warning you can just compare directly with nil, ie:

if (temp != nil)

或者因为 nil 等于 0,你也可以这样做:

or since nil is equivalent to 0, you can also just do:

if (temp)

这篇关于Objective-C 如何检查字符串是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 05:25