AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

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

Swift 數組、字典和集合

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

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

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

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

Arrays

數組是一種用于存儲相同類型元素的有序集合,是 Swift 中常用的數據結構之一。讓我們通過比較 Swift 和 TypeScript 中的示例,了解如何使用數組以及它們的基本操作。ydS28資訊網——每日最新資訊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 中,您可以通過下標訪問和修改數組元素。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊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 屬性。ydS28資訊網——每日最新資訊28at.com

Swift Code

let count = fruits.count

TypeScript Code

const count = fruits.length;

遍歷數組元素

在 Swift 中,可以使用 for-in 遍歷數組。而在 TypeScript 中,可以使用 for-of 循環。ydS28資訊網——每日最新資訊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" */

字典

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

創建一個字典

在 Swift 中,字典使用 [Key: Value] 的語法來聲明,其中 Key 是鍵的數據類型,Value 是值的數據類型。ydS28資訊網——每日最新資訊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 中,你可以通過鍵訪問和修改字典元素。ydS28資訊網——每日最新資訊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 中,使用下標賦值的方式添加鍵值對。ydS28資訊網——每日最新資訊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 操作符刪除鍵值對。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊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 循環遍歷字典。ydS28資訊網——每日最新資訊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 類型表示。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊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 方法遍歷集合。ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊28at.com

ydS28資訊網——每日最新資訊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 方法獲取兩個集合的并集。ydS28資訊網——每日最新資訊28at.com

ydS28資訊網——每日最新資訊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 方法。ydS28資訊網——每日最新資訊28at.com

ydS28資訊網——每日最新資訊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 的相關特性。ydS28資訊網——每日最新資訊28at.com

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

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

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

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

標簽:
  • 熱門焦點
  • 盧偉冰長文解析K60至尊版 對Redmi有著里程碑式的意義

    在今天的Redmi后性能時代戰略發布會結束之后,Redmi總經理盧偉冰又帶來了一篇長文,詳解了為什么 Redmi 要開啟后性能時代?為什么選擇和 MediaTek、Pixelworks 深度合作?以及后性
  • 影音體驗是真的強 簡單聊聊iQOO Pad

    大公司的好處就是產品線豐富,非常細分化的東西也能給你做出來,例如早先我們看到了新的vivo Pad2,之后我們又在iQOO Neo8 Pro的發布會上看到了iQOO的首款平板產品iQOO Pad。雖
  • 7月安卓手機性能榜:紅魔8S Pro再奪榜首

    7月份的手機市場風平浪靜,除了紅魔和努比亞帶來了兩款搭載驍龍8Gen2領先版處理器的新機之外,別的也想不到有什么新品了,這也正常,通常6月7月都是手機廠商修整的時間,進入8月份之
  • 掘力計劃第 20 期:Flutter 混合開發的混亂之治

    在掘力計劃系列活動第20場,《Flutter 開發實戰詳解》作者,掘金優秀作者,Github GSY 系列目負責人戀貓的小郭分享了Flutter 混合開發的混亂之治。Flutter 基于自研的 Skia 引擎
  • K8S | Service服務發現

    一、背景在微服務架構中,這里以開發環境「Dev」為基礎來描述,在K8S集群中通常會開放:路由網關、注冊中心、配置中心等相關服務,可以被集群外部訪問;圖片對于測試「Tes」環境或者
  • 在線圖片編輯器,支持PSD解析、AI摳圖等

    自從我上次分享一個人開發仿造稿定設計的圖片編輯器到現在,不知不覺已過去一年時間了,期間我經歷了裁員失業、面試找工作碰壁,寒冬下一直沒有很好地履行計劃.....這些就放在日
  • 2023年,我眼中的字節跳動

    此時此刻(2023年7月),字節跳動從未上市,也從未公布過任何官方的上市計劃;但是這并不妨礙它成為中國最受關注的互聯網公司之一。從2016-17年的抖音強勢崛起,到2018年的&ldquo;頭騰
  • 聯想小新Pad Pro 12.6將要推出,搭載高通驍龍 870 處理器

    聯想小新Pad Pro 12.6將于秋季新品會上推出,官方按照慣例直接在發布會前給出了機型的所有參數。聯想小新 Pad Pro 12.6 將搭載高通驍龍 870 處理器,重量為 5
  • 三翼鳥智能家居亮相電博會,讓用戶體驗更真實

    2021電博會在青島國際會展中心開幕中,三翼鳥直接把“家”搬到了現場,成為了展會的一大看點。這也是三翼鳥繼9月9日發布了行業首個一站式定制智慧家平臺后的
Top