日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Swift 數組、字典和集合

來源: 責編: 時間:2024-01-15 09:19:11 202觀看
導讀本文我們將介紹 Swift 中的變量、常量和數據類型。如果你尚未安裝 Xcode 和配置 Swift 開發環境,請您先閱讀這篇文章。接下來,我們啟動 Xcode,然后選擇 "File" > "New" > "Playground"。創建一個新的 Playground 并命名

Z8828資訊網——每日最新資訊28at.com

本文我們將介紹 Swift 中的變量、常量和數據類型。如果你尚未安裝 Xcode 和配置 Swift 開發環境,請您先閱讀這篇文章。Z8828資訊網——每日最新資訊28at.com

接下來,我們啟動 Xcode,然后選擇 "File" > "New" > "Playground"。創建一個新的 Playground 并命名為 "Collections"。Z8828資訊網——每日最新資訊28at.com

Arrays

數組是一種用于存儲相同類型元素的有序集合,是 Swift 中常用的數據結構之一。讓我們通過比較 Swift 和 TypeScript 中的示例,了解如何使用數組以及它們的基本操作。Z8828資訊網——每日最新資訊28at.com

創建一個數組

Swift Code

var numbers = [1, 2, 3, 4, 5]// 或者 var numbers: [Int] = [1, 2, 3, 4, 5]var fruits = ["Apple", "Banana", "Orange"]// 或者 var fruits: [String] = ["Apple", "Banana", "Orange"]

TypeScript Code

let numbers = [1, 2, 3, 4, 5];// 或者 let numbers: number[] = [1, 2, 3, 4, 5];let fruits = ["Apple", "Banana", "Orange"];// 或者 let fruits: string[] = ["Apple", "Banana", "Orange"];

訪問和修改數組元素

在 Swift 和 TypeScript 中,您可以通過下標訪問和修改數組元素。Z8828資訊網——每日最新資訊28at.com

Swift Code

let firstNumber = numbers[0]// firstNumber: 1numbers[1] = 10

TypeScript Code

const firstNumber = numbers[0];// firstNumber: 1numbers[1] = 10;

添加元素

在 Swift 中,使用 append 方法添加元素。而在 TypeScript 中,是使用 push 方法。Z8828資訊網——每日最新資訊28at.com

Swift Code

fruits.append("Grapes")// fruits: ["Apple", "Banana", "Orange", "Grapes"]

TypeScript Code

fruits.push("Grapes");// fruits: ["Apple", "Banana", "Orange", "Grapes"]

刪除元素

在 Swift 中,使用 remove(at:) 方法刪除指定位置的元素。而在 TypeScript 中,是使用 splice 方法。Z8828資訊網——每日最新資訊28at.com

Swift Code

fruits.remove(at: 1)// fruits: ["Apple", "Orange", "Grapes"]

TypeScript Code

fruits.splice(1, 1);// fruits: ["Apple", "Orange", "Grapes"]

獲取數組長度

在 Swift 中,使用 count 屬性獲取數組長度。而在 TypeScript 中,是使用 length 屬性。Z8828資訊網——每日最新資訊28at.com

Swift Code

let count = fruits.count

TypeScript Code

const count = fruits.length;

遍歷數組元素

在 Swift 中,可以使用 for-in 遍歷數組。而在 TypeScript 中,可以使用 for-of 循環。Z8828資訊網——每日最新資訊28at.com

Swift Code

for fruit in fruits {    print("I like /(fruit)s")}/**Output:I like ApplesI like OrangesI like Grapess*/

TypeScript Code

for (const fruit of fruits) {    console.log(`I like ${fruit}s`);}/**Output:"I like Apples" "I like Oranges" "I like Grapess" */

字典

字典是一種用于存儲鍵值對的集合,它允許你通過鍵來快速檢索值。Z8828資訊網——每日最新資訊28at.com

創建一個字典

