Reputation: 5561
I have set up an iPhone app in Xcode and added a Cocoa-Touch iOS framework to handle the data as per apple's suggestion.
Basically, I've done everything in this tutorial:
http://www.telerik.com/blogs/send-data-to-apple-watch-with-core-data-and-telerik-ui-for-ios-in-swift
On the iPhone side it works fine calling the functions however if I try to call the exact same functions, I get this horrible error:
fatal error: unexpectedly found nil while unwrapping an Optional value
After doing some commenting out and running of the code line by line I have determined this is the offending line (same line is in both data handling functions):
var entity = NSEntityDescription.entityForName("ChartDataEntity", inManagedObjectContext: self.managedObjectContext!)
Any help or ideas appreciated!
Upvotes: 0
Views: 168
Reputation: 5561
I figured out what the issue was. I had added the files from the framework to the targets of the iphone app and also the watchkit extension, which Apple advises is not allowed anyway and they'll block your app from the store if you try to do it: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
Anyway if you come across this what you need to do is untick the targets (both the data model and the swift file):
From here:
So only your framework is ticked as a target. Now my watch app works with CoreData! That was a painful couple of days...
Upvotes: 0
Reputation: 70976
The error says unexpectedly found nil while unwrapping an Optional value
. Your line of code unwraps exactly one optional, when it uses self.managedObjectContext!
, so that's the problem. For some reason you are attempting to use self.managedObjectContext
but you have not initialized it to have any value. You need to set up the context and the rest of the Core Data stack before you can use it.
In that tutorial, lazy var managedObjectContext: NSManagedObjectContext?
might return nil if it can't create a persistent store coordinator. That in turn might be nil if it can't create the persistent store file. You'll need to work out why self.managedObjectContext
is nil before you can fix the problem.
Upvotes: 0