func getUserById(userId: Int) -> User {
if (userId != nil) {
...
}
return nil
}
上述代碼報了一個錯誤:“'nil' is incompatible with return type 'User'”,表示“nil”與返回類型“User”不兼容。
解決方案:
將返回值類型改為Optional類型User?
func getUserById(userId: Int) -> User? {
if (userId != nil) {
...
}
return nil
}
同理,如果調(diào)用getUserById(nil)的時候也會報錯:“'nil' is not compatible with expected argument type 'Int'”文章來源:http://www.zghlxwxcb.cn/news/detail-701554.html
將參數(shù)類型改為Int?即可文章來源地址http://www.zghlxwxcb.cn/news/detail-701554.html
func getUserById(userId: Int?) -> User? {
if (userId != nil) {
...
}
return nil
}
到了這里,關(guān)于Swift報錯:“‘nil‘ is incompatible with return type ‘User‘”的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!