在 Swift 中,字典使用 [Key: Value] 的語法來聲明,其中 Key 是鍵的數據類型,Value 是值的數據類型。Z8828資訊網——每日最新資訊28at.com

Swift Code

var studentScores = ["Alice": 95, "Bob": 87, "Charlie": 90]

TypeScript Code

let studentScores: { [key: string]: number } = { "Alice": 95, "Bob": 87, "Charlie": 90 };

訪問和修改字典元素

在 Swift 和 TypeScript 中,你可以通過鍵訪問和修改字典元素。Z8828資訊網——每日最新資訊28at.com

Swift Code

let aliceScore = studentScores["Alice"]studentScores["Bob"] = 92// studentScores: ["Alice": 95, "Bob": 92, "Charlie": 90]

TypeScript Code

const aliceScore = studentScores["Alice"];studentScores["Bob"] = 92;// studentScores: {"Alice": 95, "Bob": 92, "Charlie": 90}

添加鍵值對

在 Swift 和 TypeScript 中,使用下標賦值的方式添加鍵值對。Z8828資訊網——每日最新資訊28at.com

Swift Code

studentScores["David"] = 88// studentScores: ["Charlie": 90, "Alice": 95, "Bob": 92, "David": 88]

TypeScript Code

studentScores["David"] = 88;// studentScores: {"Charlie": 90, "Alice": 95, "Bob": 92, "David": 88}

刪除鍵值對

在 Swift 中,使用 removeValue(forKey:) 方法刪除指定鍵的鍵值對。而在 TypeScript 中,是使用 delete 操作符刪除鍵值對。Z8828資訊網——每日最新資訊28at.com

Swift Code

studentScores.removeValue(forKey: "Charlie")// studentScores: ["David": 88, "Bob": 92, "Alice": 95]

TypeScript Code

delete studentScores["Charlie"];// studentScores: {"David": 88, "Bob": 92, "Alice": 95}

獲取鍵和值的集合

在 Swift 中,通過 keys 和 values 屬性獲取字典的鍵和值的集合。而在 TypeScript 中,是使用 Object.keys 和 Object.values 方法。Z8828資訊網——每日最新資訊28at.com

Swift Code

let allKeys = Array(studentScores.keys)let allValues = Array(studentScores.values)// allKeys: ["David", "Bob", "Alice"]// allValues: [88, 92, 95]

TypeScript Code

const allKeys: string[] = Object.keys(studentScores);const allValues: number[] = Object.values(studentScores);// allKeys: ["Alice", "Bob", "David"] // allValues: [95, 92, 88]

遍歷字典

在 Swift 和 TypeScript 中,都可以通過 for-in 循環遍歷字典。Z8828資訊網——每日最新資訊28at.com

Swift Code

for (name, score) in studentScores {    print("/(name) scored /(score)")}/**Output:Alice scored 95Bob scored 92David scored 88*/

TypeScript Code

for (const name in studentScores) {    const score: number = studentScores[name];    console.log(`${name} scored ${score}`);}/**Output:"Alice scored 95" "Bob scored 92""David scored 88" */

集合

集合是一種無序、無重復元素的集合類型。在 Swift 中,集合通過 Set 類型表示。Z8828資訊網——每日最新資訊28at.com

創建一個集合

Swift Code

var numberSet: Set<Int> = [1, 2, 3, 4, 5]

TypeScript Code

let numberSet: Set<number> = new Set([1, 2, 3, 4, 5]);

添加元素

在 Swift 中,使用 insert 方法向集合中添加元素。而在 TypeScript 中,使用 add 方法。Z8828資訊網——每日最新資訊28at.com

Swift Code

numberSet.insert(6)// numberSet: [3, 4, 5, 1, 2, 6]

TypeScript Code

numberSet.add(6);// numberSet: {1, 2, 3, 4, 5, 6}

移除元素

在 Swift 中,使用 remove 方法移除集合中的元素。而在 TypeScript 中,使用 delete 方法。Z8828資訊網——每日最新資訊28at.com

