Swift 4 学习

更新时间: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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,251评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,511评论 0 17
  • 曾经有一份美好的爱情放在我的面前我没有珍惜。等到失去后才后悔莫及。如果可以再对小李说。毛欣想说。这辈子无缘再牵手。...
    毛欣与小李阅读 2,661评论 0 13
  • “你知道吗,现在这个世界已经不适合我们了,因为我们太怀旧了。”——《三峡好人》 小商店在半山腰上。斑驳的树影,撒在...
    时坤_昵称已被占用阅读 260评论 0 0
  • 他是大家公认的暖男。从他的身上总能感受到爱,感受到贴心,感受到温暖。上课也是表现非常棒的孩子,英语...
    EchoHe阅读 389评论 0 1