Slee
Slee

Reputation: 28248

CoreData 'Name must begin with lower case letter' - can I get around this?

When I first started my iOS project I could use capital letters in core data to start my property names (FirstName, LastName, ect...). This is extremely beneficial to me since I feed data into CoreData using a web service and plists and the creation of objects inserted into my local CoraData DB is greatly simplified and super fast if all property names match exactly. Which up until now has not been a problem. Currently in xCode 4.3.1 I cannot get around the nameing convention restrictions and it is causing me a huge headache since my remote DB uses capital letters for it's column names which in turn then do not match my local CoreData properties.

Is there anyway to get around this in xCode?

Upvotes: 1

Views: 3490

Answers (6)

fefferoni
fefferoni

Reputation: 221

I encountered the same problem when mapping my web service JSON response to my Core Data models. Instead of changing the Core Data models I solved it by changing my mapping code to the following:

for (NSString *key in json) {
     NSString *keyWithFirstCharLowered = [key stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[key substringToIndex:1] lowercaseString]];

    if ([modelClass respondsToSelector:NSSelectorFromString(keyWithFirstCharLowered)]) {
        [modelClass setValue:[json valueForKey:key] forKey:keyWithFirstCharLowered];
    }
 }

Upvotes: 0

azgard
azgard

Reputation: 51

I had similar problem - I wanted to use entity names starting with lower case letter and xcode demanded to use names starting with upper case letter. I resolved this issue in the following way:

  1. Created NSManagedObject subclass for an entity - option available in Editor menu
  2. Used xcode refactoring to change the name of the created subclass

Refactoring changed also the name of the entity in the Data Model. I believe same method can be used for changning the name of attributes. I used xcode 4.6.1.

Upvotes: 0

Mundi
Mundi

Reputation: 80265

Function to convert the first character to lowercase:

-(NSString *)refactorAttributeName:(NSString*)name {
   NSString *first = [[name substringToIndex:1] lowercaseString];
   return [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) 
                                        withString:first];            
}

Upvotes: 3

dirk
dirk

Reputation: 11

you can always go into the model package and edit the contents file manually. does not mean that this new standard won't bite you later.

Upvotes: 1

user4951
user4951

Reputation: 33090

I suppose you can try opening the same project in 4.2 use big letter, and then reopening in 4.3, which will honor the old convention

Upvotes: 0

LJ Wilson
LJ Wilson

Reputation: 14427

Simple answer is No. If you have any control over the way the data is coming out of your remote DB, you could just do something like:

Select FirstName as firstName, LastName as lastName ...

If that is too much trouble, then you will need to either need to map these in your project or not use Xcode 4.3 and up.

Upvotes: 1

Related Questions