Generating Swift models from Core Data entities - xcode

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 !

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?

You can get Swift model back using NSEntityDescription.insertNewObjectForEntityForName but you must edit your core data model file and not use Person as a Class Entity but <ProjectName>.Person else it returns NSManagedObject...
Using println() you won't see Person instance but something like <_TtC5ProjectName4Person: 0xc9ad5f0> but calling methods on this will prove it's a Person instance for real. I guess it's just the way for Swift to generate unique class names, not conflict and CoreData methods show this internal mechanism.
The Apple documentation says:
Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.

According to Apple's video regarding What's new In CoreData frame: 38mins (WWDC2014 Session 225), in inspector's Data Model, prefix the class name with the project name. Like projectName.Doctor
I've tried this but what will happen is that the generated managed object class becomes: projectName.swift instead of Doctor.swift. Even the class declaration becomes class projectName: ManagedObject
Solution:
In Data model inspector, just specify the Name & Class of your object to what name you want, example: Doctor
After you generated an object model and selecting Swift, this will create a file (Doctor.swift).
Now, when inserting new records in Core Data, you might an experience error "Class not found, using default NSManagedObject instead" even if you cast the newly inserted object to a correct object name.
To solve this, you just need to add #objc(class name) above the class declaration. See sample below.
import Foundation
import CoreData
#objc(Doctor)
class Doctor: NSManagedObject {
#NSManaged var name: String
}
Then:
let doctorManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Doctor", inManagedObjectContext: context) as Doctor
doctorManagedObject.name = "John" // you can now use dot syntax instead of setValue
Save context to commit insert.

I tested #NSManaged, it didn't work. :( .
But mixed models files(.h) generated by xcdatamodel, it succeed.
please read the doc and code in https://github.com/iascchen/SwiftCoreDataSimpleDemo

Alternatively, you can just add #import "Person.h" to the bridging header, Project-Bridging-Header.h that Xcode generates for you (if you accepted it's offer to generate). Then you can use all the auto-generated Obj-C as if it were native Swift.

Editor -> Create NSManagedObject Subclass works fine for Swift
Simply go through all the usual steps, but when you get to creating the files, choose Language 'Swift' if it's the first time you've used Create NSManagedObject Subclass with a Swift project it'll be defaulting to Objective C

For XCode 12.
You can change generating model language.

Related

How to use NSObjectController and Managed Object Context using Cocoa Bindings

Searched entire Internet but couldn’t find the modern solution for my problem.
I want to use NSObjectController in pair with Core Data through Cocoa Bindings and struggle to set it up properly. Worth noting that I’m using latest version of Xcode and Swift.
What I’ve done:
For testing purposes I’ve done the following:
Created an macOS app with “Use Core Data” option selected (the app is not document based);
Dragged 2 NSTextFields into the Storyboard Dragged NSObjectController to the view controller scene;
Added Employee Entity to Core Data model with 2 attributes “name” and “surname”;
Done everything from the answer in How do I bind my Array Controller to my core data model?
Set NSObjectController to entity mode and typed in “Employee”,
Prepares Content selected, Use Lazy Fetching selected so all three options checked;
Binded the NSObjectController’s Managed Object Context in bindings inspector to the View Controller’s managedObjectContext;
Binded NSTextFields as follows: Value - Object Controller, Controller key - selection, Model Key Path - name (for 1st text field) and surname (for 2nd).
That’s it.
First set of questions: What I did wrong and how to fix it if it’s not completely wrong approach?
I’ve read in some post on stackoverflow that doing it that way allows automatic saving and fetching from Core Data model. That’s why I assumed it should work.
So here is a Second set of questions:
Is it true?
If it is then why text fields are not filled when view is displayed?
If it is not then how to achieve it if possible (trying to write as less code as possible)?
Third question: If I used approach that is completely wrong would someone help me to connect Core Data and NSObjectController using Cocoa bindings and show me the way of doing so with as less code written as possible using the right approach?
Taking into account that there no fresh posts about this topic in the wilds I think the right answer could help a lot of people that are developing a macOS app.
Thanks in advance!
I think your basic approach is correct, although it is important to understand that you need a real object, an instance, in order for it to work.
Creating a NSManagedObject subclass is generally desirable, and is almost always done in a real project, so you can define and use properties. You can do it easily nowadays by selecting the data model in Xcode's Project Navigator and clicking in the menu: Editor > Create NSManagedObject Subclass…. Technically it is not necessary, and in a demo or proof-of-concept, you often muddle through with NSManagedObject.
Assuming you are using the Xcode project template as you described, wherein AppDelegate has a property managedObjectContext, the following function in your AppDelegate class will maintain, creating when necessary, and return, what I call a singular object – an object of a particular entity, in this case Employee, which your app requires there to be one and only one of in the store.
#discardableResult func singularEmployee() -> NSManagedObject? {
var singularEmployee: NSManagedObject? = nil
let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: "Employee")
let objects = try? self.managedObjectContext.fetch(fetchRequest)
singularEmployee = objects?.first
if singularEmployee == nil {
singularEmployee = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: self.managedObjectContext)
}
return singularEmployee
}
Then, add this line of code to applicationDidFinishLaunching
singularEmployee()

