2

Possible duplicate: Looping through properties in a class

The problem is I have number of UILabels on each row upon a UITableView and I need to map the properties of my model class (which are of course NSStrings) to the label titles.

As there are multiple labels(More than 12, and it might increase later in my project), hence I was using a for loop to allocate all the labels. But for mapping the variables to the labels I have to hard code each time for the corresponding label.

Is there any way loop through all the properties of an class, so that I can declare them in an order and fetch them as an array and map it properly.

I have seen and understood the above possible duplicate link but it populates the code with as many switch cases as many times it is required to set. Instead of that is there any other alternative for concise and more clear coding using a loop.

Any help would be appreciated. Happy coding.

1 Answer 1

3

If you're talking about UILabels, I will assume you want to use them via IB in Xcode, however you're creating them in code just because you can loop in code.

If so, you can think about using IBOutletCollection instead of IBOutlet like below:

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *titleLabels;

the order in this array will be the same, as you add them in the IB ui.

Hope, this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I think both are similar and doesn't differ much. Yes I can use the IBOutletCollection but however that is not my primary requirement. It's about how to fetch a list of properties from a class object in a particular order and directly mapping them to the labels without using the switch case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.