I find myself declaring bunch of class variables and it is really tiring to write simple get/set methods for each variable. So, the question is how to synthesize setters/getter for class variables in objective-c?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Usually when you are grouping a set of related variables that are meant to be accessed globally, you create what is called a Singleton:
What should my Objective-C singleton look like?
This means you have one class level method that gives you back a shared instance – so you’d have a call like:
Since the values are you are storing are true class instance variables, you can use normal properties, but all classes will be working with the same shared data.
Note that some people dislike the use of singletons, you may want to read some caveats about the practice:
Singleton: How should it be used
But since you already start with one inherent singleton in iPhone development (the application delegate, which anyone can access at any time) making light use of the technique does not hurt if you are careful. Note that instead of creating a Singleton class, one alternative is to have the application delegate create a single instance of a variable storage class, and have everyone access that through the delegate…