Attribute Redeclaration for new NSManagedObject Subclass - ios

This is my first app using Core Data, and I have introduced two new ManagedObjects subclasses. At first I wrote the two classes as two normal Swift classes, then I decided to make them ManagedObject subclasses by creating them as entities in the dataModel.
I then went Editor >> Create NSManagedObject Subclass... and then copied and pasted the code from the first class into the new class, and deleted the old class. This worked fine.
The second time around I tried the same thing, but I am having errors with the attributes this time around.
#objc(TimeValues)
class TimeSlot: NSManagedObject {
var beginDate: NSDate //This line here
var endDate: NSDate //This line here
var currentSeconds: NSNumber //This line here
The attributes all have the error Invalid redeclaration of 'beginDate' and stored property beginDate requires an initial value or should be NSManaged
And in the other TimeSlot extension file I am also getting redeclaration errors:
import Foundation
import CoreData
extension TimeSlot {
#NSManaged var beginDate: NSDate?
#NSManaged var endDate: NSDate?
#NSManaged var currentSeconds: NSNumber?
}
I thought a redeclaration error was because I was declaring it somewhere else, but I have deleted the old folder and the other NSManagedObject subclass works fine. The only difference between the two is that the other one only has static variables.
Any help is appreciated, thanks!

Did you check syntax problems? I had a similar problem caused by an extra empty import statement in files generated by Editor >> Create NSManagedObject Subclass... in Xcode 8.0:
import Foundation
import CoreData
import // <- delete this line

Related

"Cannot find 'RealmProperty' in scope" when trying to use optional double, Swift

I'm using Realm Sync to store data for my iOS app, coded in Swift. I wanted to create an optional double property (budget) for a Realm object (User_budgets). I created the object in the Realm schema and then copied in the Data model SDK that Realm produces which is as below:
import Foundation
import RealmSwift
class User_budgets: EmbeddedObject {
let budget = RealmProperty<Double>()
#objc dynamic var date: Date? = nil
}
I then get the error: "Cannot find 'RealmProperty' in scope". I tried changing the code to the below:
#objc dynamic var budget: Double? = nil
But then I get the error: "Property cannot be marked #objc because its type cannot be represented in Objective-C"
I've had a search but can't seem to find anyone who's had this issue before. There's an easy work around, which is simply to make the budget property required (non-optional), but it would be good to know how to be able to create optional double properties in the future. Can anyone help me out?
I believe you're using the wrong definition for that optional as it's only available in beta 10.8.0-beta.0:
What you have is
let budget = RealmProperty<Double>()
and for all other releases it should be
let budget = RealmOptional<Double>()
See RealmProperty and RealmOptional
oh and here's a link to all of the Support Property Types

Custom-typed Core Data attributes problem

I want in an entity called Room, to have an array of the entity Person (basically I want an attribute in Room with a custom type Person), but when doing room.persons = currentRoomPersons the application is blocked and a SIGARBT error occurs, the current code it's something like this (Github code):
Room+CoreDataClass.swift:
import Foundation
import CoreData
#objc(Room)
public class Room: NSManagedObject {}
Room+CoreDataProperties.swift:
import Foundation
import CoreData
extension Room {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Room> {
return NSFetchRequest<Room>(entityName: "Room")
}
#NSManaged public var persons: [Person]? // Array of entity "Person" in the room.
}
SIGARBT this is what it says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Room setPersons:]: unrecognized selector sent to instance 0x600001d1eee0'
The project you can use to run it on your own, or check all the code is uploaded to Github.
What am I doing wrong?
Thanks.
Entities must be connected to each other with Relationships rather than being declared as Attribute.
Please read the section Defining Attributes and Relationships in Core Data Programming Guide
The type of a To-Many relationship is NSSet? by default. It's highly recommended yo change the type to non-optional native Set<Person>

iOS 10 Xcode 8 - migration to Swift 3 and Date

I'm in the process of migration an iOS app from swift 2 to swift 3 and I encounter this error that I don't understand and I'm not sure what to do.
The issue occurs when I try to read the property called dateApproved but not for dateCreated.
print("dateApproved: \(cEntity.dateApproved)")
print("dateCreated: \(cEntity.dateCreated)")
Entity class
#objc(entity)
open class Entity: NSManagedObject {
...
#NSManaged var dateApproved: Date
#NSManaged var dateCreated: Date
...
Upon inspecting the property cEntity I can see that
dateApproved = nil;
dateCreated = "2016-08-24 22:20:38 +0000";
This is a screenshot of the error
Note: it worked before, it just doesn't work anymore since I'm migrated all the code to make it compliant with Swift 3.
Could you please give me some pointers about how to solve/track this issue. Very much appreciated.
The instance variable dateApproved is declared as a non-optional variable, i.e. Swift will assume it's never nil. However, as the debugger shows, it is nil.
Therfore, change the declaration to match reality and make it optional.
#NSManaged var dateApproved: Date?

Opening Realm hangs after implementing new object class

I wish to support rearranging a UITableView. I have seen other answers here, here, and here recommend using a another class to manage the realm objects. The only problem is as soon as I add the class I cannot successfully open a Realm.
import RealmSwift
class Data: Object {
dynamic var id = ""
}
// Adding this class causes issues
class DataList: Object {
let list dataList = List<Data>()
}
Any ideas on what is going wrong here? When I attempt to open the Realm it just hangs: no error thrown.
Edit:
From the realm doc it says they should be declared as let.
Thanks to Tj3n for the solution. Migrate your schema or reinstall the app fixes the issue. Here is some doc on that.

App is crashing if using Core Data

I addded a new attribute (of type boolean) into my core data model and my data.swift class. I try to run my app after I added the attribute. All works fine. Now I try to work with the new attribute. I wrote some code, which work is working with the attribute. Now I tried a 2nd time to run the app. It's crashing. I get the line of code, where the error occurs. But the line of code which I get has nothing to do with the attribute, which I've added new. The line, where the error occurs is working with another attribute in the core data model but not with the attribute which I've added new. Does someone knows a solution for this?
My data.swift class:
import Foundation
import CoreData
#objc(data)
class data: NSManagedObject {
#NSManaged var aufgabe: String
#NSManaged var datum: NSDate
#NSManaged var hatDatum: Bool //new attribute
}
The crashing line:
cell.textLabel!.text = "\(daten[indexPath.row].aufgabe)"
The error message: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) There's nothing written in the console.
My data model: https://www.dropbox.com/s/rzlxi30a5mrez52/Bildschirmfoto%202015-02-01%20um%2014.39.42.png?dl=0
Reset the simulator or delete the app. It's a conflict in you data model files.

Resources