Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'UInt16' and 'UInt8'
来源:互联网 发布:怎么开淘宝店铺步骤 编辑:程序博客网 时间:2024/06/04 23:14
1. Swift报错: Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'UInt16' and 'UInt8'
错误写法:
let a:UInt16 =1_000
let b:UInt8 =1
let sum = a + b
println("sum =\(sum)")
正确写法:
let a:UInt16 =1_000
let b:UInt8 =1
let sum = a +UInt16(b)
println("sum =\(sum)")
2. Swift报错: Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'Int' and 'Double'
错误写法:
let a =123
let b = 0.456
let sum = a + b
println("sum = \(sum)")
错误原因: 加好两边的数的类型必须相同,不能直接相加.所以int和double类型不能直接相加.
正确写法:
let a =123
let b = 0.456
let sum = Double(a) + b
println("sum = \(sum)")
注意: 区别 let sum =123 +0.456 相这样写是正确的,因为数字字面量123本身没有类型,它们会在编译器需要求值的时候被推断.
以上的Double(a)和 UInt16(b) 这样的someType(ofInitialValue)是调用Swift构造器并传入一个初始化的默认方法.在语言内部,UInt16有一个构造器,可以接收一个UInt8类型的值,所以这个构造器可以用现有的UInt8来创建一个新的UInt16.注意你并不能传入任意的值,只能传入UInt16内部有对应构造器的值.不过你可以扩展现有的类型来让它可以接收其他类型的值(包括自定义的类型)
0 0
- Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'UInt16' and 'UInt8'
- Error1Operator '+' cannot be applied to operands of type 'string' and 'method group'
- Swift中推送通知设置:Binary operator '|' cannot be applied to two UIUserNotificationType operands 错误解决
- 【iOS】swift-Binary operator '|' cannot be applied to two UIViewAutoresizing operands
- Binary operator ‘|’ cannot be applied to two ‘UIViewAutoresizing’ operands
- Swift2.0后"|" cannot be applied to two operands问题解决方法
- cannot be applied to
- error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator^’
- ERROR:invalid operands to binary -
- swift报错binary operator '==' cannot be applied of two(结构体枚举如何比较大小)
- Swift Compiler Error Cannot assign to the result of this expression
- [main]- ERROR 1037 Operands of Regex can be CharArray only :(Name: Regex Type: null Uid: null)
- “cannot be resolved to a type” error
- Swift Compiler Error Integer literal overflows when stored into 'UInt8'
- 类似Build Error: #513: a value of type "int" cannot be assigned to an entity of type "char *"
- 6410_io_drv.c: error: invalid operands to binary << (have 'char *' and 'int') 错误处理
- "invalid operands of types ‘float Point3d::*’ and ‘float Point3d::*’ to binary ‘operator<’"
- Eclipse “cannot be resolved to a type” error
- C#读写txt文件的两种方法介绍
- hdu 4460 Friend Chains(最短路径,spfa)
- mule,spring构建resful应用服务
- webapp弹层
- SQL Server游标的使用
- Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'UInt16' and 'UInt8'
- 设计模式--装饰模式(Decorator)
- hdu 2883 kebab
- 初步探索Log4j
- LeetCode Binary Tree Zigzag Level Order Traversal
- 使用WinDug查内存泄露
- STM32之系统重启
- GraphicsMagick和im4java按比例压缩图片
- 20150706