0

I am currently learning Swift and I am experimenting with Core Data where I would like to save a linked list. To add an element at the end of the list I have a while-loop like this:

var curr = start
while (curr.nextElem != nil) {
    curr = curr.nextElem
}

The problem is though that curr.next != nil seems to be invalid, following error is shown: 'ListElem' is not convertible to UInt8

I noticed this error a few times before but always found a way around comparing but how can this be done in this case?

Deproblemify
  • 3,340
  • 5
  • 26
  • 41
  • I strongly assume that this is a duplicate of [Check if property is set in Core Data?](http://stackoverflow.com/questions/25661120/check-if-property-is-set-in-core-data). – The problem is that even if `nextElem` is defined as optional in the Core Data model inspector, Xcode does not create an optional property in the Swift class. The answer to above question shows a workaround. – Martin R Feb 20 '15 at 15:49

1 Answers1

1

Your ListElem.nextElem property seems to be returning an actual ListElem, so it can never be nil. For it to be able to be nil, it has to be of optional type (ListElem?).

Also, try the Xcode 6.3 beta — most of the error messages where Swift 1.1 said "I dunno what you're doing, so I'll just say you can't convert it to UInt8" have been replaced with better diagnostics in Swift 1.2.

rickster
  • 124,678
  • 26
  • 272
  • 326