我已经开始使用CoreData编写iPhone应用程序并尝试使我的代码库尽可能可维护.因此,我想避免使用硬编码的字符串,例如:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", aName];
Run Code Online (Sandbox Code Playgroud)
相反,我希望能够写出如下内容:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", Person.name, aName];
Run Code Online (Sandbox Code Playgroud)
这样我就可以让编译器检查是否name是实体的属性Person并避免拼写错误.
使用此代码虽然我在编译时遇到以下错误:name在类型的对象上找不到属性Person
Person属于类型NSManagedObject并由我的Xcode自动生成MyApp.xcdatamodeld.
name 是实体的简单字符串属性 Person
为了解决这个具体问题,我已经开始搜索了很多东西并且已经很多了.到目前为止,我也试过使用property_getName并NSPropertyDescription没有运气.
在此先感谢您的帮助,
乔斯.