Swift的基本知識 一

2021-08-01 03:11:23 字數 3393 閱讀 9087

1、常量與變數的定義

在swift中,常量的命名用let關鍵字來建立,變數的命名用關鍵字var來建立

//常量的命名

let name = "helloworld"

print("常量是:\(name)")

//變數的命名

var nickname = string()

nickname = "bird"

print("變數是:\(nickname)")

輸出:

常量是:

helloworld

變數是:

bird

2、字串串接及型別轉換

在swift中,支援「+」運算子,用它可以對兩個字串進行連線

let label = "the width is "

let width = 94

let widthlabel = label + string(width);

print(widthlabel);

輸出:the width is 94

3、陣列和字典

在swift中,陣列和字典都使用「[]」中括符,並且可以通過索引或key/value的方式儲存。

//陣列

var array = ["d","df","cs","ea","fees"]

print(array)

//利用索引對陣列進行修改

array[1] = "abc"

print(array)

輸出:

["d", "df", "cs", "ea", "fees"]

["d", "abc", "cs", "ea", 

fees」]

// 字典

var dictionary = [string:string]();

dictionary["adc"] = "adc"

dictionary["aaa"] = "aaa"

print(dictionary)

輸出:

["aaa": "aaa", "adc": 

adc」]

//陣列中的增刪改查

var arr = ["a","b","c","d","e","f"]

//增加某個元素

print(arr)

print(arr)

arr.insert("abc", at: 5)

print(arr)

arr.insert(contentsof: ["aaaaa","bbbbb","ccccc"], at: 3)

print(arr)

//刪除序號處的元素

arr.remove(at: 2)

print(arr)

//全部刪除

arr.removeall()

print(arr)

//改arr = ["abc","abd","abf","abe","abg","abh"]

print(arr)

arr[2] = "11111"

print(arr)

arr.replacesubrange(1...3, with: ["222","333","444","555"])

print(arr)

arr.replacesubrange(1...3, with: ["bmw"])

print(arr)

//陣列的長度

print(arr.count)

//遍歷陣列

for ele in arr

輸出:

["a", "b", "c", "d", "e", "f", "aa"]

["a", "b", "c", "d", "e", "f", "aa", "1", "2", "3"]

["a", "b", "c", "d", "e", "abc", "f", "aa", "1", "2", "3"]

["a", "b", "c", "aaaaa", "bbbbb", "ccccc", "d", "e", "abc", "f", "aa", "1", "2", "3"]

["a", "b", "aaaaa", "bbbbb", "ccccc", "d", "e", "abc", "f", "aa", "1", "2", "3"]

["abc", "abd", "abf", "abe", "abg", "abh"]

["abc", "abd", "11111", "abe", "abg", "abh"]

["abc", "222", "333", "444", "555", "abg", "abh"]

["abc", "bmw", "555", "abg", "abh"]

5abc

bmw555

abgabh

//swift中字典的運用

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

//新增

dict["c"] = "3"

print(dict)

dict["c"] = "333"

print(dict)

dict .removevalue(forkey: "c")

print(dict)

dict.updatevalue("aaaaa", forkey: "a")

print(dict)

let abc = array(dict.keys)

let abc1 = array(dict.values)

print("\(abc)==\(abc1)");

輸出:

["b": "2", "a": "1", "c": "3"]

["b": "2", "a": "1", "c": "333"]

["b": "2", "a": "1"]

["b": "2", "a": "aaaaa"]

["b", "a"]==["2", 

aaaaa」]

mysql的基本知識 一

u 後面更隨便使用者名稱 p 後面跟隨密碼 h 後面跟隨ip root下可以省略ip 退出伺服器 exit或quit 8 sql語言概述 結構化語言 isc國際化標準 方言 sql語法 sql語句可以在單行或多行書寫,以分號結尾 可使用空格和縮進來增強語句的可讀性 mysql不區別大小寫,但建議大寫...

linux的基本知識(一)

那麼你可以使用su bluewind命令。如果你是從root身份su的話,不用輸入password。而從其它身份則系統會提示輸入密碼的。簡單的敲入su不帶任何引數的話,預設的認為你想成為root使用者身份。使用者退出登入有兩種方法,一種是exit命令 這個命令一般的系統都會提供,一種是logout ...

基本知識總結一

一.物件導向和面向過程的區別 面向過程就是分析出解決問題所需要的步驟,然後用函式把這些步驟一步一步實現,使用的時候乙個乙個依次呼叫就可以了。物件導向是把構成問題事務分解成各個物件,建立物件的目的不是為了完成乙個步驟,而是為了描敘某個事物在整個解決問題的步驟中的行為。例如五子棋,面向過程的設計思路就是...