Swift报错:“‘nil‘ is incompatible with return type ‘User‘”

news/2024/7/20 23:02:23 标签: swift, ios
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
}

同理,如果调用getUserById(nil)的时候也会报错:“'nil' is not compatible with expected argument type 'Int'”

将参数类型改为Int?即可

func getUserById(userId: Int?) -> User? {
    if (userId != nil) {
        ...
    }
    return nil
}


http://www.niftyadmin.cn/n/5012061.html

相关文章

Jmeter系列-阶梯加压线程组Stepping Thread Group详解(6)

前言 tepping Thread Group是第一个自定义线程组但,随着版本的迭代,已经有更好的线程组代替Stepping Thread Group了【Concurrency Thread Group】,所以说Stepping Thread Group已经是过去式了,但还是介绍一下 Stepping Thread …

超高清Mac动态壁纸桌面:Dynamic Wallpaper for Mac中文版本

Dynamic Wallpaper for Mac是一款引人注目的动态壁纸软件,为你的Mac桌面带来全新的视觉体验。 [特点1]:轻松定制动态壁纸:这款软件具有用户友好的界面,使得定制动态壁纸变得异常简单。用户可以轻松选择和调整动画效果&#xff0c…

[小尾巴 UI 组件库] 组件库配置与使用

文章归档于:https://www.yuque.com/u27599042/row3c6 组件库地址 npm:https://www.npmjs.com/package/xwb-ui?activeTabreadme小尾巴 UI 组件库源码 gitee:https://gitee.com/tongchaowei/xwb-ui小尾巴 UI 组件库测试代码 gitee&#xff1a…

笙默考试管理系统-MyExamTest----codemirror(18)

笙默考试管理系统-MyExamTest----codemirror(18) 目录 一、 笙默考试管理系统-MyExamTest----codemirror 二、 笙默考试管理系统-MyExamTest----codemirror 三、 笙默考试管理系统-MyExamTest----codemirror 四、 笙默考试管理系统-MyExamTest---…

查看视频文件关键帧间隔

一、Elecard StreamEye Tools拖放视频文件查看。 红的是I帧;蓝的是P帧;绿的是B帧。 二、ffprobe -show_streams统计。 1、统计视频关键帧、非关键帧 ffprobe.exe -i 1.mp4 -show_streams v -show_packets -print_format json > d:\1.json 再统计1.j…

Qt多线程网络通信-[套接字通信 socket]

构造函数 QTcpServer::QTcpServer(QObject *parent nullptr) 创建套接字后 设置监听 bool QTcpServer::listen(const QHostAddress &address QHostAddress::Any, quint16 port 0) QHostAddress::Any支持IPV4和IPV6,使用默认参数 端口号可以指定8000以上的 判断客户…

C# WPF 自己写的一个模拟病毒传播的程序,有可视化

源代码: https://github.com/t39q/VirusSpread 主要代码 using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;namespace VirusSpread.Bu…

索尼 toio™ 应用创意开发征文 | 如何用Python控制Q宝进行机器人擂台赛

你是否曾经想过,如果能用编程来控制真实的物体,那该有多有趣?如果能让一个小方块按照你的指令来移动、旋转、闪烁,那该有多酷?如果能让一个小方块和其他小方块互动,那该有多神奇?这些想法&#…