ios 快速声明变量

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

Declaring variables in swift

iosswift

提问by Biscuit128

I am trying to follow a tutorial which says if we don't want to initialise a variable in swift we can do the following;

我正在尝试遵循一个教程,该教程说如果我们不想快速初始化变量,我们可以执行以下操作;

var year:Integer
year = 2;

However, if I declare the above block of code I get an error

但是,如果我声明上述代码块,则会出现错误

"use of undeclared type Integer"

“使用未声明的整数类型”

If i use the following instead it works;

如果我使用以下内容,它会起作用;

var year:integer_t
year = 2;

Why do I need to do this yet the tutorial can use the first method?

为什么我需要这样做而教程可以使用第一种方法?

Thanks

谢谢

Edit : screen shot from tutorial

编辑:来自教程的屏幕截图

enter image description here

在此处输入图片说明

回答by AlBlue

You need to use an Int, not Integer.

您需要使用 Int,而不是 Integer。

var year:Int
year = 2

Note: You can do this in one line

注意:您可以在一行中执行此操作

var year:Int = 2

and use type inference

并使用类型推断

var year = 2

I've been blogging a little about Swift if you're interested, starting here:

如果你有兴趣,我一直在写一些关于 Swift 的博客,从这里开始:

http://alblue.bandlem.com/2014/09/swift-introduction-to-the-repl.html

http://alblue.bandlem.com/2014/09/swift-introduction-to-the-repl.html

(You can subscribe to the feed at http://alblue.bandlem.com/Tag/swift/)

(您可以在http://alblue.bandlem.com/Tag/swift/订阅提要)

回答by Tamás Sengel

If the variable never changes, you should declare a constantinstead.

如果变量永远不会改变,你应该声明一个常量

let year: Int = 2

...or with type inference:

...或类型推断:

let year = 2

Note that Swift infers Intwhen you assign a whole number to a variable/constant and Doublewhen you assign a fraction.

请注意,Int当您将整数分配给变量/常量以及Double分配分数时,Swift 会进行推断。

回答by Yogendra Singh

In addition if you have created a third party for others and you want a value to enter from user. The you can create variable with the placeholder.

此外,如果您为他人创建了第三方并且您希望从用户输入一个值。您可以使用占位符创建变量。

var APIKey: String = <"Place API key here">

回答by Kumar Vivek Mitra

-First its notIntegerthats the type in swift but Int.

-首先,它不是Integerswift 中的类型,而是Int.

Using Type Annotation:

使用类型注解:

var year : Int
year = 2

Using Type Inference:

使用类型推断:

var year = 2;

Here the type Intis inferred by the compiler during the assignment of the integer literalto the variable year

这里的类型Int是由编译器在分配integer literal给变量的过程中推断出来的year

回答by Barry Wang

In swift book said that “A constant or variable must have the same type as the value you want to assign to it. However, you don't always have to write the type explicitly”. The var year that you first assign it a int type value,so you can't assign another type value to it.

在 swift 书中说:“常量或变量必须与您要分配给它的值具有相同的类型。但是,您不必总是显式地编写类型”。您首先为它分配一个 int 类型值的 var year,因此您不能为其分配另一个类型值。