Home > DeveloperSection > Forums > Assign blank if String is NULL in Objective-C
Maria Susan

Total Post:29

Posted on    December-07-2015 10:28 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 569  View(s)
Rate this:

I am new in iOS app development. I want to print string if string variable is null.
I am trying this code:(C#)

strObj = strObj??string.Empty;

In this code if strObj is null, value of string.Empty (for ex: string: "") will be assigned.
So how can I achieve this in Objective-C?

Tarun Kumar

Total Post:214

Posted on    December-08-2015 11:16 PM

You can check null string like my example, I am checking first name and last name if any one is not available then it will not assign in personName. Use it as your need;
Here is my example:

NSString *personName;
if(person.firstName.lenght !=0 && person.lastName.length !=0)
   personName = [[NSString alloc] initWithFormat:@"%@ %@", person.firstName, person.lastName];
else if(person.lastName.length == 0)
   personName = person.firstName;
   personName = person.lastName;

Don't want to miss updates? Please click the below button!

Follow MindStick