二进制运算符“ +”不能应用于两个CGFloat操作数吗? [英] Binary operator '+' cannot be applied to two CGFloat operands?

查看:126
本文介绍了二进制运算符“ +”不能应用于两个CGFloat操作数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中编码并得到上面的错误...

Coding in Swift and get the above error...

消息是在屏蔽其他内容吗,还是您真的不能添加两个CGFloat操作数?如果不是,为什么(在地球上)不是呢?

Is the message masking something else OR can you really not add two CGFloat operands? If not, why (on earth) not?

我的代码中没有什么特别的试图做有趣的是,以上错误消息VERBATIM是Swift助手编译器告诉我的(用红色波浪线在我的代码下划线)。

There is nothing special in the code I am trying to do; what is interesting is that the above error message, VERBATIM, is what the Swift assistant compiler tells me (underlining my code with red squiggly lines).

运行Xcode 6.3(Swift) 1.2)

Running Xcode 6.3 (Swift 1.2)

推荐答案

使用二进制运算符 +添加两个CGFloat变量是绝对可能的。您需要知道的是,结果变量也是CGFloat变量(基于类型推断原理)。

It's absolutely possible, adding two CGFloat variables using the binary operator '+'. What you need to know is the resultant variable is also a CGFloat variable (based on type Inference Principle).

let value1 : CGFloat = 12.0
let value2 : CGFloat = 13.0
let value3   = value1 + value2
println("value3 \(value3)")

//The result is value3 25.0, and the value3 is of type CGFloat.

编辑:
通过Swift v3.0约定

By Swift v3.0 convention

let value = CGFloat(12.0) + CGFloat(13.0)
println("value \(value)")

//The result is value 25.0, and the value is of type CGFloat.

这篇关于二进制运算符“ +”不能应用于两个CGFloat操作数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