1
votes

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?

1
Seems like the error is on other thread on your version 1 something, and version 2 is doesnt seems like its related to Realm, its Swift stuff, because you do if let check on object that is non-optional, so you got that error, because it assumed never gonna be nil, still, kinda hard to see whats the real problem here...maybe write more clearly - Tj3n
Could you please provide error message you get in V1? As for v2 if your itemDetailsViewController.item is not option you don't need to use if let ... - Dmitry
OK, it looks as though there was something in the comments that swift was not interpreting as a comment and therefore was causing an issue. I blew away all comments and although things not working perfectly, all the weird errors have gone - Michael Moulsdale

1 Answers

1
votes

OK, this all seemed to be down to some commented out code that swift was interpreting and therefore caused all sorts of issues, which then led me down all sorts of rabbit holes.

Closing the issue