Take the 2-minute tour ×

I'm working on a function to fetch JSON data from my web service, let's say, books. Here is an example JSON

"books" : [
        name : "this is book name",
        isbn_number : "this is isbn number of the book"
        author_id : "the author id related to this book"
        name : "this is second book name",
        isbn_number : "this is isbn number of the second book"
        author_id : "the author id related to this second book"

The way I want to save data into sqlite is something like this

NSMutableArray * bookList = [JSONDataFromServer objectForKey:@"books"];

[bookList enumerateObjectsUsingBlock:^(id book, NSUInteger idx, BOOL *stop){

    Book *bookObject = (Book *)[NSEntityDescription insertNewObjectForEntityForname:@"Book"

    for (id key in book) {
        SEL selector = NSSelectorFromString(key);
        if ([bookObject respondToSelector:selector]){
            // do something to set bookObject attribute (which is the name as key)

    if (![managedObjectContext save:&error]){

*** I hope you can see what I'm doing ***

By doing this, I don't have to manually set value of every attribute. I just have to update the app migration to match with web server, make sure that JSON is correct. But the question are.

  1. For web service (Ruby on Rails), we usually name with lowercase, replace space with underscore (author_id). But here, in Objective-C, remove space and replace first character of each word with uppercase (authorId). So, what should the entity property name be, book.author_id or book.authorId? Will there be any problem if I use the same format with web service? (author_id)

    Note : I can convert the name easily with just one line of code on the server side. So, any of those is fine, just want to make it right and avoid future problem

  2. How can I set the value dynamically. I believe there must be a way to do this. I tried the following code but no luck

    [book performSelector:selector] = [book objectForKey:key];

Note : The reason behind this is that the web service is currently under development, its database has been update daily.


Found the solution for question 2

[book performSelector:sel withObject:value];

What I have to do is to convert author_id to setAuthor_id (or authorId to setAuthorId) somehow.

share|improve this question

1 Answer 1

Regarding to your second question, do you use xcode to generate a visual map of your database objects? i.e the book? ( you seem to have done so) and then generate a NSManagedObject for your book entity. i.e. Book.hm

Then, you could access/change the attribute of your bookObject by using dot notation, i.e. bookObject.name = [book ObjectForKey:@"name"]; bookObject.id = [book ObjectForKey:@"id"] etc. as your book attributes will be the properties defined in Book.hm.

For your first question, xCode will automatically do it for you when u generate the NSManagedObject on your book or other objects. It really depends on how you add/set the entity of your book object, if you set it as author_id, then you could access it as book.author_id.

share|improve this answer

Your Answer


