swift 之 数组集合字典的简单使用
2016-06-26 11:04
507 查看
//: Playground - noun: a place where people can play
import UIKit
//有序集合
var str = "Hello, playground"
var array1: Array<Int> =
Array<Int>()
var array2: [Int] =
array1
var array3 = array2
array1.count
if array1.isEmpty{
print("kong");
}
var fiveInts = [1,2,3,4,5]
fiveInts.count
fiveInts.append(6)
fiveInts += [7,8,9]
fiveInts.insert(0, atIndex:
0)
fiveInts.endIndex.predecessor()
fiveInts.startIndex.successor()
fiveInts[0]
fiveInts[0...1]
fiveInts[1...2]
fiveInts[0..<2]
fiveInts[0...1] = [0]
fiveInts
fiveInts.removeAtIndex(0)
fiveInts
for intvalue in
fiveInts{
print(intvalue)
}
fiveInts.enumerate()
for(index,value) in
fiveInts.enumerate(){
print("index--\(index),value---\(value)")
}
fiveInts.reverse()
//无序集合 set
let number = 10;
number.hashValue
let pi = 3.14
pi.hashValue
let string = "swift"
string.hashValue
var emptySet = Set<Character>()
emptySet = ["a","e","i","o","u"]
var evenSet:Set = [2,4,6,8,10]
emptySet.isEmpty
emptySet.count
evenSet.insert(13)
evenSet.remove(13)
evenSet.contains(10)
for number in
evenSet.sort(){
print(number)
}
//union join about set
var setA:Set = [1,2,3,4,5,6]
var setB:Set = [4,5,6,7,8,9]
//交集
let interSectAB: Set =
setA.intersect(setB)//{5, 6, 4}
//补集
let exclusiveAB: Set =
setA.exclusiveOr(setB)//{9, 7, 2, 3, 1, 8}
let unionAB: Set =
setA.union(setB)//{2, 4, 9, 5, 6, 7, 3, 1, 8}
let astructB : Set =
setA.subtract(setB)//{2, 3, 1}
var setC: Set = [1,2,3]
if setA ==
setC{
}else{
print("not equal")
}
setA.isSupersetOf(setC)
//true
setC.isSubsetOf(setA)
//true
setA.isStrictSupersetOf(setC)
//true
setC.isStrictSubsetOf(setA)
//true
setC.isStrictSubsetOf(setB)
//false
setB.isDisjointWith(setC)
//true
//dictionary
var int2String = [Int :String]()
int2String[10] =
"TEN"
int2String[20] =
"TWENTY"
int2String//[20: "TWENTY", 10: "TEN"]
int2String = [:]
var capitalNumber = [
1:"one",
2:"two",
3:"three",
4:"four",
5:"five",
6:"six",
7:"seven",
8:"eight",
9:"nine",
10:"ten"
]
capitalNumber[1]//"one"
capitalNumber[10]
capitalNumber.updateValue("一", forKey:
1)//"one"
capitalNumber
capitalNumber.count
var opiontalNine =
capitalNumber.removeValueForKey(9)//"nine"
opiontalNine.dynamicType
for(key ,value ) in
capitalNumber{
print("\(key):
\(value)")
}
for number in
capitalNumber.keys{
print(number)
}
capitalNumber.keys//LazyMapCollection<Dictionary<Int, String>, Int>
let keyArray = [Int](capitalNumber.keys)//[10,
2, 4, 5, 6, 7, 3, 1, 8]
let valueArray = [String](capitalNumber.values)//["ten",
"two", "four", "five", "six", "seven", "three"
import UIKit
//有序集合
var str = "Hello, playground"
var array1: Array<Int> =
Array<Int>()
var array2: [Int] =
array1
var array3 = array2
array1.count
if array1.isEmpty{
print("kong");
}
var fiveInts = [1,2,3,4,5]
fiveInts.count
fiveInts.append(6)
fiveInts += [7,8,9]
fiveInts.insert(0, atIndex:
0)
fiveInts.endIndex.predecessor()
fiveInts.startIndex.successor()
fiveInts[0]
fiveInts[0...1]
fiveInts[1...2]
fiveInts[0..<2]
fiveInts[0...1] = [0]
fiveInts
fiveInts.removeAtIndex(0)
fiveInts
for intvalue in
fiveInts{
print(intvalue)
}
fiveInts.enumerate()
for(index,value) in
fiveInts.enumerate(){
print("index--\(index),value---\(value)")
}
fiveInts.reverse()
//无序集合 set
let number = 10;
number.hashValue
let pi = 3.14
pi.hashValue
let string = "swift"
string.hashValue
var emptySet = Set<Character>()
emptySet = ["a","e","i","o","u"]
var evenSet:Set = [2,4,6,8,10]
emptySet.isEmpty
emptySet.count
evenSet.insert(13)
evenSet.remove(13)
evenSet.contains(10)
for number in
evenSet.sort(){
print(number)
}
//union join about set
var setA:Set = [1,2,3,4,5,6]
var setB:Set = [4,5,6,7,8,9]
//交集
let interSectAB: Set =
setA.intersect(setB)//{5, 6, 4}
//补集
let exclusiveAB: Set =
setA.exclusiveOr(setB)//{9, 7, 2, 3, 1, 8}
let unionAB: Set =
setA.union(setB)//{2, 4, 9, 5, 6, 7, 3, 1, 8}
let astructB : Set =
setA.subtract(setB)//{2, 3, 1}
var setC: Set = [1,2,3]
if setA ==
setC{
}else{
print("not equal")
}
setA.isSupersetOf(setC)
//true
setC.isSubsetOf(setA)
//true
setA.isStrictSupersetOf(setC)
//true
setC.isStrictSubsetOf(setA)
//true
setC.isStrictSubsetOf(setB)
//false
setB.isDisjointWith(setC)
//true
//dictionary
var int2String = [Int :String]()
int2String[10] =
"TEN"
int2String[20] =
"TWENTY"
int2String//[20: "TWENTY", 10: "TEN"]
int2String = [:]
var capitalNumber = [
1:"one",
2:"two",
3:"three",
4:"four",
5:"five",
6:"six",
7:"seven",
8:"eight",
9:"nine",
10:"ten"
]
capitalNumber[1]//"one"
capitalNumber[10]
capitalNumber.updateValue("一", forKey:
1)//"one"
capitalNumber
capitalNumber.count
var opiontalNine =
capitalNumber.removeValueForKey(9)//"nine"
opiontalNine.dynamicType
for(key ,value ) in
capitalNumber{
print("\(key):
\(value)")
}
for number in
capitalNumber.keys{
print(number)
}
capitalNumber.keys//LazyMapCollection<Dictionary<Int, String>, Int>
let keyArray = [Int](capitalNumber.keys)//[10,
2, 4, 5, 6, 7, 3, 1, 8]
let valueArray = [String](capitalNumber.values)//["ten",
"two", "four", "five", "six", "seven", "three"
相关文章推荐
- Swift入门基础语法
- swift: 方法的重载/重写/KVC构造
- Swift 3.0 (二)
- SwiftDate 浅析
- swift学习(1)----tableVIew
- Swift - 选择框(UIPickerView)的用法
- Swift的NSLog和print的区别
- 使用Carthage安装第三方Swift库
- Swift - 警告提示框(UIAlertController)的用法
- Swift - 图像控件(UIImageView)的用法
- Swift - 分段选择控件(UISegmentedControl)的用法
- Swift 3.0(一)
- swift-5类型别名和布尔值
- Swift - 开关按钮(UISwitch)的用法
- Swift - 多行文本输入框(UITextView)的用法
- swift:闭包的解析
- Swift-传值
- Swift滑动手势
- Swift中设置tableview的分割线(separator)的样式、颜色、边距