0

This is more like general question: Is there a way to assign a value like an if statement? I have a dictionary and I want to check if key exists and then assign. For example:

if((_attr.val = [self.dic objectForKey:@"name"]))

And i know that if key exist in _attr.val i will get object, if not i will get null. Great, but if statemant requires to_do_something after it, but in my case this is it - all what i want to do.

This is probably a silly question, but i'm having trouble with finding a solution.

I want to find something like ASSERT() known from c++.

Jakub
  • 13,712
  • 17
  • 82
  • 139

1 Answers1

1

The if makes sense when you want to create a conditional path. In this case you just want to assign the value to _attr.val, still you should check if everything went was you expected. I would go with:

_attr.val = [self.dic objectForKey:@"name"]
if(!_attr.val){
  // Conditional case for a nil value.
}

If it really doesn't matter the end result (either a value or nil) just go:

_attr.val = [self.dic objectForKey:@"name"]

Either way, if you started this question with the if in mind it seems that you want to make some sort of check.

With NSAssert:

_attr.val = [self.dic objectForKey:@"name"]
NSAssert(_attr.val,@"Value is nil"); 
Rui Peres
  • 25,741
  • 9
  • 87
  • 137