Swift Code

numberSet.remove(3)// numberSet: [5, 2, 1, 4, 6]

TypeScript Code

numberSet.delete(3);// numberSet: {1, 2, 4, 5, 6}

遍歷集合

在 Swift 中,可以使用 for-in 循環遍歷集合。而在 TypeScript 中,可以通過 forEach 方法遍歷集合。Z8828資訊網——每日最新資訊28at.com

Swift Code

for number in numberSet {    print("Number is /(number)")}/**Output:Number is 2Number is 1Number is 5Number is 6Number is 4*/

TypeScript Code

numberSet.forEach((number) => {    console.log(`Number is ${number}`);});/**Output:"Number is 1""Number is 2""Number is 4""Number is 5""Number is 6"*/

集合交集

在 Swift 中,使用 intersection 方法獲取兩個集合的交集。而在 TypeScript 中,可以使用 Set 構造函數和 filter 方法。Z8828資訊網——每日最新資訊28at.com

Z8828資訊網——每日最新資訊28at.com

Swift Code

let anotherSet: Set<Int> = [4, 5, 6]let intersection = numberSet.intersection(anotherSet)// numberSet: [1, 5, 4, 2, 6]// anotherSet: [5, 4, 6]// intersection: [5, 4, 6]

TypeScript Code

const anotherSet: Set<number> = new Set([4, 5, 6]);const intersection: Set<number> = new Set([...numberSet].filter(x => anotherSet.has(x)));// numberSet: {1, 2, 4, 5, 6} // anotherSet: {4, 5, 6}// intersection: {4, 5, 6}

集合并集

在 Swift 中,使用 union 方法獲取兩個集合的并集。Z8828資訊網——每日最新資訊28at.com

Z8828資訊網——每日最新資訊28at.com

Swift Code

let union = numberSet.union(anotherSet)// numberSet: [5, 1, 4, 2, 6]// anotherSet: [4, 5, 6]// union: [5, 1, 4, 2, 6]

TypeScript Code

const union: Set<number> = new Set([...numberSet, ...anotherSet]);// numberSet: {1, 2, 4, 5, 6} // anotherSet: {4, 5, 6}// union: {1, 2, 4, 5, 6}

集合差集

在 Swift 中,使用 subtracting 方法獲取兩個集合的差集。而在 TypeScript 中,可以使用 Set 構造函數和 filter 方法。Z8828資訊網——每日最新資訊28at.com

Z8828資訊網——每日最新資訊28at.com

Swift Code

let difference = numberSet.subtracting(anotherSet)// numberSet: [6, 2, 4, 1, 5]// anotherSet: [5, 6, 4]// difference: [1, 2]

TypeScript Code

const difference: Set<number> = new Set([...numberSet].filter(x => !anotherSet.has(x)));// numberSet: {1, 2, 4, 5, 6} // anotherSet: {4, 5, 6}// difference: {1, 2}

本文我們介紹 Arrays, Dictionaries 和 Sets 三種集合類型等相關的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關特性。Z8828資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-60905-0.htmlSwift 數組、字典和集合

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 我們一起聊聊Swift 條件控制和循環

下一篇: Spring解決泛型擦除的思路不錯,現在它是我的了

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 武强县| 大竹县| 夏邑县| 同江市| 郯城县| 泰和县| 石门县| 金溪县| 汽车| 武陟县| 盐城市| 黄冈市| 平罗县| 南川市| 锡林郭勒盟| 桐城市| 安顺市| 固阳县| 广东省| 买车| 安西县| 旬阳县| 江油市| 渝中区| 德格县| 时尚| 乐东| 格尔木市| 阜平县| 依兰县| 阜阳市| 五峰| 石阡县| 安吉县| 萨迦县| 平南县| 光泽县| 定州市| 都昌县| 阿坝县| 巴彦淖尔市|