CustomClass Reference in Swift

I am new to Objective C and Swift. I have created a SampleTest project in Swift.. While creating a ViewController.xib we need to give the reference class in identity inspector which is contain in right side pane in xcode.
ViewController denotes the ViewController.swift module and Here what it denotes _TtC10SampleTest14ViewController in class field.
_TtC10SampleTest14ViewController is the mangled name of your ViewController, swift using Name Mangling .
1. Read more about name mangling in this article
2.
Check the related answer also

Change UILabel from appDelegate

I want to do some stuff from the appDelegate in Xcode. One of these things are to change a UILabel.
ViewController *viewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"id"];
viewController.label.text = #"heeej";
I was told this would do the trick. Doesn't work. Label doesn't change. Does anyone know what the problem is?
There are several problems:
Don't do anything in the AppDelegate except for loading the window an the initial view controllers (if needed).
You are instantiating a new view controller in the first line. I assume you already have a view controller and you want to change the label in that view controller. Than you need a reference to that view controller and use this reference to send messages to it.
The label should not be a property of the view controller. You should try to follow the design pattern Model-View-Controller or the pattern Model-View-ViewModel. Ask you preferred search engine what those are if you don't know.
id as an identifier for anything in Objective-C is a bad idea because this is used as an object type.
Edit: You don't need a reference to change a property in the view controller. Use notifications to update the label. The system sends a notification with the name UIApplicationWillEnterForgroundNotification' (see [here][1]). Add the view controller as an observer to the[NSNotificationCenter defaultCenter]` for this name and react on the notification. Read the Apple documentation if you don't know what I am talking about.

Cocoa drag & drop information

I have a very simple application test in which I want to drag and drop files in a NSImageView object.
I can already get the list of files from this action, but now I want to store this data in an array to be accessed in the View using data bindings. The only code I have that works fine with data bindings, tough, has an array in AppDelegate that I access using an Array Controller. But then, my data is inside a class I created to the NSImageView called "DropView", which extends "NSImageView" class and handles the drop action.
How can I pass the array information to make the bindings possible?
Any suggestion is welcome. Thanks!
I decided to use a singleton in a bigger project. In this smaller one I did like this:
1 - Add AppDelegate reference to the subclass (m file):
#import "AppDelegate.h"
2 - Call a method declared in the AppDelegate passing all the information needed like this:
[[NSApp delegate] doSomething:someInformation];
3 - Set all the information in the method used.
4 - Make the bindings in the interface.
The simple answer is that you should not be storing model data (the array of file URLs) in a view.
Your view should pass the list of files to some other controller object, which then stores the list of files in some sort of model object. This is what MVC is about.
One way to do this is to use the delegate pattern, where your view would declare a protocol method something like this:
#protocol YourViewProtocol
- (void)imageView:(YourImageViewClass*)aView receivedDroppedURLs:(NSArray*)arrayOfURLs;
#end
Your view would also have a weak datasource property:
#interface YourImageViewClass : NSImageView
#property (weak) id <YourViewProtocol> datasource;
#end
You would then set your controller object as the datasource of the view. The controller must conform to the protocol and implement its method:
#interface YourController <YourViewProtocol>
- (void)imageView:(YourImageViewClass*)aView receivedDroppedURLs:(NSArray*)arrayOfURLs;
#end
Then, in the view method that receives the drop, you'd do something like:
- (void)receivedDroppedURLs:(NSArray*)urls
{
if([datasource conformsToProtocol:#protocol(YourViewProtocol)])
{
[datasource imageView:self receivedDroppedURLs:urls];
}
}
The other way to do this is via custom bindings. Implementing bindings in a custom view is a bit more complicated and I'd recommend that you use the delegate/datasource pattern initially, if you're not an experienced Cocoa developer.

iOS protocols and delegates. Basic questions

I'm creating an app that has a UITableView.
The data will be comming from an XML fetched over the net. I'm using NSXMLParser for this and it works. I used my tableView controller as the delegate for this so it implements the protocol for it:
#protocol NSXMLParserDelegate;
#interface MainView : UITableViewController <NSXMLParserDelegate>
Now this works perfectly, as I've nslogged the resulting parse.
Now, I want to populate the NStableView, so Reading I find that I need to add the datasource and delegate.
UITableViewDataSource
and
UITableViewDelegate
both of which are protocols.
How would I go about doing this on the same class? can I implement more than one protocol with the same class? should I move the delegation of the parser to another object and use this controller for this purpose?
Basically the question is what is the best way to do this?
thank you
Sure, you can implement as many protocols in a class as you want:
#interface MainView : UITableViewController <NSXMLParserDelegate, UITableViewDataSource, UITableViewDelegate>
Is that the "proper" way of doing that? I don't think there's a "right" answer to that. A purist might say no. I'd say do it where is makes sense, but err on the side of breaking it out into separate classes. For example, if your view controller is a simple menu then it would make sense for your view controller to also be your table view delegate and data source; there's no advantage in breaking it out into multiple classes.
If you have to parse XML my intuition suggests that it's starting to get a bit more complex. Maybe have a data class that implements the data source and XML parser and a controller class?

Resources