上一篇介绍了Swift的常量/变量和循环,详情见:json
Swift基本语法01
// 1> 定义不可变字符串 : 使用let修饰
// let str : String = "hello swift"
let str = "Hello Swift"
// str = "hello Objective-C"
// 2> 定义可变字符串 : 使用var修饰
var strM = "Hello World"
strM = "Hello China"
复制代码
##2、 字符串的使用swift
let length = str.characters.count数组
let str1 = "字符串"
let str2 = "拼接"
// OC拼接方式 NSString stringwithFormat:@"%@%@", str1, str2
let str3 = str1 + str2
复制代码
let name = "tqj"
let age = 19
let height = 1.87
let infoStr = "my nams is \(name), age is \(age), height is \(height)"
复制代码
let min = 3
let second = 4
let timeStr = String(format: "%02d:%02d", min, second)
复制代码
判断字符串是否为空bash
let str = "1"
//输出false
print(str.isEmpty)
let str = ""
//输出为true
print(str.isEmpty)
复制代码
其余判断和操做(Swift3.0特性)数据结构
//判断是否包含某字符
let str = "Hello, playground"
let is1 = str.contains("ell")
//输出true
print(is1)
//指定字符串的替换
let str2 = str.replacingOccurrences(of: "Hello", with: "HELLO")
print(str2)
//输出HELLO, playground
//转为大写
let s1 = str.localizedUppercase
//转为小写
let s2 = str.localizedLowercase
复制代码
let urlString = "www.520it.com"
// 4.1.方式一:
// 将String类型转成NSString类型,再进行截取: as NSString
let header1 = (urlString as NSString).substring(to: 3)
let range1 = NSMakeRange(4, 5)
let middle1 = (urlString as NSString).substring(with: range1)
let footer1 = (urlString as NSString).substring(from: 10)
复制代码
// 4.2.方式二:
let headerIndex = urlString.index(urlString.startIndex, offsetBy: 3)
let header2 = urlString.substring(to: headerIndex)
let startIndex = urlString.index(urlString.startIndex, offsetBy: 4)
let endIndex = urlString.index(urlString.startIndex, offsetBy: 9)
let range = Range(startIndex..<endIndex)
let middle2 = urlString.substring(with: range)
let footerIndex = urlString.index(urlString.endIndex, offsetBy: -3)
let footer2 = urlString.substring(from: footerIndex)
复制代码
// 1> 定义不可变数组
let array : [Any] = ["why", 18, 1.88]
// 2> 定义可变数组: 使用var修饰
var arrayM = [Any]()
复制代码
// 增删改查
// 2.1.添加元素
arrayM.append("why")
// 2.2.删除元素
arrayM.remove(at: 0)
// 2.3.修改元素
arrayM[0] = "yz"
// 2.4.获取元素
let item = arrayM[1]
复制代码
// 3.1.获取数组的长度
let count = array.count
// 3.2.对数组进行遍历(能够获取到下标值)
for i in 0..< count {
print(array[i])
}
// 3.3.对数组进行遍历(设置遍历区间)
for item in array {
print(item)
}
// 3.3.对数组进行遍历(不须要获取下标值)
for item in array[0..<2] {
print(item)
}
// 3.5.对数组进行遍历(既获取下标值,又获取元素)
for (index, item) in array.enumerated() {
print(index)
print(item)
}
复制代码
// 若是两个数组中存放的是相同的元素,那么在swift中能够对两个数组进行相加,直接合并
let array1 = ["why", "yz"]
let array2 = ["lmj", "lnj"]
let array3 = [12, 20, 30]
let resultArray = array1 + array2
// let result = array1 + array3 错误写法
// 不建议一个数组中存放多种类型的数据
var array3 = [2, 3, "why"]
var array4 = ["yz", 23]
array3 + array4
复制代码
// 定义一个可变字典
var dict1 : [String : Any] = [String : Any]()
// 定义一个不可变字典
let dict2 : [String : Any] = ["name" : "why", "age" : 18]
复制代码
// 2.1.添加元素
dictM["name"] = "why"
dictM["age"] = 18
dictM["height"] = 1.88
// 2.2.删除元素
dictM.removeValue(forKey: "height")
dictM
// 2.3.修改元素
dictM["name"] = "lmj"
dictM.updateValue("lnj", forKey: "name")
dictM
// 2.4.查找元素
dictM["age"]
复制代码
// 3.1.遍历字典中全部的key
for key in dict.keys {
print(key)
}
// 3.2.遍历字典中全部的value
for value in dict.values {
print(value)
}
// 3.3.遍历字典中全部的key/value
for (key, value) in dict {
print(key, value)
}
复制代码
var dict1 : [String : Any] = ["name" : "why", "age" : 18]
let dict2 : [String : Any] = ["height" : 1.88, "phoneNum" : "+86 110"]
//let resultDict = dict1 + dict2字典不能够相加合并,只能遍历
for (key, value) in dict2 {
dict1[key] = value
}
复制代码
// 3.使用元组保存信息(取出数据时,更加方便)
// 3.1.写法一:
let infoTuple0 = ("why", 18, 1.88)
let tupleName = infoTuple0.0
let tupleAge = infoTuple0.1
infoTuple0.0
infoTuple0.1
infoTuple0.2
// 3.2.写法二:
let infoTuple1 = (name : "why",age : 18, height : 1.88)
infoTuple1.age
infoTuple1.name
infoTuple1.height
// 3.3.写法三:
let (name, age, height) = ("why", 18, 1.88)
name
age
height
复制代码
// 错误写法
// let string : String = nil
// 正确写法:
// 注意:name的类型是一个可选类型,可是该可选类型中能够存放字符串.
// 写法一:定义可选类型
let name : Optional<String> = nil
// 写法二:定义可选类型,语法糖(经常使用)
let name : String? = nil
复制代码
// 演练一:给可选类型赋值
// 定义可选类型
var string : Optional<String> = nil
// 给可选类型赋值
// 错误写法:所以该可选类型中只能存放字符串
string = 123
// 正确写法:
string = "Hello world"
// 打印结果
print(string)
// 结果:Optional("Hello world")\n
// 由于打印出来的是可选类型,全部会带Optional
// 演练二:取出可选类型的值
// 取出可选类型的真实值(解包)
print(string!)
// 结果:Hello world\n
// 注意:若是可选类型为nil,强制取出其中的值(解包),会出错
string = nil
print(string!) // 报错
// 正确写法:
if string != nil {
print(string!)
}
// 简单写法:为了让在if语句中能够方便使用string
// 可选绑定
if let str = string {
print(str)
}
复制代码
// 1.将字符串类型转成Int类型
let str = "123"
let result : Int? = Int(str) // nil/Int
// 2.根据文件名称,读取路径
let path : String? = Bundle.main.path(forResource: "123.plist", ofType: nil)
// 3.根据string,建立URL
let url = URL(string: "http://www.520it.com/小码哥")
// 4.从字典中取内容
let dict : [String : Any] = ["name" : "why", "age" : 18]
dict["name"]
dict["height"]
复制代码
// 1.定义数组
let array : [AnyObject] = [12, "why", 1.88]
// 2.取出第二个元素
let objc = array[1]
// 3.将objc转成真正的类型来使用
// 3.1.as? 将AnyObject转成可选类型,经过判断可选类型是否有值,来决定是否转化成功了
let age = objc as? Int
print(age) // 结果:Optional(12)
// 3.2.as! 将AnyObject转成具体的类型,可是注意:若是不是该类型,那么程序会崩溃
let age1 = objc as! Int
print(age1) // 结果:12
复制代码
##7、try throw 代码实践app
throw catch 是 Xcode 7.0 对错误处理的一个很是大的变化性能
// 2. 反序列化
// 1.获取json文件路径
let jsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
// 2.加载json数据
let jsonData = NSData(contentsOfFile: jsonPath!)
// 3.序列化json
do{//解析成功
// throw是Xcode7最明显的一个变化, Xcode7以前都是经过传入error指针捕获异常, Xocode7开始经过try/catch捕获异常
let dictArray = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
// 遍历字典时候须要明确指明数组中的数据类型
for dict in dictArray as! [[String:String]]
{
// 因为addChildVC方法参数不能为nil, 可是字典中取出来的值多是nil, 因此须要加上!
addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
}
}catch{//解析失败
print(error)
addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
}
复制代码
let array = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)
复制代码
不过须要注意的是,一旦解析错误,程序会直接崩溃!学习