美文网首页
Swift 4 学习

Swift 4 学习

作者: marlonxlj | 来源:发表于2017-08-16 17:25 被阅读73次

更新时间:2017-8-16

一、基础知识笔记

/**
 * Swift 4.0 学习
 */

import UIKit

var a = ""

var b = " "

a.isEmpty
b.isEmpty

b.count

var ruo: Character = "其"
let words = "中国人有人人械"
for heword in words {
    print("字: ",heword)
}

let a = "便是"
let b = "人间"
let c = "好时节"

var d = a + b + c

let smil = "小明"
d.append(smil)

print("\" \\ \n a \'")

多行字符串
let code = """
<html>
<head>js test</head>
<script>
function changeParaText() {

document.getElementById('p1').innerHTML = "更新后的文字"

}
</script>
<body>
<p id="p1">这是一个swift多行字符串</p>

<button type="button" onclick="changeParaText()">点击按钮更新文字</button>
</body>

</html>
"""

let name = "李白"
let type = "G"
let num = 168
let desc = "长安"
let deTi = "洛阳"
let price = 380

let msg = """
尊敬\(name)的乘客,您已订购\(type)\(num)\(desc)到\(deTi)的往返票,需要支付\(380 * 2)元。
"""
let a = """
海客谈瀛洲
烟涛微茫信难求
越人语天姥
云霞论坛系统好
世博五月延迟成
天台一万八千张
对此欲倒东南倾
"""

索引
a.startIndex
a.endIndex
a.count

a[a.startIndex]

a[a.index(after: a.startIndex)]

a[a.index(before: a.endIndex)]

a[a.index(a.startIndex, offsetBy: 3)]
a[a.index(a.endIndex, offsetBy: -3)]

for index in a.indices {
    print(a[index])
}

for b in a {
    print(b)
}

插入和删除
var a = "世界尽快头"

a.insert(",", at: a.endIndex)

let b = "难分离李克勤春哥?"

a.insert(contentsOf: b, at: a.endIndex)

//删除 remove
//a.remove(at: a.index(before: a.endIndex))

//删除一个范围
let str = a.index(a.endIndex, offsetBy: -(b.count))

let bRange = str..<a.endIndex
a.removeSubrange(bRange)

let a = "等额本息"
let b = "等额本金!"

let c = "等额"
let d = "!"
//前缀
a.hasPrefix(c)
b.hasPrefix(c)
//后缀
a.hasSuffix(d)
b.hasSuffix(d)

//集合类
/**
 * 1.有序可重复的集合: 数组
 * 2.无序不重复的集合: Set
 * 3.无序可重复,但有键值:Dic
 * 4.批量处理集合中的元素,可以使用for in循环
 */

//创建数组

let array = Array(repeating: 1, count: 50)

let intRangeArray = Array(-2...2)

var topLange = ["java", "c", "C++", "Php"]

topLange.count

topLange.isEmpty

//添加元素
topLange.append("OC")

let h5 = ["html","css", "js"]

topLange += h5

//获取元素索引
topLange[7]

//插入
topLange.insert("Swift", at: 4)

//移除
topLange.remove(at: 8)

//高阶操作:求和,连接元素,reduce
//高阶操作: 变形map



//set : 如身份证号、护照

var cardNo:Set = [1,2,3]

var citys: Set = ["shanghai", "beijing","chengdu", "shanghai"]

citys.count

//插入
citys.insert("ShenZhen")

//移除
citys.remove("shanghai")

//是否包含某元素:contains
citys.contains("beijing")

//转换为数组:sorted
let citysArray = citys.sorted()

//集合音的运算:交差并补

var x: Set = [1,2,3,4]
var y: Set = [4,5,6,7,]
//交集
x.intersection(y)

//补集
x.symmetricDifference(y)

//差集
x.subtract(y)

//并集
x.union(y)

//集合间的关系

//相等
let h: Set = [1,2,3]
let i: Set = [3,2,1,4]

h == i

//子集
h.isSubset(of: i)
h.isStrictSubset(of: i)
//父集
i.isSuperset(of: h)
i.isStrictSubset(of: h)

//无交集
let j: Set = ["game","player"]
let k: Set = ["sport", "walking"]

j.isDisjoint(with: k)

//字典
var b:[Int: String]
var airports = ["PVG":"东城国际", "CHU": "成都大酒店","DUB":"大理"]
//计算
airports["SHQ"] = "虹桥机场"
airports["CHU"] = "大边周水"

//获取
airports["SHQ"]

//移除
airports["DUB"] = nil

//循环用元组
//for (key, value) in airports {
//    print(key, value)
//}
for value in airports.keys {
    print(value)
}

for key in airports.values {
    print(key)
}

let codes = [String](airports.keys)
let name = [String](airports.values)

相关文章

网友评论

      本文标题:Swift 4 学习

      本文链接:https://www.haomeiwen.com/subject/yozirxtx.html