Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if property is set in Core Data?

how can i check if an property is set in an Core Data Object?

I load all my Core Data Objects in an Directory:

var formQuestions = [Questions]()

And my Core Data NSManagementObject is:

@NSManaged var noticeText: String

formQuestions[indexPath.row].noticeText

// load with:

var fetchRequest = NSFetchRequest(entityName: "Questions")
fetchRequest.predicate = NSPredicate(format: "forms = %@", currentForm!)
formQuestions = context.executeFetchRequest(fetchRequest, error: nil) as [Questions]

My property "noticeText" could be empty or not, so when i create my Core Data Object some values could be not set. (The property is set as optional in Core Data)

When i now try to proove if there is a value, it always brings me an "EXC_BAD_ACCESS...."

if(formQuestions[indexPath.row].noticeText.isEmpty == false)

I could set an empty String when i create my Core Data Object, but that should be not a good solution.

So how can i check if an (optinal) and not setted value exists?

Thanks in advance.

like image 201
derdida Avatar asked Sep 04 '14 08:09

derdida


People also ask

What is Core Data database?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

What is managed object model in Core Data?

A managed object model is a set of objects that together form a blueprint describing the managed objects you use in your application. A model allows Core Data to map from records in a persistent store to managed objects that you use in your application.

How do I enable Core Data in Xcode?

Get our help adding Core Data to your project So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we'll be deleting it when we're done. You'll see the “Use Core Data” checkbox on the project options screen, make sure it is checked.

Can Core Data store arrays?

"arrays" in Core Data are often implemented using relationships, but there is another option. let' suppose you want some entity X to have an associated Set of Strings. option 1: create a new Entity in Core Data to represent a single String.


2 Answers

Update for Xcode 7: This issue has been solved with Xcode 7 beta 2. Optional Core Data properties are now defined as optional properties in the managed object subclasses generated by Xcode. It is no longer necessary to edit the generated class definition.


(Previous answer:)

When creating the NSManagedObject subclasses, Xcode does not define optional properties for those attributes which are marked as "optional" in the Core Data model inspector. This looks like a bug to me.

As a workaround, you can cast the property to an optional (as String? in your case) and then test it with optional binding

if let notice = someManagedObject.noticeText as String? {
    println(notice)
} else {
    // property not set
}

In your case that would be

if let notice = formQuestions[indexPath.row].noticeText as String? {
    println(notice)
} else {
    // property not set
}

Update: As of Xcode 6.2, this solution does not work anymore and crashes with a EXC_BAD_ACCESS runtime exception (compare Swift: detecting an unexpected nil value in a non-optional at runtime: casting as optional fails)

The "Old answer" solution below still works.


(Old answer:)

As @Daij-Djan already stated in a comment, you have to define the property for an optional Core Data attribute as optional or implicitly unwrapped optional:

@NSManaged var noticeText: String? // optional
@NSManaged var noticeText: String! // implicitly unwrapped optional

Unfortunately, Xcode does not define the optional properties correctly when creating the NSManagedObject subclasses, which means that you have to re-apply the changes if you create the subclasses again after a model change.

Also this seems to be still undocumented, but both variants worked in my test case.

You can test the property with == nil:

if formQuestions[indexPath.row].noticeText == nil {
    // property not set
}

or with an optional assignment:

if let notice = formQuestions[indexPath.row].noticeText {
    println(notice)
} else {
    // property not set
}
like image 193
Martin R Avatar answered Oct 11 '22 10:10

Martin R


Your application is crashing because you try to access a not optional variable that is nil. This is not allowed in swift. To fix your problem just add in the NSManagedObject subclass a ? to made the property optional:

@objc class MyModel: NSManagedObject {
    @NSManaged var noticeText: String? // <-- add ? here
}

Then to test the property you can do like this :

if let aNoticeText = formQuestions[indexPath.row].noticeText? {
    // do something with noticeText
}
like image 44
ant_one Avatar answered Oct 11 '22 10:10

ant_one