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?
Related
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
I have recently released a new version of our app and during beta testing, it's crashing on all iOS 10 devices but not other versions. Since we have Crashlytics, we found a strange crash message in the backend that we can confirm is the reason all iOS 10 crashing since it's 100% iOS 10 and there's like 40 of them.
It reads as follows:
Fatal Exception: RLMException
Property Article.id is declared as String, which is not a supported managed Object property type. If it is not supposed to be a managed property, either add it to ignoredProperties() or do not declare it as #objc dynamic. See https://realm.io/docs/swift/latest/api/Classes/Object.html for more information.
And here's the object:
class Article: Object {
#objc dynamic var id: String = UUID().uuidString
// others...
override static func primaryKey() -> String? {
return "id"
}
}
As you can see, this is perfectly nomral and runs fine on other iOS. In Realm's doc, it LITERALLY SAYS to use String with #objc dynamic and there's no way it's unsupported. I suspect there's nothing special about Article.id, and since Article starts with A, it happens to be the first String property of all realm Objects. Maybe somehow all Strings stopped working on iOS 10?
Can anyone offer some advice or insights?(Please don't say things like drop iOS 10 support. For now, we need it.)
We ran into the same issue a couple of times, trying to drag Realm fully into Swift. This is not really the answer but more of a workaround we've had success with when needing backward compatibility.
It's an ObjC object, not Swift.
There's something going on with the bridging, perhaps conforming to NSCopy'ing or something along those line, so just change it to read
#objc dynamic var id = NSUUID().uuidString
See the Getting Started Guide in the Models section which calls for using NSUUID
NSUUID: An object representing a universally unique value that bridges
to UUID; use NSUUID when you need reference semantics or other
Foundation-specific behavior.
Turns out it was a Realm's bug. We happen to have another app that runs just fine on iOS 10, and after some inspection we realized that it was using Realm 4.3.2, instead of 4.4.1. After we downgraded Realm to 4.3.2, this problem disappeared.
I am trying to update my project to Swift 3 which has Core Data. I have confronted with serious error and I really don't know what is going on. My Core Data Model has following properties
#NSManaged var name: String?
#NSManaged var count: NSNumber
#NSManaged var isDelivered: NSNumber
I can set any other properties but isDelivered. When I try to use
myobject.isDelivered = true
I get following error on the console.
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[MyModel setDelivered:]:
unrecognized selector sent to instance 0x6000000d3780'
It looks like Xcode is removing is part from isDelivered property which crashes the app. Is there anything that I can do to prevent this other than updating my model? Thanks.
It is a BUG. It is very confusing bug. For anyone visiting this question, here is the answer I found on Apple forums.
The current version of Swift 3 beta seems to have some flaw about treating properties prefixed with "is".
https://forums.developer.apple.com/thread/50863
Answer from user OOPer
Avoid using "is" in your property name, or if you cannot, try this:
newWriter.setValue(true, forKey: "favorite")
(Update)
Try adding #objc name to the property:
#NSManaged #objc(isFavorite) var isFavorite: Bool
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
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.