Objective-C how to check if a string is null

主宰稳场 提交于 2019-12-05 13:31:37

问题


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?


回答1:


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)



回答2:


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

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



回答3:


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.




回答4:


The best solution to test NULL or Empty values:

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



回答5:


Quiet simple

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



回答6:


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!




回答7:


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.




回答8:


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




回答9:


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;


来源:https://stackoverflow.com/questions/2401234/objective-c-how-to-check-if-a-string-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!