ios Swift 支持隐式转换吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24645798/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:17:35  来源:igfitidea点击:

Does Swift support implicit conversion?

iosswiftimplicit-conversionprimitive-types

提问by Bagusflyer

For example, I have the following code:

例如,我有以下代码:

    let numberOfBlocks = 3
    let blockWidth = SKSpriteNode(imageNamed: "image.png").size.width
    let padding = 20.0

    let offsetX : Float = (self.frame.size.width - (blockWidth * numberOfBlocks + padding * (numberOfBlocks-1))) / 2

I got the error:

我得到了错误:

'Double' is not convertible to 'UInt8'

'Double' 不能转换为 'UInt8'

Is there a way to implicitly convert the data type (maybe only for primitive data type)?

有没有办法隐式转换数据类型(可能仅适用于原始数据类型)?

Edit:

编辑:

I know how to do the explicit conversion by using constructor of particular type as Iducoolsuggested. But it's not a big help to my question because we even don't know where to add the conversions. I simplified my expression in playground:

我知道如何通过使用Iducool建议的特定类型的构造函数来进行显式转换。但这对我的问题没有多大帮助,因为我们甚至不知道在哪里添加转换。我在操场上简化了我的表达:

enter image description here

在此处输入图片说明

The problem is in "padding" variable, the error message is

问题出在“填充”变量中,错误消息是

'Double' is not convertible to 'UInt8'.

“Double”不可转换为“UInt8”。

So I did the conversion:

所以我做了转换:

enter image description here

在此处输入图片说明

Then the problem is in "blockWidth" variable now.

那么问题现在在“blockWidth”变量中。

I added the conversion again:

我再次添加了转换:

enter image description here

在此处输入图片说明

And error message is:

错误信息是:

Type 'UInt8' does not conform to protocol 'FloatLiteralCovertible'

类型“UInt8”不符合协议“FloatLiteralCovertible”

The final working expression is:

最终的工作表达式是:

enter image description here

在此处输入图片说明

Is it simple and swift? I don't think so.

它简单快捷吗?我不这么认为。

回答by Iducool

There is no implicitly cast in Swift.

Swift 中没有隐式转换。

Easy way of conversion in swift is using constructor of particular type.

swift 中的简单转换方法是使用特定类型的构造函数。

Like if you want to get Float from double then you can use Float(doubleValue)and Same way if you want to convert float to integer then you can use Int(floatValue).

就像如果你想从 double 得到 Float 那么你可以使用Float(doubleValue)和同样的方式如果你想将 float 转换为 integer 那么你可以使用Int(floatValue).

In your case:

在你的情况下:

let intValue = UInt8(doubleValue)

Beware that you will lose any value after the decimal point. So, choose a better way. Above conversion is just to help you in understanding.

请注意,您将丢失小数点后的任何值。所以,选择更好的方式。以上转换只是为了帮助您理解。

Note that Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.

请注意,Swift 在推断浮点数类型时总是选择 Double(而不是 Float)。

回答by Bryan Chen

Swift doesn't support implicitly cast anymore in Xcode6 GM. Following answer only apply to Xcode6 beta version.

Swift 不再支持 Xcode6 GM 中的隐式转换。以下答案仅适用于 Xcode6 测试版。



I don't want to talk about implicitly cast is good or bad, but you can have it if you really want with __conversion()

我不想隐含地谈论演员是好是坏,但如果你真的想要,你可以拥有它 __conversion()

e.g. If you need UInt8and Intbe able to convert from Double

例如,如果您需要UInt8并且Int能够从Double

extension Double {
    func __conversion() -> UInt8 { return UInt8(self) }
    func __conversion() -> Int { return Int(self) }
    // add more if you need to
}


xcrun swift
Welcome to Swift!  Type :help for assistance.
  1> extension Double {
  2.     func __conversion() -> UInt8 { return UInt8(self) }
  3. }
  4> var d = 1.0
d: Double = 1
  5> var u8 : UInt8 = d
u8: UInt8 = 1
  6>


Note: I won't put this in my production code. I only want to point out it if possible but not recommending it.

注意:我不会把它放在我的生产代码中。如果可能的话,我只想指出它,但不推荐它。

回答by Raj

using bridgeToObjectiveC() method you can call the methods provided in Objective - C to convert from one primitive data type to another for e.g.

使用 bridgeToObjectiveC() 方法,您可以调用 Objective-C 中提供的方法将一种原始数据类型转换为另一种,例如

variable_name.bridgeToObjectiveC().intValue

will convert that variable named variable_name to integer

会将名为 variable_name 的变量转换为整数