Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:33:08+00:00 2026-06-16T15:33:08+00:00

I’m relatively new to Objective-C (mainly using cocoa/apple foundation framework), but a long-time C++

  • 0

I’m relatively new to Objective-C (mainly using cocoa/apple foundation framework), but a long-time C++ programmer, so I’ll start by explaining what I’m trying to accomplish; It’s quite possible my approach isn’t the best one, so I’d love to hear any suggestions for a different approach.

I’ve got a text file in json format, just made this quick example:

"section1" : {
    "director" : "Sample Name 1",
    "writers" : {
        "name" : "Example Name 1",
        "name" : "Example Name 2",
    },
},

And I’ve got a class with properties which I want to match with the data inside this file, since I’m planning to parse this file and store some of the values in an instance of this class. My class header would look roughly like this:

@interface SongData : NSObject
@property (nonatomic, copy) NSString *director;
@property (nonatomic, copy) NSArray *writers;
@end

So I’m trying to find the cleanest, somewhat “data-driven” way to store this json data into the correct properties. Basically I want a way to loop through the json “dictionary” of data, and somehow use the “keys” to match with the correct property in my class and assign the value to that property. My first crack at it, I created a dictionary where the “key” is the actual key to lookup the proper data inside the json data, and the “value” would be a pointer to the correct class property. Sort of like this:

NSDictionary *descriptionDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                                       director, @"name",
                                       writers, @"writers",
                                       nil];

Then the idea was to loop through the json data (which is stored in another NSDictionary), use each key from my “descriptionDictionary” to lookup the appropriate value in the json data, then use the matching “value” of the description dictionary based on the same key (which I was hoping is sort of a pointer to the actual class property?) and set that properties value to the value from the json data. Perhaps an example will make it a bit more clear :-). Lets assume “jsonDictionary” is the result of parsing my json data, and I’m already inside section1, so there should be a 1-1 match between keys that make up the jsonData NSDictionary and the keys that make up my descriptionDictionary NSDictionary:

[descriptionDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
    value = [jsonDictionary objectForKey:propertyName];
}];

Now “value” would technically be pointing to one of my classes properties, and I want to assign relevant json data to it (ie director = @”Sample Name 1″). I know there’s problems with what I’m doing (I realize that I probably can’t just dereference a pointer to a random class property and assign an arbitrary object of unknown type to it, hoping it all matches up 🙂 but hoping my example at least illustrates what I’m trying to do so someone can tell me a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:33:10+00:00Added an answer on June 16, 2026 at 3:33 pm

    Have a look at KVC (source: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueCoding/Articles/BasicPrinciples.html)

    Setting Attribute Values Using Key-Value Coding

    The method setValue:forKey: sets the value of the specified key,
    relative to the receiver, to the provided value. The default
    implementation of setValue:forKey: automatically unwraps NSValue
    objects that represent scalars and structs and assigns them to the
    property. See “Scalar and Structure Support” for details on the
    wrapping and unwrapping semantics.

    If the specified key does not exist, the receiver is sent a
    setValue:forUndefinedKey: message. The default implementation of
    setValue:forUndefinedKey: raises an NSUndefinedKeyException; however,
    subclasses can override this method to handle the request in a custom
    manner.

    The method setValue:forKeyPath: behaves in a similar fashion, but it
    is able to handle a key path as well as a single key.

    Finally, setValuesForKeysWithDictionary: sets the properties of the
    receiver with the values in the specified dictionary, using the
    dictionary keys to identify the properties. The default implementation
    invokes setValue:forKey: for each key-value pair, substituting nil for
    NSNull objects as required.

    One additional issue that you should consider is what happens when an
    attempt is made to set a non-object property to a nil value. In this
    case, the receiver sends itself a setNilValueForKey: message. The
    default implementation of setNilValueForKey: raises an
    NSInvalidArgumentException. Your application can override this method
    to substitute a default value or a marker value, and then invoke
    setValue:forKey: with the new value.

    As long as your class properties have the same names as the JSON fields you can use the setValuesForKeysWithDictionary: and pass in the JSON root dictionary.

    For keys/properties that are named differently you can simply override the setValue:forUndefinedKey: and set the appropriate property yourself.

    Finally there is the case of a value being represented by a different type in the JSON than in the property. For example NSURL would be an NSString instead. Here you can simply check the class of the passed parameter and if it does not match the IVAR, do a conversion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.