加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift官文阅读记录一(基础)

发布时间:2020-12-14 06:58:03 所属栏目:百科 来源:网络整理
导读:题记:难得最近闲下来一段时间,就想着把项目用Swift做一遍,以前也学过Swift,但是仗着自己有OC的基础就没怎么用心学,发现很多基础的东西都不了解,用Swift撸代码时脑子里还是OC的那一套思想,我想这是不对的,于是就从头开始看官文,不求最快掌握,只求把

题记:难得最近闲下来一段时间,就想着把项目用Swift做一遍,以前也学过Swift,但是仗着自己有OC的基础就没怎么用心学,发现很多基础的东西都不了解,用Swift撸代码时脑子里还是OC的那一套思想,我想这是不对的,于是就从头开始看官文,不求最快掌握,只求把基础打的扎实一些。这将是一个系列,阅读官文的过程我都会做一些记录,写下来的东西会扎实一些。

——

文中所有的代码是直接Copy官文上的。
这里附上官文地址:Swift The Basics

一、常量和变量
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

常量一旦定义不能更改值 let
定以后使用过程中还需要变化的就用变量 var

一次性定义一堆变量并赋予初始值

var x = 0.0,y = 0.0,z = 0.0

如果不赋予初始值则需要指定值类型

var welcomeMessage: String
var red,green,blue: Double

常量以及变量的名称几乎可以包含任何字符

let π = 3.14159
let 你好 = "你好世界"
let ???? = "dog cow"

打印常量和变量

在swift中打印常量和变量就不像在OC中那么麻烦了
可以直接传入变量

print(friendlyWelcome)

如果需要取值则用 (friendlyWelcome)转换即可

print("The current value of friendlyWelcome is (friendlyWelcome)")
二、几种注释的方式
// this is a comment

/* this is also a comment,but written over multiple lines */

/* this is the start of the first multiline comment
/* this is the second,nested multiline comment */
 this is the end of the first multiline comment */
三、数值

整数的边界值

let minValue = UInt8.min  // minValue is equal to 0,and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255,and is of type UInt8

Int && Uint

Swift会根据环境来指定适当的类型
32位环境下Int 大小为Int32
64位环境下Int 大小为Int64

32位环境下UInt 大小为UInt32
64位环境下UInt 大小为UInt64

同理浮点型被分为了 Float 和 Double

四、类型安全和类型推断

Swift是一种类型安全的语言,它需要你知道你需要什么类型的值。类型安全能帮你在最早的时候发现和解决一些基础且难以察觉的错误。

为了避免每次定义变量都要声明类型,Swift引入了类型推断,在编译的时候帮你推断出变量的类型,例如

let meaningOfLife = 42  //推断为Int类型
let pi = 3.14159  //推断为Double类型(Swift 会推断为Double而不是Float)

强制类型转换

SomeType(ofInitialValue)

let integerPi = Int(pi)

类型别名

为已存在的类型定义别名 用关键字 typealias,定义好的别名在任何地方都可以用。

typealias AudioSample = UInt16  
var maxAmplitudeFound = AudioSample.min
五、元组

把几个多样的值组合成一个混合值,组合中的值可以为任意多种类型

let http404Error = (404,"Not Found")
//http404Error为(Int,String)类型的元组,值为(404,"Not Found")

可以分解元组中的值

let (statusCode,statusMessage) = http404Error
print("The status code is (statusCode)")
// Prints "The status code is 404"
print("The status message is (statusMessage)")
// Prints "The status message is Not Found"

如果你只需要元组中的一部分值,那么分解元组的时候可以用“_”来忽略相应的部分

let (justTheStatusCode,_) = http404Error
print("The status code is (justTheStatusCode)")
// Prints "The status code is 404"

当然,也可以通过Index来取元组中的值

print("The status code is (http404Error.0)")
// Prints "The status code is 404"
print("The status message is (http404Error.1)")
// Prints "The status message is Not Found"

也可以在定义的时候单独命名元素,然后以名称取值。

let http200Status = (statusCode: 200,description: "OK")
print("The status code is (http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is (http200Status.description)")
// Prints "The status message is OK"

个人思考:类似Java中的枚举。OC中的枚举的值只能数字,而这里通过元组的这种定义,可以做枚举,而且枚举的值可以为任意类型。
在使用时可以用来定义一些提示信息。

元组适用于简单的数据,如果是复杂的数据结构,建议使用Class 或者 Struct

六、可选值 Optionals

可选值不存在与C或者OC,最接近它的应该是OC中的nil 。比如OC中的一个方法,本来要返回一个object,但最后却返回了nil,对于这个方法的返回值来说就是一个可选值。
可选值仅用在object上,不支持 结构体、基本的C类型数据、枚举。

例如

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?",or "optional Int"

convertedNumber 就是一个可选值,因为并不能确保一定能转化成功,如果possibleNumber = “abc” 那么convertedNumber 肯定也就不存在

只有可选值能置为nil。

可选值的运用

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation mark

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // no need for an exclamation mark

当把一个可选值赋值给非可选值时,要确保这个可选值必须有值(用‘!’确保)。
如果你确保有值的这个可选值是个空值,那么会造成在运行时崩溃。

if assumedString != nil {
    print(assumedString)
}

if let definiteString = assumedString {
    print(definiteString)
}

可以把确保有值的可选值当做一个正常的可选值来看待

七、捕获异常
func canThrowAnError() throws {
    // this function may or may not throw an error
}

//调用方法
do {
    try canThrowAnError()
    // no error was thrown
} catch {
    // an error was thrown
}
写给自己,也写给有需要的童鞋,如有意见或建议欢迎指出和探讨 —— LC.West

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读