加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift错误: Type "xxx" does not confirm to prot

发布时间:2020-12-14 02:01:56 所属栏目:百科 来源:网络整理
导读:刚接触swift,做值判断是否为空的时候,报了这个错误,Type "String" does not confirm to protocol "BooleanType" let s_num1 = "123";let s_num2 = s_num1.toInt()var s_num4 = 123;if (s_num1 != nil) {println("true")} //这行报错if (s_num2 != nil) {p

刚接触swift,做值判断是否为空的时候,报了这个错误,Type "String" does not confirm to protocol "BooleanType"

let s_num1 = "123";
let s_num2 = s_num1.toInt()
var s_num4 = 123;

if (s_num1 != nil) {println("true")}  //这行报错
if (s_num2 != nil) {println("true")}  //这里不报错
if (s_num4 != nil) {println("true")}  //这里报错
上面,s_num2跟另外两个的区别是:s_num2是optional类型,而将s_num4设置为nil的时候也是报错的,所以可以这样理解,swift中如果一个变量可能为nil,那么在声明的时候一定要加上?,不然就不能为nil。s_num2的实际写法其实是:
let s_num2:Int? = s_num1.toInt()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读