学习重学Swift5-基本数据类型
Posted weicy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习重学Swift5-基本数据类型相关的知识,希望对你有一定的参考价值。
一、基本数据类型
1.声明常量和变量
let maxNum = 10
var currentNum = 0
let x = 10, y = 20, z = 30
let x1 = 10, y2 = 20, z = "30"
var welcomeMessage: String = "hello world"
print(maxNum)
print("x = \\(x), y = \\(y)")
2.整型、浮点型
Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64
Int UInt 当前平台原生字相同的长度
Double 64位浮点数,至少15位的精度
Float 32位浮点数,至少6位的精度
3.Bool
Bool: true和false
Swift的类型安全机制会阻止你用一个非布尔量的值替换掉Bool
4.类型别名
类型别名是一个为已存在类型定义一个可选择的名字
typealias AudioSample = UInt8
let simple: AudioSample = 32
5.元祖(Tuple)
let error = (1, "没有权限")
print(error.0, error.1)
let error2 = (errorCode: 1, errorMessage: "没有权限")
print(error.errorCode, error.errorMessage)
let (errcode, errMsg) = error
print(errcode, errMsg)
let (_, errMsg) = error
print(errMsg)
6.Optional
var str: String? = nil
var str1: String? = "abc"
if str1 != nil
let count = str1!.count
print(count)
// 可选项绑定
if let actualStr = str1
let count = actualStr.count
print(count)
// 隐式展开
var str2: String! = "abc"
let count = str2.count
print(count)
// 可选链
let count = str1?.count
if count != nil
let lastIndex = count! - 1
/*
原理
Optional是标准库里的一个enum类型
Optional.none 就是nil
Optional.some 则包装了实际的值
范型属性 unsafelyUnwrapped
*/
var str3: Optional<String> = "abc"
if let actualStr = str3
let count = actualStr.cout
let count = str3.unsafelyUnwrapped.count
7.字符串
-
初始化
// 字面量 var emptyString = "" // 初始化器 var emptyStr = String() let catCharacters: [Character] = ["C","a","t"] let catStr = String(catCharacters) // 多行 let softWrappedQuotation = """The White Rabbit put on his spectacles. "Where shall O begin, plaese your Majesty" he asked. """
-
是否是空串
if emptyStr.isEmpty print("Nothing to see here")
-
常见操作
// 可变不可变使用var、let var variableString = "Horse" variableString += " and carriage" // 遍历 for character in "Dog! Cat!" print(character) // 拼接 var newVarStr = variableString + "new sreing" variableString += " and carriage" // 末尾追加,不会创建新的 variableString.append("d") // 插值 let multiplier = 3 let message = "\\(multiplier) times 2.5 is \\(Double(multiplier) * 2.5)" // 字符串索引 let greeting = "Guten tag!" greeting[greeting.startIndex] // 不能使用greeting[1]这样的 greeting[greeting.index(after: greeting.startIndex)] greeting[greeting.index(before: greeting.endIndex)] let index = greeting.index(greeting.startIndex, offsetBy: 7) greeting[index] // 插入 var welcomeMessage = "hello" welcomeMessage.insert("!", at: welcomeMessage.endIndex) welcomeMessage.insert(contentsOf: " world", at: welcomeMessage.index(before: welcomeMessage.endIndex)) // 移除 welcomeMessage.remove(at: welcomeMessage.index(before: welcomeMessage.endIndex)) let range = welcomeMessage.index(welcomeMessage.endIndex, offsetBy: -6)..<welcomeMessage.endIndex welcomeMessage.removeSubrange(range) // 子字符串 let welcome = "hello, world" let index = welcome.firstIndex(of: ",") ?? welcome.endIndex let subString = welcome[..<index] let newString = String(subString) // 比较、前后缀 let welcome1 = "hello" print(welcome == welcome1) print(welcome != welcome1) print(welcome.hasPrefix("hello")) print(welcome.hasSuffix("world"))
以上是关于学习重学Swift5-基本数据类型的主要内容,如果未能解决你的问题,请参考以下文章