微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift学习笔记一

1.Constants and Variables(常量和变量)

let定义常量var定义变量。

[Note] If a stored value in your code won’t change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change.
如果代码中的存储值不会更改,则始终使用let关键字将其声明为常量。仅将变量用于存储需要更改的值。

2.Type Annotations(类型注解)——不同与C语言

Write a type annotation by placing a colon(冒号) after the constant or variable name, followed by a space, followed by the name of the type to use.var/let <变量名>(:)(空格)<类型名>

var welcomeMessage: String
welcomeMessage = "Hello"

The colon in the declaration means “…of type…,” so the code above can be read as:

“Declare a variable called welcomeMessage that is of type String.”

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

var red, green, blue: Double

[Note]在实践中很少需要编写类型注释。如果在定义常量或变量时为其提供一个初始值,Swift几乎总是可以推断出该常量或变量所使用的类型,如类型安全性和类型推断中所述。在上面的welcomeMessage示例中,没有提供初始值,因此welcomeMessage变量的类型是用类型注释指定的,而不是从初始值推断出来的。

3.Naming Constants and Variables(命名常量和变量)

Constant and variable names can contain almost any character, including Unicode characters:

let π = 3.14159
let 你好 = "你好世界"
let
                
                                 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