如何在快速运动场上转换智力?

时间:2023-01-23 15:09:04

I made a new Playground and wrote the code as following.

我建了一个新的运动场,并按照如下的方式写代码。

var value = 33 //This will make an integer type of value variable with value 33

value = 22.44 //This doesn't execute as its assigning Double/Float value to an integer.

I tried

我试着

1. 
value = Double(value)
value = 44.55 //Error here

2. 
value = Double(33.44) //Error here too.

3.
value : Double = 33.22 //Error here too.

Now what should I do to assign floating point to value.

现在我该怎么做才能把浮点赋值。

NOTE: I am at learning level in Swift.

注意:我在Swift学习级别。

Thanks.

谢谢。

3 个解决方案

#1


12  

Declaring var value = 33 will infer the type of value as Int. If you want to assign 33 and make value as Float or Double, you have to declare the type var value : Double = 33 or convert 33 to a Double while assign var value = Double(33).

声明var值= 33将推断值的类型为Int类型。如果您想分配33并使值为Float或Double,则必须声明类型var值:Double = 33或将33转换为Double,同时赋值var值= Double(33)。

#2


1  

You must set the data type within the variable declaration.

您必须在变量声明中设置数据类型。

var value: Double = 33

But you could also do it like so:

但你也可以这样做:

var value: Double
value = 33

Defining it as a var will make the variable mutable, so after defining you can change the value

将它定义为var将使变量变为可变的,因此在定义之后可以更改该值

value = 33.2
value = 46.1

If you are only defining a constant, or a variable which does not need to change, you're best defining it like so:

如果你只是定义一个常数,或者一个不需要改变的变量,你最好这样定义:

let value: Double = 33.2

If you need this to be an Int for any reason at some point you can pass it through to a function or define it like so:

如果你需要这是一个Int因为任何原因,你可以把它传递给一个函数或者定义它:

let intValue = Int(value)

#3


0  

With the first line

与第一行

var value = 33

you created a variable with the type of Int (because 33 is an integer literal). After that, you cannot assign any other type to that, only Ints. You should create another variable to store the result of the convertion.

您使用Int类型创建了一个变量(因为33是一个整数文本)。在此之后,您不能为它分配任何其他类型,只分配Ints。您应该创建另一个变量来存储转换的结果。

#1


12  

Declaring var value = 33 will infer the type of value as Int. If you want to assign 33 and make value as Float or Double, you have to declare the type var value : Double = 33 or convert 33 to a Double while assign var value = Double(33).

声明var值= 33将推断值的类型为Int类型。如果您想分配33并使值为Float或Double,则必须声明类型var值:Double = 33或将33转换为Double,同时赋值var值= Double(33)。

#2


1  

You must set the data type within the variable declaration.

您必须在变量声明中设置数据类型。

var value: Double = 33

But you could also do it like so:

但你也可以这样做:

var value: Double
value = 33

Defining it as a var will make the variable mutable, so after defining you can change the value

将它定义为var将使变量变为可变的,因此在定义之后可以更改该值

value = 33.2
value = 46.1

If you are only defining a constant, or a variable which does not need to change, you're best defining it like so:

如果你只是定义一个常数,或者一个不需要改变的变量,你最好这样定义:

let value: Double = 33.2

If you need this to be an Int for any reason at some point you can pass it through to a function or define it like so:

如果你需要这是一个Int因为任何原因,你可以把它传递给一个函数或者定义它:

let intValue = Int(value)

#3


0  

With the first line

与第一行

var value = 33

you created a variable with the type of Int (because 33 is an integer literal). After that, you cannot assign any other type to that, only Ints. You should create another variable to store the result of the convertion.

您使用Int类型创建了一个变量(因为33是一个整数文本)。在此之后,您不能为它分配任何其他类型,只分配Ints。您应该创建另一个变量来存储转换的结果。