2

I have NSManagedObject with properties. Suppose that one property it is:

@property (retain, nonatomic) NSString *city;

For fetching data form core data I always use NSPredicate. The predicate looks like below:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"city", @"San Francisco"];

But the problem here that the project use key "city" as undefined key in meaning like a constant. So lets suppose that I use key "city" in my app 1000 times, so then I need to replace all this keys with new one if property of NSManagedObject was changed, e.g. to cityName. In this case I need to use predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"cityName", @"San Francisco"];

It is not comfortable. So right now I am using global extern NSString key kCity = @"city"; but in case if the data model changed as well there is a problem, because if property will be changed I will have crash only when run app and console will say me:

'key path city not found in entity <NSSQLEntity...

Which workaround can be here? I thought using something like

NSString * const kCity = myEntity.city.propertyString;

So if property was changed then parser will tell me that project has an error and I can't run app. So after I change property for constant only then I will be able to run app

How can get name of property in NSString. Of course it is just suggestion and maybe someone have another variant.

Thanks

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

2 Answers2

2

You could use mogenerator, one thing it does is to create a struct for attribute and relationship keys that you can use.

// .h
extern const struct MBListAttributes {
    __unsafe_unretained NSString *title;
} MBListAttributes;

// .m
const struct MBListAttributes MBListAttributes = {
    .title = @"title",
};

You can then use MBListAttributes.title to get the correct key.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • 1
    For this and some other (perhaps less known) features of *mogenerator*, see [What features does mogenerator provide?](http://stackoverflow.com/questions/22566121/what-features-does-mogenerator-provide). – Martin R May 05 '14 at 14:01
  • How can I use it. Can some one share link with installation guide? I ve got main idea, but there is no steps in the repo how to start to use it. Maybe I miss something. Also I am using MagicalRecord, will it affect on some parts if the mogenerator because of fetch or something else? – Matrosov Oleksandr May 17 '14 at 08:41
0

I use this macro-based solution to avoid string literals when I want the name of a key.

It allows you to write key names in this fashion:

[NSPredicate predicateWithFormat:@"%K = %@", Key(YourManagedObjectSubclass, propertyName), @"aValue"];

The compiler will check if YourManagedObjectSubclass has a property of the given name.

But of course, keep in mind that if you change an attribute name in your data model you still need to change it in your NSManagedObject subclass.

e1985
  • 6,239
  • 1
  • 24
  • 39