Objective-C get a class property from string

18,154

The Key Value Coding mechanism allows you to interact with a class's properties using string representations of the property names. So, for example, if your Record class has a property called column1, you can access that property as follows:

NSString* dataToGet = @"column1";
id value = [myRecord valueForKey:dataToGet];

You can adapt that principle to your specific needs.

Share:
18,154
MechEngineer
Author by

MechEngineer

I'm a mechanical engineer by education, but have stepped out into a more software oriented career path. I use a variety of code platforms like Swift, Objective-C, Java and C++ with web platforms like PHP, jQuery, HTML and CSS. I also work for an engineering company building advanced cooling solutions for computers, so my home system is fully water cooled. Hobbies away from the office include Arduino development, robots using ROS, boating, mountain biking and track driving events.

Updated on June 04, 2022

Comments

  • MechEngineer
    MechEngineer almost 2 years

    I've heard a number of similar questions for other languages, but I'm looking for a specific scenario.

    My app has a Core Data model called "Record", which has a number of columns/properties like "date, column1 and column2". To keep the programming clean so I can adapt my app to multiple scenarios, input fields are mapped to a Core Data property inside a plist (so for example, I have a string variable called "dataToGet" with a value of 'column1'.

    How can I retrieve the property "column1" from the Record class by using the dataToGet variable?