从核心数据实体生成Swift模型

从核心数据实体生成Swift模型

本文介绍了从核心数据实体生成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:

这就是我将重写上述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模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:50