0

Does ObjC have something like in swift default value?

let a = b.name ?? "Empty"

I have an object with parameters and I have to check all parameters if they are not nil and if yes, set them to some default value.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Lifeplus
  • 531
  • 2
  • 9
  • 22
  • 2
    In C (and therefore Objective-C) `nil` evaluates to `false` so you can use the standard ternary operator `NSString *a = b.name ? b.name:@"Empty"` – Paulw11 Feb 05 '18 at 08:51
  • 3
    the ObjC alternative is `?:` for Swift `??`, like in your case that would look something like this in ObjC: `NSString *a = b.name ?: @"Empty";` – holex Feb 05 '18 at 08:59

1 Answers1

2

This is Ternary operator in most of the programming language. You can find details on this here - What does the question mark and the colon (?: ternary operator) mean in objective-c?

In Objective C ?? replaced by ?: Do follow the link or here is a short example if this comes to your help -

NSString *msgFullString = [NSString stringWithFormat:@"Message: %@", msg ? : @"Unknown error"];

You can also check if value exist or not using if(b.name). it will return true if b.name has a value or false if b.name is nil.

Hope this helps....

Shawon91
  • 200
  • 3
  • 9
  • 1
    at least if you copied my comment to make an own answer, you could have spend a little more time to format it properly. – holex Feb 05 '18 at 09:09
  • What is known by everyone, doesn't look like copy or stealing to me, anyone will answer like this given the swift code. – Shawon91 Feb 05 '18 at 09:12
  • you __actually__ coped and pasted my comment word-by-word, apart from you have cut off the middle part... geez. at least be a bit more humble, if you are not able to come out the answer on your own. – holex Feb 05 '18 at 09:14
  • geez, why are you making this a big issue or something ? i have said already - What is known by everyone, doesn't look like copy or stealing to me & also another way for checking nil is also given. what's the problem here ?!!! – Shawon91 Feb 05 '18 at 09:15
  • @holex sir, let's just be helpful we all, Here is ternary operators for you that everyone know :) https://en.wikipedia.org/wiki/Ternary_operation – Shawon91 Feb 05 '18 at 09:18
  • the problem is you being painfully high-minded and arrogant while aggressively chasing score – and in spite of you'd format your answer (=the copy of my comment) properly you are spending more energy and time on to convince me about what is common knowledge and what is not. over and out. – holex Feb 05 '18 at 09:20
  • i rarely comment in stackoverflow anyways and regarding score, you can check profile activity. I haven't even though about this before you have said this ! and i am being arrogant. Clap & adios. – Shawon91 Feb 05 '18 at 09:24