Questions tagged [nsmanagedobject]

NSManagedObject is a generic class that implements all the basic behavior required of a Core Data model object. NSManagedObject is available in OS X v10.4 and later and available in iOS 3.0 and later.

From the documentation:

Overview

A managed object is associated with an entity description (NSEntityDescription) that provides metadata about the object, including the name of the entity that the object represents and the names of its attributes and relationships. A managed object is also associated with a managed object context that tracks changes to the object graph.

[...]

Data Storage

In some respects, an NSManagedObject acts like a dictionary—it’s a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance. NSManagedObject supports a range of common types for attribute values, including string, date, and number (see NSAttributeDescription for full details).

Resource

Sample NSManagedObject declaration

In Objective-C:

NSManagedObjectContext *context = [self managedObjectContext];

//create a new managed object
NSManagedObject *newFlight = [NSEntityDescription insertNewObjectForEntityForName:@"FlightEvent" 
        inManagedObjectContext:context];

In Swift:

let newFlight = NSEntityDescription.insertNewObjectForEntityForName("FlightEvent", inManagedObjectContext: managedObjectContext)
1597 questions
52
votes
8 answers

'filenames are used to distinguish private declarations of the same name' error

I am getting this error on generating an NSManagedObject in Xcode 8.1 in Swift. :0: error: filename "DemoOne+CoreDataClass.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataClass.swift' and…
Swasidhant
  • 1,231
  • 1
  • 12
  • 13
52
votes
7 answers

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…
Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67
39
votes
1 answer

Is it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?

So, my scenario is this: I have an NSManagedObject subclass in my iOS application, and as a property I want to store the contents of an MKPolygon object. The way I've decided to go about this (and whether it's valid is perhaps a different question)…
HowlingEverett
  • 820
  • 1
  • 9
  • 10
36
votes
6 answers

Duplicate symbol error when adding NSManagedObject subclass, duplicate link

I was trying to create NSManagedObject subclasses (2 related entities) automatically in Xcode. They are generated like this: However, before I do anything further, when I tried to build and run it, a link error occur, as shown: duplicate symbol…
KFZ
  • 369
  • 1
  • 3
  • 10
35
votes
5 answers

NSNull handling for NSManagedObject properties values

I'm setting values for properties of my NSManagedObject, these values are coming from a NSDictionary properly serialized from a JSON file. My problem is, that, when some value is [NSNull null], I can't assign directly to the property: …
Lucien
  • 8,263
  • 4
  • 30
  • 30
35
votes
4 answers

How to make a designated initializer for NSManagedObject subclass in Swift?

class Alternative: NSManagedObject { @NSManaged var text: String @NSManaged var isCorrect: Bool @NSManaged var image: NSData } convenience init(text: String, isCorrect: Bool, entity: NSEntityDescription, insertIntoManagedObjectContext…
bogen
  • 9,954
  • 9
  • 50
  • 89
35
votes
8 answers

Setting an NSManagedObject relationship in Swift

How does one add an object to a relationship property in an NSManagedObject subclass in Swift? In Objective-C, when you generate an NSManagedObject subclass in Xcode from the data model, there's an automatically generated class extension which…
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
35
votes
5 answers

CoreData could not fulfill a fault for

I have a really annoying problem, which I just can't seem to get fixed. I have a view when I send a message that gets saved to the Core Data, when thats done it asked the database for a random message (sentence) and saved that as well to an other…
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
31
votes
3 answers

Using property observers on NSManaged vars

I have a var declared in a class like so: @NSManaged var isFavorite: Bool I would like to declare a property observer, very similar to the one below. var organization: String { didSet { postNotificationWithName( "newData" ) } …
Satre
  • 1,724
  • 2
  • 18
  • 22
30
votes
9 answers

"[something copyWithZone:]: unrecognized selector sent to instance" when using Bindings / Core Data

(self asking and self-answering because I spent hours on the web looking for this, and most of the resources all say "I solved it in the end" without giving an explanation) I had a very simple Core Data + Bindings application: An NSArrayController…
Adam
  • 32,900
  • 16
  • 126
  • 153
30
votes
4 answers

Core Data: Could not cast value of type 'MyType_MyType_2' to MyType

I have an Objective-C model class MyType. This class is used in Swift code: NSEntityDescription.insertNewObjectForEntityForName("MyType", inManagedObjectContext: context) as! MyType The as! cast results in the error message Core Data: Could not…
Bastian
  • 4,638
  • 6
  • 36
  • 55
30
votes
1 answer

Which Swift types can be represented in Objective-C?

I'm creating an NSManagedObject subclass in Swift and I get an error when I make an Optional property that's of type Int, Float or Double (and maybe others that I didn't try out). @NSManaged var number: Float? //error:Property cannot be marked…
Moon Cat
  • 2,029
  • 3
  • 20
  • 25
28
votes
3 answers

Changing a managed object property doesn't trigger NSFetchedResultsController to update the table view

I have a fetchedResultsController with a predicate, where "isOpen == YES" When calling for closeCurrentClockSet, I set that property to NO. Therefore, it should no longer appear on my tableView. For Some Reason, this is not happening. Can someone…
26
votes
2 answers

Why is the extension of my Swift class not visible outside the defining file?

I have an Xcode-generated NSManagedObject class for my CoreData model. @objc(SomeClass) class SomeClass : NSManagedObject { /* ... */ } It is defined in a file named 'SomeClass.swift'. I would like to extend this class, so I created…
Richard Dong
  • 704
  • 1
  • 7
  • 13
25
votes
2 answers

Set auto increment in Core data iOS

I am using Core Data, and want to set an auto_increment ID as one of the fields which will be unique. Is it possible to set auto_increment in iOS using core data? Can anyone help me with a small example of how to implement this? Below is the code…
Anu Padhye
  • 615
  • 1
  • 6
  • 16