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() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |