Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • The declaration in the header file (NSObject * myObject;)
  • The @property() line
  • The @synthesize() line in the implementation
  • Releasing it under dealloc: (only for objects of course)

I do this because it works, not because I completely understand what's going on. I do 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 constructed. And the synthesize line actually builds those getter/setter methods. I also know that primitive types should use (nonatomic,assign) instead of (nonatomic,retain), but I have no clue when I should omit the nonatomic.

What can I do to avoid redundancy in my code. If I change or add a variable in my class I have to check 4 different places, and it gets old really fast. Are there any key strokes to make this process faster? Are there lines of code I can simplify or combine to obtain the same result?

like image 886
Tozar Avatar asked Jan 25 '26 05:01

Tozar


1 Answers

Accessorizer will automate a lot of this for you.

like image 151
nevan king Avatar answered Jan 27 '26 18:01

nevan king