I'm trying to achieve the equivalent of the following C# code:
someStringValue = someStringValue ?? string.Empty;
Where if someStringValue
is null, a value of string.Empty
(the empty string: ""
) will be assigned. How do I achieve this in Objective-C? Is my only option:
if(!someStringValue)
someStringValue = @"";
Solution thanks to @Dave DeLong:
someStringValue = someStringValue ?: @"";
Simple, using ternary operator.
someStringValue = someStringValue ? someStringValue : @"";
Or if you want a macro, you can do that too.
#if !defined(StringOrEmpty)
#define StringOrEmpty(A) ({ __typeof__(A) __a = (A); __a ? __a : @""; })
#endif
Sample usage:
someStringValue = StringOrEmpty(someStringValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With