Objective-C 如何检查字符串是否为空

发布于 2024-08-24 10:58:30 字数 556 浏览 9 评论 0原文

所以 我想检查数组 [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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

2024-08-31 10:58:30

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

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

if (temp != nil)

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

if (temp)

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.

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

if (temp != nil)

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

if (temp)
爱人如己 2024-08-31 10:58:30

在尝试了所有选项之后,我认为这是 comapre NSString null 的最佳选择

if ( temp != ( NSString *) [ NSNull null ] )
{
  // do some thing
}

After trying all the options, i think this is the best option to comapre NSString null

if ( temp != ( NSString *) [ NSNull null ] )
{
  // do some thing
}
救星 2024-08-31 10:58:30

前面给出的两个答案都忽略了一个基本点:您不能将 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 get nil out of an array. Using NSNull as a placeholder in an array is the correct thing to do, however your variable temp then cannot be declared as an NSString *, as it might not be one. Use either NSObject * or id as the type of the variable to suppress the comparison warning.

浅忆流年 2024-08-31 10:58:30

测试 NULL 或空值的最佳解决方案:

if (yourObj.yourString == (NSString*) [NSNull null] || yourObj.yourString.length == 0 )
{
   yourObj.yourString = @"";
}

The best solution to test NULL or Empty values:

if (yourObj.yourString == (NSString*) [NSNull null] || yourObj.yourString.length == 0 )
{
   yourObj.yourString = @"";
}
空城旧梦 2024-08-31 10:58:30

安静简单

if (myString == nil){
     NSLog(@"myString is null");
}

Quiet simple

if (myString == nil){
     NSLog(@"myString is null");
}
杀お生予夺 2024-08-31 10:58:30

我认为问题是(我的意思是第二个警告)是您正在比较 NSString 对象,该对象可以设置为 null 与 NSNull 对象。

您是否尝试过检查 null 的常用 C 方法?

if(temp) {
// It's not null, do something.
}

我对此不是 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?

if(temp) {
// It's not null, do something.
}

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!

浅暮の光 2024-08-31 10:58:30

NSNull 单例可用于构建“交错”数组,例如 obj0、obj1、obj2、NSNull、obj3、NSNull、...、nil。

警告:“NSArray”可能不会响应
到'-objectForKey:'

NSArray 未实现 objectForKey
您的代码将在运行时崩溃(如果 clientDataArray 已分配并初始化)
您可以通过索引访问数组元素(objectAtIndex:)。
如果您需要将对象与键关联起来,请查看 NSDictionary

The NSNull singleton can be used to build "interleaved" arrays like obj0, obj1, obj2, NSNull, obj3, NSNull, ..., nil.

warning: 'NSArray' may not respond
to'-objectForKey:'

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.

奢欲 2024-08-31 10:58:30

如果(温度!= nil)

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

如果(临时)

我不是一个 Obj-C 或 Cocoa 专家,在 C/C++ 中你可以使用一些东西就像:

if(somePtr != NULL)

并且,除非您明确地将其设置为 NULL 或更改它所指向的内容,
你可以确定,事实上,它不是 Null 或 Null(无论你在寻找什么......)

但我注意到(无论如何我的个人经验)在 Obj-C 中,

如果你做了类似的事情:
if(someObj != nil) 或者相反,

没有保证,它会告诉你它是真实状态...

所以(在处理了一堆崩溃之后)当我处理 Obj-C (BS) 时,为了安全起见,我总是设置一个 BOOL 或标志来跟踪它的状态,所以你最终不会释放,对不起......“释放”已经被释放的东西......

if (temp != nil)

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

if (temp)

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...

┊风居住的梦幻卍 2024-08-31 10:58:30

尽管这个问题很老,但我希望我的回答对其他人有所帮助。

你可以试试这个

temp = [clientDataArray objectForKey:@"ClientCompany"];
if (![temp isKindOfClass:[NSNull class]]) infofieldCompany.text = temp;

Even though this question is old, I hope My Answer will help some one else.

You can try this

temp = [clientDataArray objectForKey:@"ClientCompany"];
if (![temp isKindOfClass:[NSNull class]]) infofieldCompany.text = temp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文