当前位置:网站首页>【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
2022-07-06 19:36:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快。
1、定义变量和常量
var 定义变量,let定义常量。
比如:
var test = 1
test = 2 //变量能够改变值
let test = 1
test = 2 //常量不能改变值,编译器会报错
var test1=1,test2=2,test3=3 //逗号分隔多个变量2、添加类型标注
在 var test = 1 这个样例中。test被swift判断为int类型。
swift是类型安全的。
var test: Int = 1 这个定义和上面是一样的。仅仅是给test这个变量添加了一个类型标注。告诉swfit不用判断了。
3、基础数据类型
Int表示整型值;Double和Float表示浮点型值;Bool是布尔型值;String是文本型数据;Character是字符类型。Swift 还提供了两个主要的集合类型。Array和Dictionary
4、全局输出函数 println和print
这个大家都知道,换行和不换行的差别。输出到控制台。
比如:println(“this is my first swift test”)
如今要把上面定义的test进行输出怎么办?
println(“test value = \(test)”)
Swift 用字符串插值(string interpolation)的方式把常量名或者变量名当做占位符增加到长字符串中,Swift 会用当前常量或变量的值替换这些占位符,就是\()
5、凝视 //当行凝视 /* */片段凝视
6、分号 可要可不要。任意,可是假设一行写多条独立的语句就必需要分号了
比如:let test = “test”;println(test)
7、整数 整数分为有符号(正,负,0)和无符号(正,0)
Swift 提供了8,16。32和64位的有符号和无符号整数类型。这些整数类型和 C 语言的命名方式非常像。比方8位无符号整数类型是UInt8。32位有符号整数类型是Int32。
就像 Swift 的其它类型一样。整数类型採用大写命名法。
let minValue = UInt8.min // minValue 为 0。是 UInt8 类型的最小值
let maxValue = UInt8.max // maxValue 为 255。是 UInt8 类型的最大值Int 和 UInt
一般来说。你不须要专门指定整数的长度。Swift 提供了一个特殊的整数类型Int,长度与当前平台的原生字长同样:
在32位平台上,Int和Int32长度同样。
在64位平台上,Int和Int64长度同样。
Swift 也提供了一个特殊的无符号类型UInt,长度与当前平台的原生字长同样:
在32位平台上。UInt和UInt32长度同样。 在64位平台上,UInt和UInt64长度同样
8、浮点数
Double 和 Float
浮点数是有小数部分的数字
浮点类型比整数类型表示的范围更大。能够存储比Int类型更大或者更小的数字。
Swift 提供了两种有符号浮点数类型:
Double表示64位浮点数。当你须要存储非常大或者非常高精度的浮点数时请使用此类型。
Float表示32位浮点数。
精度要求不高的话能够使用此类型。
Double准确度非常高。至少有15位数字,而Float最少仅仅有6位数字。选择哪个类型取决于你的代码须要处理的值的范围。
9、数值型字面量
整数字面量能够被写作:
一个十进制数。没有前缀 一个二进制数。前缀是0b 一个八进制数,前缀是0o 一个十六进制数。前缀是0x
浮点字面量能够是十进制(没有前缀)或者是十六进制(前缀是0x)。小数点两边必须有至少一个十进制数字(或者是十六进制的数字)。浮点字面量另一个可选的指数(exponent)。在十进制浮点数中通过大写或者小写的e来指定。在十六进制浮点数中通过大写或者小写的p来指定。
假设一个十进制数的指数为exp。那这个数相当于基数和10^exp的乘积:
1.25e2 表示 1.25 × 10^2,等于 125.0。 1.25e-2 表示 1.25 × 10^-2,等于 0.0125。
假设一个十六进制数的指数为exp,那这个数相当于基数和2^exp的乘积:
0xFp2 表示 15 × 2^2,等于 60.0。 0xFp-2 表示 15 × 2^-2,等于 3.75。 以下的这些浮点字面量都等于十进制的12.1875:
let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0 数值类字面量能够包括额外的格式来增强可读性。整数和浮点数都能够加入额外的零而且包括下划线,并不会影响字面量:
let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1
看起来就头疼,哪那么麻烦都用十进制不就好了。。。 吐吐槽 还是有点用的。
。。
10、字符类型的类型转换
var one: Int = 10
var two = 3.11
var three = Double(one) + two //Double有整型构造器,其他整数类型一样处理,这里是演示样例11、布尔值 true false
12、类型别名 typealias
给整型改个名字,意义不大。
阅读性变差。。比如
typealias MyInt = Int
var test: MyInt = 12313、元组 这个挺重要的
元组(tuples)把多个值组合成一个复合值。
元组内的值能够使随意类型。并不要求是同样类型。
比如创建一个(Int,String)类型的元组 : (500,”server error”)
let Mytuples = (500,"server error") //定义一个元组
let (code,message) = Mytuples //将元组数据分解输出
let (code,_) = Mytuples //仅仅须要第一个值 。不须要的用_表示吧
println("code is \(code)") //输出500
println("code is \(Mytuples.0)") //通过下标訪问 输出500
let Mytuples2 = (code:500,message:"server error") //定义一个带參数名称的元组
println("code is \(Mytuples2.code)");//通过元组元素名称訪问 输出500 14、可选类型
使用可选类型(optionals)来处理值可能缺失的情况。
可选类型表示:
有值,等于 x 或者 没有值 nil
let possibleNumber = “123” let convertedNumber = possibleNumber.toInt() // convertedNumber 被猜測为类型 “Int?
“, 或者类型 “optional Int” 由于toInt方法可能会失败。所以它返回一个可选类型(optional)Int,而不是一个Int。一个可选的Int被写作Int?
而不是Int。问号暗示包括的值是可选类型,也就是说可能包括Int值也可能不包括值。
(不能包括其它不论什么值比方Bool值或者String值。仅仅能是Int或者什么都没有。)
if convertedNumber {
println("\(possibleNumber) has an integer value of \(convertedNumber!)")
} else {
println("\(possibleNumber) could not be converted to an integer")
}
// 输出 "123 has an integer value of 123" 你能够在可选的名字后面加一个感叹号(!)来获取值
上面的多麻烦啊 还得推断变量有没有值:用if推断,再用!进行强制解析。
能够用可选绑定来简化代码:
if let actualNumber = possibleNumber.toInt() {
println("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
println("\(possibleNumber) could not be converted to an integer")
}
// 输出 "123 has an integer value of 123"也能够用隐式解析可选类型来处理(确定变量总是会有值才行,否则会出错的)
let assumedString: String! = "An implicitly unwrapped optional string."
println(assumedString) // 不须要感叹号
// 输出 "An implicitly unwrapped optional string."15、断言 推断逻辑 true继续运行 false 结束应用
比如:let a = 1;assert(a >= 0) 就会触发
版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116776.html原文链接:https://javaforall.cn
边栏推荐
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
- PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
- sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
- MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
- Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
- QT common Concepts-1
- Huitong programming introductory course - 2A breakthrough
- Es6中Promise的使用
- How to find file accessed / created just feed minutes ago
- Examples of how to use dates in Oracle
猜你喜欢

杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】

Have fun | latest progress of "spacecraft program" activities

Redis入门完整教程:客户端案例分析

掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺

又一百万量子比特!以色列光量子初创公司完成1500万美元融资
![[socket] ① overview of socket technology](/img/91/dccbf27a17418ea632c343551bccc0.png)
[socket] ① overview of socket technology

你知道电子招标最突出的5大好处有哪些吗?
![[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]](/img/f0/79e7915ba3ef32aa21c4a1d5f486bd.jpg)
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]

Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!

PSINS中19维组合导航模块sinsgps详解(时间同步部分)
随机推荐
Redis入门完整教程:客户端案例分析
杰理之发射端在接收端关机之后假死机【篇】
A complete tutorial for getting started with redis: AOF persistence
Unity custom webgl packaging template
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
Unity webgl adaptive web page size
Data analysis from the perspective of control theory
Redis入门完整教程:问题定位与优化
How to find file accessed / created just feed minutes ago
LeetCode 77:组合
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
Redis introduction complete tutorial: client case analysis
“零售为王”下的家电产业:什么是行业共识?
C language exercises_ one
oracle连接池长时间不使用连接失效问题
Leetcode 77: combination
Redis getting started complete tutorial: replication topology
惯导标定国内外研究现状小结(删减版)
Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform