如何做的算术之间的int和浮动迅速?

时间:2021-09-13 09:44:35

This is my first day in Swift programming and till now we are using Objective C. I tried to write simple addition program it works. Like,

这是我在Swift编程的第一天,到目前为止我们使用Objective c。就像,

var i = 10
var j = 10
var k = i + j
println(k)

But when I change one of the values to float it gives error.

但是当我改变其中一个值使其浮动时,它会产生错误。

var i = 10
var j = 10.4
var k = i + j
println(k)

Error: main.swift:13:11: Could not find an overload for '+' that accepts the supplied arguments

错误:主要。swift:13:11:找不到接受提供的参数的'+'重载

Now I did Google search and tried few thing e.g. Double(i+j) , but it doesn't work. Swift should implicitly convert int to float in this case, isn't it?

现在我做了谷歌搜索,尝试了很少的东西,比如Double(I +j),但是它不起作用。Swift应该隐式地将int转换为float,不是吗?

Please suggest if I am doing any mistake understanding Swift language.

如果我理解Swift语言有任何错误,请建议。

3 个解决方案

#1


23  

Depending on what you want your result to be, you should convert it to the appropriate type using that types init method.

根据您想要的结果,您应该使用该类型init方法将其转换为适当的类型。

eg.

如。

var myInt = 5;
var myDouble = 3.4;

If I want a double for example in my result

如果我想要一个双精度的

var doubleResult = Double(myInt) + myDouble;

if I want an integer instead, please note the double will be truncated.

如果我想要一个整数,请注意double将被截断。

var intResult = myInt + Int(myDouble)

The problem I see in your example is you're trying to do an add operation and then convert it but both values needs to be the same before you perform the addition.

我在您的示例中看到的问题是,您正在尝试执行一个add操作,然后对其进行转换,但是在执行加法之前,两个值都必须相同。

Apple has made it quiet strict to avoid type mis-match and conversion errors. Sometimes this can be a bit 'too strict' for dev coming from other languages, I was annoyed at first but I got used to it.

苹果公司为了避免类型错误匹配和转换错误而采取了严格的保密措施。有时,对于来自其他语言的开发人员来说,这可能有点“太严格”了。一开始我很生气,但我习惯了。

#2


5  

You could define your own operator...

您可以定义自己的操作符……

// Put this at file level anywhere in your project
operator infix + { }
@infix func + (a: Int, b: Double) -> Double {
    return Double(a) + b
}
@infix func + (a: Double, b: Int) -> Double {
    return Double(b) + a
}

let i = 10
let j = 10.4
let k = i + j // 20.4

...but I feel this is going against the spirit of the language (and as @TheLazyChap says, it depends what you want, which may not always be the same).

…但我觉得这违背了语言的精神(正如@TheLazyChap所说,它取决于你想要什么,这可能并不总是相同的)。

#3


0  

try this:

试试这个:

 var i = 10  //Int Type
 var j = 10.4  //Double Type
 var k = Double(i) + j //result is now Double Type
 println(k)

#1


23  

Depending on what you want your result to be, you should convert it to the appropriate type using that types init method.

根据您想要的结果,您应该使用该类型init方法将其转换为适当的类型。

eg.

如。

var myInt = 5;
var myDouble = 3.4;

If I want a double for example in my result

如果我想要一个双精度的

var doubleResult = Double(myInt) + myDouble;

if I want an integer instead, please note the double will be truncated.

如果我想要一个整数,请注意double将被截断。

var intResult = myInt + Int(myDouble)

The problem I see in your example is you're trying to do an add operation and then convert it but both values needs to be the same before you perform the addition.

我在您的示例中看到的问题是,您正在尝试执行一个add操作,然后对其进行转换,但是在执行加法之前,两个值都必须相同。

Apple has made it quiet strict to avoid type mis-match and conversion errors. Sometimes this can be a bit 'too strict' for dev coming from other languages, I was annoyed at first but I got used to it.

苹果公司为了避免类型错误匹配和转换错误而采取了严格的保密措施。有时,对于来自其他语言的开发人员来说,这可能有点“太严格”了。一开始我很生气,但我习惯了。

#2


5  

You could define your own operator...

您可以定义自己的操作符……

// Put this at file level anywhere in your project
operator infix + { }
@infix func + (a: Int, b: Double) -> Double {
    return Double(a) + b
}
@infix func + (a: Double, b: Int) -> Double {
    return Double(b) + a
}

let i = 10
let j = 10.4
let k = i + j // 20.4

...but I feel this is going against the spirit of the language (and as @TheLazyChap says, it depends what you want, which may not always be the same).

…但我觉得这违背了语言的精神(正如@TheLazyChap所说,它取决于你想要什么,这可能并不总是相同的)。

#3


0  

try this:

试试这个:

 var i = 10  //Int Type
 var j = 10.4  //Double Type
 var k = Double(i) + j //result is now Double Type
 println(k)