Xcode 8 更新:
在 Xcode 8 中,需要转到核心数据模型编辑器并显示文件检查器。底部附近是代码生成选项。选择 swift 。
编辑:我找到了从 Core Data 实体生成 Swift 模型的解决方案:
在 Xcode 上:
编辑器 > 创建 NSManagedOjbect > 单击“下一步”按钮 > 单击“下一步”按钮 > 选择“Swift”语言 > 单击“创建”按钮
我通过使用 Core Data 在 Xcode 6 beta 上创建一个新的 Swift 项目来尝试使用 Swift 语言。
当我从 Core Data 的实体生成模型时,Xcode 会创建 Objective-C 模型。
有没有办法用 Core Data 生成 Swift 模型而不是 Obejctive-C 模型?
谢谢!
最佳答案
让我们看看 Objective-C 的方式:
Person.h(头文件)
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Person : NSManagedObject
@property (nonatomic, retain) NSString *name;
@end
Person.m(实现文件)
#import "Person.h"
@implementation Person
@dynamic name;
@end
swift
Xcode6-Beta 中已经包含的文档说:
Core Data provides the underlying storage and implementation of properties in subclasses of the NSManagedObject class. Add the @NSManaged attribute before each property definition in your managed object subclass that corresponds to an attribute or relationship in your Core Data model. Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of a property will be provided at runtime. However, unlike @dynamic, the @NSManaged attribute is available only for Core Data support.
这就是我如何为 Swift 重写上面的示例(未测试):
Person.swift
import CoreData
class Person: NSManagedObject {
@NSManaged var name : NSString
}
根据你的问题,我认为子类生成功能可能还没有包含在 Xcode6 中。当您在 Xcode 中创建 Cocoa 项目时,您确定选择了“Swift”作为编程语言吗?
关于xcode - 从 Core Data 实体生成 Swift 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015185/