2

I want to set a property but I'm not sure what is the best way to do it. I can think of two ways to do this. By creating an object, storing it in a variable, and setting the property:

PropertyClass *myProperty = [[PropertyClass alloc] init];
self.myClassProperty = myProperty

Or by creating and setting in one line:

self.myClassProperty = [[PropertyClass alloc] init];

Are there any reasons one way is actually better/safer/faster/etc.? Is there a better way than the two I listed above? I'm not sure if this makes a difference but this would be the first time the property is set.

jscs
  • 63,694
  • 13
  • 151
  • 195
Justin Moser
  • 2,005
  • 3
  • 22
  • 28
  • 1
    If you check out the generate assembly for these you'll see that there is a subtle difference but I can;t read assembly so I have no idea what the effect is. – Paul.s Feb 17 '14 at 01:31

1 Answers1

1

They are equal but I always use the second one. Or the following

self.myClassProperty = [PropertyClass new];

The reason - in one line of code it is more difficult to make some stupid error. Also it is easier to read.

Avt
  • 16,927
  • 4
  • 52
  • 72