简体   繁体   English

Swift:检查变量是否为nil

[英]Swift: Check if variable is nil

I am currently learning Swift and I am experimenting with Core Data where I would like to save a linked list. 我目前正在学习Swift,并且正在尝试使用Core Data来保存链接列表。 To add an element at the end of the list I have a while-loop like this: 要在列表的末尾添加元素,我有一个while循环,如下所示:

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 问题是,虽然curr.next != nil似乎无效,但显示以下错误: '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? 我之前几次注意到此错误,但总是找到一种比较的方法,但是在这种情况下该怎么办?

Your ListElem.nextElem property seems to be returning an actual ListElem , so it can never be nil. 您的ListElem.nextElem属性似乎正在返回实际的ListElem ,因此它永远不能为nil。 For it to be able to be nil, it has to be of optional type ( ListElem? ). 为了使其为零,它必须为可选类型( 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. 此外,请尝试Xcode 6.3 beta-大多数错误消息,其中Swift 1.1表示“我不知道您在做什么,所以我只能说您无法将其转换为UInt8 ”已被Swift 1.2中更好的诊断程序所取代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM