0

This is the output of my string in console.

device type-------------(
mobile,
"<null>",
tablet

How to deal with the 2nd value. I tried the following way, but doesn't work app gets kill here on these lines.

if([dType isEqualToString:@"<null>"])
if([dType isEqualToString:@""])
if([dType isEqualToString:@"null"])

None of the above work. Please guide for the above. Thanks in advance.

iPhone Programmatically
  • 1,213
  • 2
  • 22
  • 52
  • 1
    Try this if[dType length]== 0; – Sudha Tiwari Feb 13 '13 at 05:14
  • On my first google hit I found: http://stackoverflow.com/questions/482276/how-can-i-test-an-nsstring-for-being-nil And http://stackoverflow.com/questions/2401234/objective-c-how-to-check-if-a-string-is-null – rptwsthi Feb 13 '13 at 05:21

6 Answers6

1
 if (dType == (NSString *)[NSNull null])
    {
       // your logic here
    }
Jerry Coffin
  • 455,417
  • 76
  • 598
  • 1,067
Rushabh
  • 3,165
  • 4
  • 27
  • 50
0

Instead of comparing the string. It's always advisable you check the length of string. Like :

[yourString length] == 0

And, check the condition. You can also check this. Hope this help.

Community
  • 1
  • 1
Rushi
  • 4,518
  • 4
  • 31
  • 46
  • it is giving error this way. -[NSNull length]: unrecognized selector sent to instance 0x1e6678. – iPhone Programmatically Feb 13 '13 at 05:26
  • @iPhone : [NSNull length]: this is wrong. You have to send your string here. For eg [yourStrin length] == 0 if it equals to 0, that means it's a null,nil or "" string. – Rushi Feb 13 '13 at 05:28
0

You can check if [string length] == 0. This will check if it's a valid but empty string (@"") as well as if its nil, since calling length on nil will also return 0.

Alternatively, you can try with [string isEqual:[NSNull null]]; also

Ajay Sharma
  • 4,499
  • 3
  • 30
  • 59
Vikas S Singh
  • 1,747
  • 1
  • 14
  • 29
0

I use two macros for this its in my Defines.h file

#define NULLVALUE(m)                    (m == nil ? @"" : m)
#define NULLVALUE1(m)                    (m == nil ? @" " : m)

It changes null value to "" or space " " easy to handle.

amar
  • 4,237
  • 6
  • 38
  • 50
0

you can check that like this

if ([YOUR_STRING isKindOfClass:[NSNull class]] || YOUR_STRING == nil ) {

}
SachinVsSachin
  • 6,322
  • 3
  • 32
  • 39
0
+(NSString *) removeNull:(NSString *) string {

    if ([string isEqual:[NSNull null]] ) {
        return @"";
    }

    if ([string isEqualToString:@"<null>"]) {
        return @"";
    }
    if ([string isEqualToString:@"null"]) {
        return @"";
    }
    @try {
        NSRange range = [string rangeOfString:@"null"];
        ////////NSLog(@"in removeNull : %d  >>>> %@",range.length, string);
        if (range.length > 0 || string == nil) {
            string = @"";
        }
        range = [string rangeOfString:@"<null>"];
        ////////NSLog(@"in removeNull : %d  >>>> %@",range.length, string);
        if (range.length > 0 || string == nil) {
            string = @"";
        }
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {

    }


    string = [self trimString:string];
    return string;
}

------------------------------ call like this NSString *strFb = [classname removeNull:YourString];