Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift官文阅读记录一(基础)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

——

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

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

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"

打印常量和变量

print(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

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

四、类型安全和类型推断

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

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

强制类型转换

let IntegerPi = Int(pi)

类型别名

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"
六、可选值 Optionals

例如

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

可选值的运用

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
}

大佬总结

以上是大佬教程为你收集整理的Swift官文阅读记录一(基础)全部内容,希望文章能够帮你解决Swift官文阅读记录一(基础)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。