Swift 2.2, IOS 9, Latest version of Realm
I have a classic two controller project where they are both TableView Controllers.
ViewController 1 is a list of Items: ItemsViewController ViewController 2 has the ability to add items to the trip = ItemDetailsViewController
The code to prepare the details and save the details was done using basic prepareForSeque in the details controller and an @IBAction function in the list controller
All was working well when this was done using a basic array. The array at the time was called items and a single element called item.
However, when I converted this to a Realm project things went wrong.
I started to get errors in the ItemsViewController inside the @IBAction function.
To track down the error I carried out the write command in the details controller mainly as a troubleshooting activity but intend to move it to the list controller once I sort things out
Prepare for segue code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SaveItemDetail" {
item.id = NSUUID().UUIDString
item.name = nameTextField.text!
try! uiRealm.write {
uiRealm.add(item)
}
}
}
Version 1 of the IBAction.
@IBAction func saveTripDetail(segue:UIStoryboardSegue) {
}
In this version where I do nothing at all I get a break error at the end of the block. Or if I put in a print("hello world") get a break error there at run time
Version 2 If I try to get the item information from the details controller
@IBAction func saveItemDetail(segue:UIStoryboardSegue) {
if let itemDetailsViewController = segue.sourceViewController as? ItemDetailsViewController {
if let item = itemDetailsViewController.item {
}
}
}
In this version I can't get the build to work and have the following error
initialiser for conditional binding must have Optional type not 'Item'
So I'm clearly missing something with how Realm works with classes and can't really see why. Any thoughts on this?
if letcheck on object that is non-optional, so you got that error, because it assumed never gonna benil, still, kinda hard to see whats the real problem here...maybe write more clearly - Tj3nitemDetailsViewController.itemis not option you don't need to useif let ... - Dmitry