开发者

Objective-C get a class property from string

开发者 https://www.devze.com 2023-03-09 19:45 出处:网络
I\'ve heard a number of similar questions for other languages, but I\'m looking for a specific scenario.

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 mapp开发者_开发技巧ed 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消