Xcode 8更新:
在Xcode 8中,需要转到Core Data Model Editor和Show the File Inspector.靠近底部是代码生成的选项.选择Swift.
编辑:我找到了从Core Data实体生成Swift模型的解决方案:
在Xcode上:
编辑>创建NSManagedOjbect>单击按钮"下一步">单击"下一步"按钮>选择"Swift"Langage>单击"创建"按钮
我使用Core Data在Xcode 6 beta上创建了一个新的Swift项目,尝试了Swift语言.
当我从Core Data的实体生成模型时,Xcode创建了Objective-C模型.
有没有办法用Core Data生成Swift模型而不是Obejctive-C模型?
谢谢 !
我在Swift中的Xcode 8.1中生成NSManagedObject时遇到此错误.
:0:错误:文件名"DemoOne + CoreDataClass.swift"使用两次:'/ Users/Swasidhant/Desktop/demo/DemoOne + CoreDataClass.swift'和'/ Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again- hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataClass.swift':0:注意:文件名用于区分具有相同名称的私有声明:0 :错误:文件名"DemoOne + CoreDataProperties.swift"使用了两次:'/ Users/Swasidhant/Desktop/demo/DemoOne + CoreDataProperties.swift'和'/ Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/build /intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataProperties.swift':0:注意:文件名用于区分具有相同名称的私有声明Command/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefaul t.xctoolchain/usr/bin/swiftc失败,退出代码为1
我搜索了这个并得到了答案,说该文件可能在项目中出现两次,这不是我的情况.我遇到的另一个解决方案是从构建阶段删除文件并再次添加.那也行不通.是一些构建设置配置错误?或者是其他东西?
以下是使用Xcode 8.1创建的演示项目的链接,并显示此问题: - https://www.dropbox.com/s/xwyzhshktb2hqe7/demo2.zip?dl=0
我有一个名为Record的coredata实体,并且有一个属性dateUpdated.我注意到生成的NSManagedObject子类没有可选标记(?)
CoreData编辑器:

生成的子类:

预期:

更新: 这是乏味的在我的部分,因为我想再生子类每一次,这意味着我还需要手动更新所有可选值.在子类中使用非可选(不带'?')会导致我在分配之前检查evalue,如下例所示:
// sample value:
// serverDateFormatter = "yyyy/MM/dd"
// dateString = ""
// Branch is a subclass of Record (see above images)
var date = self.coreData.serverDateFormatter.dateFromString(dateString)
if date != nil
{
branch.dateUpdated = date
}
Run Code Online (Sandbox Code Playgroud)
但是如果xcode可以在子类中使用(?)自动设置可选值,我只需要这样做:
branch.dateUpdated = self.coreData.serverUTCDateFormatter.dateFromString(dateString)
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我有一堆属性需要手动标记为可选.
假设我有一个存储在Core Data中的书籍列表.我想通过它的主键ID搜索一本书.我知道Core Data创建的sqlite文件在每个表中都有一个ID列,但无论如何这似乎都没有暴露给我.
有没有人有任何建议?
谢谢!
所以,我的情况是这样的:
我的iOS应用程序中有一个NSManagedObject子类,作为一个属性,我想存储MKPolygon对象的内容.我决定解决这个问题的方式(以及它是否有效可能是一个不同的问题)是将polygon属性声明为可转换对象,然后存储包含多边形点的NSArray(作为NSValue对象).
为此,我在模型对象上编写了几个便捷类方法:
+ (NSArray *)coordsArrayFromMKPolygon:(MKPolygon *)polygon pointCount:(int)count
{
CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
[polygon getCoordinates:coords range:NSMakeRange(0, count)];
NSMutableArray *coordsArray = [NSMutableArray array];
for (int i = 0; i < count; i++) {
NSValue *coordVal = [NSValue valueWithBytes:&coords[i] objCType:@encode(CLLocationCoordinate2D)];
[coordsArray addObject:coordVal];
}
free(coords);
return [NSArray arrayWithArray:coordsArray];
}
+ (MKPolygon *)polygonFromCoordsArray:(NSArray *)coordsArray pointCount:(int)count
{
CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
for (int i = 0; i < count; i++) {
CLLocationCoordinate2D coord;
[[coordsArray objectAtIndex:i] getValue:&coord];
coords[i] = coord; …Run Code Online (Sandbox Code Playgroud) 我正在使用CoreData,在一个名为"RoleName"的实体上.
问题是:我在我的模型中单击"Create NSManagedObject subclass",因此它会自动为我的实体创建类.
但是,在类的声明中,我收到此错误:
无效的"RoleName"重新声明
即使我没有任何其他同名的课程.
我正在为我的属性设置值NSManagedObject,这些值NSDictionary来自JSON文件中的正确序列化.我的问题是,当某个值是[NSNull null],我不能直接分配给属性:
fight.winnerID = [dict objectForKey:@"winner"];
Run Code Online (Sandbox Code Playgroud)
这引发了一个 NSInvalidArgumentException
"winnerID"; desired type = NSString; given type = NSNull; value = <null>;
Run Code Online (Sandbox Code Playgroud)
我可以轻松检查值[NSNull null]并指定nil:
fight.winnerID = [dict objectForKey:@"winner"] == [NSNull null] ? nil : [dict objectForKey:@"winner"];
Run Code Online (Sandbox Code Playgroud)
但我认为这不是很优雅,而且要设置很多属性会变得混乱.
此外,在处理NSNumber属性时,这会变得更难:
fight.round = [NSNumber numberWithUnsignedInteger:[[dict valueForKey:@"round"] unsignedIntegerValue]]
Run Code Online (Sandbox Code Playgroud)
现在NSInvalidArgumentException是:
[NSNull unsignedIntegerValue]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我必须[dict valueForKey:@"round"]在制作NSUInteger它的价值之前进行治疗.一线解决方案消失了.
我尝试创建一个@try @catch块,但只要捕获到第一个值,它就会跳转整个@try块,并忽略下一个属性.
是否有更好的方法来处理[NSNull null]或者使这个完全不同但更容易?
如何NSManagedObject在Swift 的子类中将对象添加到关系属性?
在Objective-C中,当您NSManagedObject从数据模型在Xcode中生成子类时,会有一个自动生成的类扩展,其中包含如下声明:
@interface MyManagedObject (CoreDataGeneratedAccessors)
- (void)addMySubObject: (MyRelationshipObject *)value;
- (void)addMySubObjects: (NSSet *)values;
@end
Run Code Online (Sandbox Code Playgroud)
然而,Xcode目前缺乏Swift类的这一类生成功能.
如果我尝试直接在Swift对象上调用等效方法:
myObject.addSubObject(subObject)
Run Code Online (Sandbox Code Playgroud)
...我在方法调用上遇到编译器错误,因为这些生成的访问器不可见.
我已经将关系属性声明为@NSManaged,如文档中所述.
或者我是否必须为具有关系的数据模型恢复Objective-C对象?
我试图在Xcode中自动创建NSManagedObject子类(2个相关实体).它们是这样生成的:

但是,在我做任何进一步操作之前,当我尝试构建并运行它时,会发生链接错误,如下所示:
duplicate symbol _OBJC_CLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_CLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我曾多次尝试创建新项目并做同样的事情.我的初衷是将一些自定义方法添加到子类中.但是当我添加任何东西时,例如Photo+CoreData.h/m,出现与上面相同的错误.
我找到了一些关于"双重包含"或"将文件保存到错误的目录"的答案,但我没有这样做.有人对此有任何想法吗?