Swift 结构体使用
Swift将OC中的大部分类都变成了结构体,比如Sring,比如Array,比如Dictionary。
Swift中不再存在OC中的NSMutableArray、NSMutableString,NSMutableDictionary
在Swift中,只通过参数名前面是let还是var,let就是不可变,var就是可变。
let chars:[Character] = ["h","e","l","l","o"]
let hello = String(chars)
print(hello)
let helloWorld = "hello" + " world"
print(helloWorld)
let name = "Yvan"
let sex:String = "man"
print(name,sex)
var hei = "hello"
hei.append(",world")
print(hei)
print(hei.count)
print(hello.startIndex)
print(hello[hello.index(hello.endIndex, offsetBy: -1)])
print(hei[hei.index(hei.startIndex, offsetBy: 3)...hei.index(hei.endIndex, offsetBy: -1)])
hei.insert(contentsOf: "!", at: hei.endIndex)
hei.insert(contentsOf: "wuwa ", at: hei.startIndex)
print(hei)
hei.remove(at: hei.index(hei.endIndex, offsetBy: -1))
print(hei)
if hei == hello {
print("两字符串相等")
}
if hei.hasPrefix("hel") {
print("有hel前缀")
}
if hei.hasSuffix("lo") {
print("有ol后缀")
}
if hei.isEmpty {
print("字符串为空")
}
print("----------------------------------------------------------")
let arr01 = ["abc","def","ghi"]
let arr02:[String] = ["123","234","345"]
let arr03:[Any] = ["lilei",25,"man",UIView()]
let arr04 = [String]()
let arr05:NSArray = ["12","13","14"]
let arr06:NSMutableArray = ["1","2","3","4"]
print(arr01,arr02,arr03,arr04,arr05,arr06)
var arr07 = ["hello","world","!"]
arr07.append("append")
arr07.insert("insert", at: 0)
print(arr07)
var arr08 = ["1","2","3","4"]
arr08.remove(at: 1)
print(arr08)
arr08.removeFirst()
arr08.removeLast()
arr08.removeAll()
print(arr08)
var arr09 = ["1","2","3","4"]
arr09[0] = "5"
arr09[1...2] = ["6","7"]
print(arr09)
let arr10 = ["1","2","3","4"]
for str in arr10 {
print(str)
}
for (index,value) in arr10.enumerated() {
print("Index: \(index),Valuie: \(value)")
}
print("----------------------------------------------------------")
let dict01:[String:Int] = ["A":90,"B":80,"C":70,"D":60]
let dict02 = [String:String]()
let dict03 = ["a":"A","b":"B","c":"C"]
print(dict01,dict02,dict03)
var dict04 = ["name":"lucy","sex":"female"]
dict04["age"] = "19"
dict04["name"] = "limei"
print(dict04)
var dict05 = ["name":"Lucy","sex":"female","age":"18"]
dict05.removeValue(forKey: "sex")
print(dict05)
dict05.remove(at: dict05.index(forKey: "name")!)
print(dict05)
dict05.removeAll()
let set01:Set = [1,2,3,4,5,6]
print(set01)
var set02:Set = [1,2,3,4,5,6,7]
set02.insert(8)
set02.remove(2)
print(set02)
for i in set02.sorted() {
print(i)
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-31032.html