Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
856 views
in Technique[技术] by (71.8m points)

xcode - Generating Swift models from Core Data entities

Update for Xcode 8:

In Xcode 8, one needs to go to the Core Data Model Editor and Show the File Inspector. Near the bottom is an option for code generation. Select Swift.

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

On Xcode:

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


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

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

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

Thanks !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Lets have a look on the Objective-C way:

Person.h (Header-File)

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Person : NSManagedObject
@property (nonatomic, retain) NSString *name;
@end

Person.m (Implementation-File)

#import "Person.h"

@implementation Person
@dynamic name;
@end

Swift

The documentation already included in Xcode6-Beta says:

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.

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

}

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...