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.
题
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.
解决方案
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.