Swift:Nil与返回typesString不兼容

我在Swift中有这样的代码:

guard let user = username else{ return nil } 

但是我收到以下错误:

 Nil is incompatible with return type String 

你们中的任何一个人都知道在这种情况下为什么或者如何返回零?

我会很感激你的帮助

你的函数是否声明了一个可选的返回types?

func foo() – > String? {…

有关详情,请访问: https : //developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

注意

C或Objective-C中不存在可选项的概念。 Objective-C中最近的东西是从一个方法返回nil的能力,否则返回一个对象,nil意思是“缺less一个有效的对象”。

Interesting Posts