How can I avoid redundancy when declaring new class attributes in Objective-C?

In my code, every time I need a new object attribute for my class, I usually copy / paste its name in 4 different places!

  • Ad in header file ( NSObject * myObject;)
  • Line @property()
  • String @synthesize()in implementation
  • Release it under dealloc:(only for objects, of course)

I do this because it does not work because I fully understand what is happening. I know that the declaration in the header file allows other classes to see its attributes, the property specifier determines how its getter / setter methods will be built. And the synthesis line actually creates these getter / setter methods. I also know that primitive types should use ( nonatomic,assign) instead of ( nonatomic,retain), but I don't know when I should omit non-atomic ones.

What can I do to avoid redundancy in my code. If I change or add a variable to my class, I have to check 4 different places and it gets old very quickly. Are there any key touches to speed up this process? Are there lines of code that I can simplify or combine to get the same result?

+3
3
+5

Clang (Ships XCode 4, XCode 3) default @synthesize, ivar . ivar , . , , @property dealloc

. atom - , , . Atomic , , - , - . Atomic .

+3

, . , . , - , NSObject *myObject; , , .

, @property @interface , . , , API ( , , ) .

@synthesize @property. , . , Core Data , @dynamic. , @property - , myObject_, object, .

Finally, you send the instance variable -releaseto -dealloc- for the property of the type object labeled retainor copy- because you said you would manage your memory. You do not release real estate; you release repository. If you implemented the storage in some other way, you will clean it in another way.

+1
source

Source: https://habr.com/ru/post/1775269/


All Articles