Swift4 3 陣列 字典 示例

2021-08-28 23:57:08 字數 3067 閱讀 9984

1、乙個陣列只能儲存特定型別的資料;

2、所儲存的資料不一定是乙個類的物件,可以是基礎資料型別;

var array = ["a","b","c"] //["a", "b", "c"]

var array2:[string] = ["a","b","c"] //["a", "b", "c"]

var array3:array= ["a","b","c"] //["a", "b", "c"]

array[0] = "aa" //"aa"

array //["aa", "b", "c"]

var array4 = [int]() //

var array5 = array() //

var array6:[int] = //

array = [string]() //清空陣列

array

array6 = [int](repeatelement(0, count: 10)) //[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

var array7 = [2,3,4]

var array8 = array6 + array7 //兩個陣列合併 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4]

var array = ["a","b","c"]

array.count //3

array.isempty //false

array += ["d","e"] //["a", "b", "c", "#", "d", "e"]

array.insert("hello", at: 0) //在陣列位置上插入 ["hello", "a", "b", "c", "#", "d", "e"]

array.remove(at: 0) //"hello" 刪除的元素

array.removelast() //"e" 刪除最後乙個元素

array[0] = "aa"

array //["aa", "b", "c", "#", "d"]

array[2...4] = ["cc","dd","ee"] //批量修改,陣列賦給陣列

array //["aa", "b", "cc", "dd", "ee"]

//遍歷陣列-1

for index in 0..1、字典儲存的資料是鍵和值的資料對

2、所儲存的資料中,鍵和值可以是任意資料型別

3、乙個字典只能儲存固定一種鍵和值的資料型別搭配

//隱式宣告

var dict = [1:"a",2:"b",3:"c"]

var site = ["search":"google","web":"mooc"]

//顯式宣告

var dict2:dictionary= [1:"a",2:"b",3:"c"]

var site2:dictionary= ["search":"google","web":"mooc"]

var dict3 = dictionary() //宣告乙個空字典

dict = dictionary() //清空字典

dict = [:] //清空字典

var dict = [1:"a",2:"b",3:"c"]

var site = ["search":"google","web":"mooc"]

dict.count //返回資料對數

dict.isempty //是否空

//訪問鍵值

dict[1] //"a"

site["search"] //"google"

dict[4] //nil

dict[0] = "0" //"0"

"seo:"+site["search"]! //"seo:google"

site.updatevalue("imaginecode", forkey: "web")

site //["web": "imaginecode", "search": "google"]

//遍歷key,value

for (key,value) in site

//遍歷key

for key in site.keys

//遍歷val

for val in site.values

array(site.keys) //強制型別轉換 ["web", "search"]

1、列表

示例: uikit

import uikit

let colors = [

"blue":(red:93,green:138,blue:168),

"sweet":(red:254,green:111,blue:94),

"yellow":(red:255,green:239,blue:0),

"orange":(red:255,green:140,blue:0),

"violet":(red:143,green:0,blue:255),

"fern":(red:113,green:188,blue:120),

"gamboge":(red:228,green:155,blue:15),

"cerise":(red:244,green:0,blue:161),

"icterine":(red:252,green:247,blue:94),

"jam":(red:165,green:11,blue:94)

]//var backview = uiview(frame: cgrect(x:0, y:0, width: 320, height:colors.count * 50)

var backview = uiview(frame:cgrect(x: 0.0, y: 0.0, width: 320.0, height: cgfloat(colors.count * 50)) )

backview.backgroundcolor = uicolor.black

var index = 0

for (colorname,rgbtuple) in colors

swift,陣列和字典

建立陣列和字典使用符號 訪問元素通過索引或鍵。例如 陣列 var colors red blue green 想要答案為紅色的,可以這樣獲取到 var color cllors 0 字典 var people name jack age 20,height 70 想要獲取人的屬性,可以這樣獲取 va...

swift 15 0519 陣列和字典

陣列和字典的區別 陣列有序,字典無序但可以通過key來查詢 陣列的兩種定義 陣列 型別 型別 陣列迭代 for 站名 in 0.山手線.count for 車站名 in 山手線 enumerate 把陣列拆解為元祖變數 for 序號,車站名 in enumerate 山手線 陣列的建立和初始化 建立...

swift集合型別 陣列和字典

swift 語言提供了陣列和字典兩種集合型別來儲存集合資料。陣列用來按順序儲存相同型別的資料。字典則是以無序的方式來儲存鍵值對 key,value 陣列和字典都是強型別的,也就意味著,陣列和字典在宣告時必須指明型別。本篇部落格我們來分別討論swift中的陣列和字典 陣列就是以乙個從頭到尾的順序儲存相...