从核心数据实体生成Swift模型 [英] Generating Swift models from Core Data entities

查看:126
本文介绍了从核心数据实体生成Swift模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我找到了从Core Data实体生成Swift模型的解决方案:

Edit: I found the solution to generate a Swift model from Core Data entity:

在Xcode上:

编辑器>创建NSManagedOjbect>点击按钮下一步>点击按钮下一步>选择SwiftLangage>点击按钮创建

Editor > Create NSManagedOjbect > Click button "Next" > Click button "Next" > Select "Swift" Langage > Click button "Create"

我通过使用Core Data在Xcode 6 beta上创建一个新的Swift项目来尝试Swift语言。

I tried Swift langage by creating a new Swift project on Xcode 6 beta using Core Data.

当我从Core Data的实体生成模型时,Xcode创建了Objective-C模型。

When I generate my models from my Core Data's entities, Xcode creates Objective-C models.

有没有办法生成Swift模型而不是Obejctive-C模型与核心数据?

Is there a way to generate Swift model rather than Obejctive-C model with Core Data ?

谢谢!

推荐答案

对Objective-C方式:

Lets have a look on the Objective-C way:

Person.h (Header-File)

Person.h (Header-File)

#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中已经包含的文档说:

Swift

The documentation already included in Xcode6-Beta says:


Core Data提供了NSManagedObject类的子类中的属性的底层存储和实现。在受管对象子类中的每个属性定义之前添加@NSManaged属性,该属性对应于Core Data模型中的属性或关系。与Objective-C中的@dynamic属性一样,@NSManaged属性通知Swift编译器将在运行时提供属性的存储和实现。但是,与@dynamic不同,@NSManaged属性仅用于Core Data支持。

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(未测试)示例:

So that is how I would rewrite the above example for Swift (not tested):

Person.swift

import CoreData

class Person: NSManagedObject {

    @NSManaged var name : NSString

}

根据你的问题,我认为子类生成功能可能不包括在Xcode6中。当你在Xcode中创建Cocoa-Project时,你确定你选择了Swift作为编程语言吗?

And according to your question I think the subclass-generation-feature might be not included in Xcode6 yet. Did you made sure that you have chosen "Swift" as programming language when you were creating the Cocoa-Project in Xcode?

这篇关于从核心数据实体生成Swift模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