ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Swift学习笔记(一)

2020-02-11 17:50:57  阅读:214  来源: 互联网

标签:常量 Int 笔记 学习 类型 let print 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 

标签:常量,Int,笔记,学习,类型,let,print,Swift
来源: https://www.cnblogs.com/lvhang/p/12295942.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有