objective-c 如何处理 Int 和 CGFloat 之间的操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24095197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I deal with operation between Int and CGFloat?
提问by drinking
In Objective-C I can do the following operation:
在Objective-C中,我可以进行以下操作:
Objective-C code:
目标-C代码:
CGFloat width = CGRectGetWidth(mView.bounds);
CGFloat total = width*[myArray count];
but in Swift, it will raise an error:
但是在 Swift 中,它会引发一个错误:
Could not find an overload for '*' that accepts the supplied arguments
找不到接受提供的参数的“*”的重载
How can I avoid this situation elegantly?
我怎样才能优雅地避免这种情况?
回答by Sulthan
First, let's create some demo data
首先,让我们创建一些演示数据
let array: NSArray = ["a", "b", "c"] //you could use a Swift array, too
let view = UIView() //just some view
Now, everything else works almost the same way as in Obj-C
现在,其他一切都以与 Obj-C 中几乎相同的方式工作
let width: CGFloat = CGRectGetWidth(view.bounds)
or simply
或者干脆
let width = CGRectGetWidth(rect) //type of the variable is inferred
and total:
和总计:
let total = width * CGFloat(array.count)
Note that we have to add a CGFloatcast for array.count. Obj-C would implicitly cast NSUIntegerto CGFloatbut Swift has no implicit casts, so we have to add an explicit one.
请注意,我们必须CGFloat为array.count. Obj-C 会隐式转换NSUInteger为 toCGFloat但 Swift 没有隐式转换,所以我们必须添加一个显式转换。
回答by Dash
In Swift you cannot multiply two numbers of different types (NSNumber, Int, Double, etc.) directly. The width of a CGRect is of floating point type and the array count is of integer type. Here's a working example:
在 Swift 中,您不能直接将两个不同类型(NSNumber、Int、Double 等)的数字相乘。CGRect 的宽度为浮点类型,数组计数为整数类型。这是一个工作示例:
let myArray: Int[] = [1,2,3]
let rect: CGRect = CGRect(x:0,y:0,width:100,height:100)
let total: Double = rect.size.width * Double(myArray.count)
回答by Imanou Petit
Swift does not allow operations between two numbers of different types. Therefore, before to multiply your array.count(Int) by your width (CGFloat), you'll have to cast it to CGFloat.
Swift 不允许在两个不同类型的数字之间进行运算。因此,在将您的array.count( Int)乘以您的宽度 ( CGFloat) 之前,您必须将其转换为CGFloat.
Fortunately, Swift provides a simple CGFloatinitializer init(_:)that creates a new CGFloatfrom an Int. This initializer has the following declaration:
幸运的是,Swift 提供了一个简单的CGFloat初始化器init(_:),可以CGFloat从Int. 此初始化程序具有以下声明:
init<Source>(_ value: Source) where Source : BinaryInteger
Creates a new value, rounded to the closest possible representation.
创建一个新值,四舍五入到最接近的可能表示。
The Swift 5 Playground sample code below shows how to perform your calculation by using CGFloat's initializer:
下面的 Swift 5 Playground 示例代码显示了如何使用CGFloat的初始化程序执行计算:
import UIKit
import CoreGraphics
// Set array and view
let array = Array(1...3)
let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
let view = UIView(frame: rect)
// Perform operation
let width = view.bounds.width
let total = width * CGFloat(array.count)
print(total) // prints: 300.0
回答by Zgpeace
need to change All Int to CGFloat Type, or change All CGFloat to Int
需要把 All Int 改成 CGFloat 类型,或者把 All CGFloat 改成 Int
let a: CGFloat = 0.25
let b: Int = 1
// wrong: Binary operator '*' cannot be applied to operands of type 'CGFloat' and 'Int'
// let c = a * b
// right: change All Int to CGFloat Type
let r = a * CGFloat(b)
// right: change All CGFloat to Int
let r = Int(a) * b

