标签:
好了,废话不多说了,直接把我对swift的语法的一些理解奉献给大家,希望能对学习swift语法的朋友有所帮助,如有不足之处,还请多多包涵,如果有错误之处,欢迎指正
Chris Lattner
开始着手 Swift 编程语言的设计工作克里斯·拉特纳
何许人?1 OC: #import <UIKit/UIKit.h> 2 Swift: import UIKit
1 OC: NSLog(@"Hello world”); / NSLog(@"%d", a); 2 //Swift中字符串不需要@ 3 print(a) / print("hello world")
1 let view : UIView = UIView() 2 view.alpha = 0.5 3 view.backgroundColor = UIColor.orangeColor()
1 let n = 3.14 2 let View = UIView()
1 let m = 20 2 let n = 3.44 3 // let result = m + n 错误写法
1 let a : Double = 2.44 2 let b : CGFloat = 4.55 3 let result1 = a + Double(b)
1 int a = 20 2 if (a > 0) { 3 NSLog(@"a大于0") 4 } 5 6 if (a) { 7 NSLog(@"a不等于0") 8 }
1 let a = 20 2 if a > 0 { 3 print("a大于0") 4 } else { 5 print("a不大于0") 6 } 7
let result = m > n ? m : n
1 func online(age : Int , IDCard : Bool , money : Int) { 2 guard age >= 18 else { 3 print("回家叫家长") 4 return 5 } 6 guard IDCard == true else { 7 print("回家拿身份证") 8 return 9 } 10 guard money >= 5 else { 11 print("回家拿钱") 12 return 13 } 14 print("留下来上网") 15 } 16 online(19, IDCard: true, money: 4)
1 switch sex { 2 case 0, 1: 3 print("正常") 4 default: 5 print("非正常人") 6 }
5.3 swift支持多种数据类型判断
1 //浮点型switch判断 2 switch m { 3 case 3.14: 4 print("m是π") 5 default: 6 print("m非π") 7 } 8 9 //字符串switch判断 10 switch opration { 11 case "+": 12 result = a + b 13 case "-": 14 result = a - b 15 case "*": 16 result = a * b 17 case "/": 18 result = a / b 19 default: 20 print("非法操作") 21 } 22
1 let score = 92 2 3 switch score { 4 case 0..<60: 5 print("不及格") 6 case 60..<80: 7 print("及格") 8 case 80..<90: 9 print("良好") 10 case 90...100: 11 print("优秀") 12 default: 13 print("不合理分数") 14 } 15
1 for (int i = 0; i < 10; i++) { 2 3 }
1 // 区间遍历 0..<10 0...9 2 for i in 0..<10 { 3 print(i) 4 } 5 6 for i in 0...9 { 7 print(i) 8 } 9 10 // 如果一个标识符不需要使用, 那么可以通过 _ 来代替 11 for _ in 0..<10 { 12 print("hello world") 13 }
int a = 20 while (a) { }
1 var i = 10 2 while i > 0 { 3 print(i) 4 i -= 1 5 }
1 // 区别: 不再使用do while --> repeat while 2 var m = 0 3 repeat { 4 print(m) 5 m += 1 6 } while m < 10
let str = "hello swift"
var strM = "hello world" strM = "hello china"
let length = str.characters.count
for c in str.characters { print(c) }
let str1 = "Hello" let str2 = "World" let str3 = str1 + str2
let name = "lgp" let age = 18 let height = 1.98 let infoStr = "my name is \(name), age is \(age), height is \(height)"
let min = 3 let second = 4 let timeStr = String(format: "%02d:%02d", arguments: [min, second])
1 // 1.方式一: 将String类型转成NSString类型, 再进行截取 2 // (urlString as NSString) --> NSString 3 let header = (urlString as NSString).substringToIndex(3) 4 let footer = (urlString as NSString).substringFromIndex(10) 5 let range = NSMakeRange(4, 5) 6 let middle = (urlString as NSString).substringWithRange(range)
1 // 2.方式二: Swift原生方式进行截取 2 let headerIndex = urlString.startIndex.advancedBy(3) 3 let header1 = urlString.substringToIndex(headerIndex) 4 5 let footerIndex = urlString.endIndex.advancedBy(-3) 6 let footer1 = urlString.substringFromIndex(footerIndex) 7 8 let range1 = headerIndex.advancedBy(1)..<footerIndex.advancedBy(-1) 9 let middle1 = urlString.substringWithRange(range1)
let array = ["why", "yz"]
//基本写法 var arrayM = Array<String>() //单写法 var arrayM = [String]()
arrayM.append("ljp")
let removeItem = arrayM.removeAtIndex(1) 返回值为删除的元素 arrayM.removeAll()
arrayM[0] = "why"
let item = arrayM[0]
1 for i in 0..<array.count { 2 print(array[i]) 3 }
for name in array { print(name) }
1 for (index, name) in array.enumerate() { 2 print(index) 3 print(name) 4 }
1 let array1 = ["why", "yz"] 2 let array2 = ["lmj", "lnj"] 3 let resultArray = array1 + array2
let dict = ["name" : "why", "age" : 18, "height" : 1.88]
var dictM = Dictionary<String, NSObject>()
var dictM = [String : NSObject]() // 常见
dictM.updateValue("why", forKey: "name")
1 dictM.removeValueForKey("age")
1 //如果原有没有对应的key/value, 那么就添加键值对 2 // 如果原有已经有对应的key/value, 那么直接修改 3 dictM.updateValue("1.77", forKey: "height") 4 dictM["name"] = "why"
1 let item = dictM["name"]
1 for key in dict.keys { 2 print(key) 3 }
1 for value in dict.values { 2 print(value) 3 }
1 for (key, value) in dict { 2 print(key) 3 print(value) 4 }
1 let dict1 = ["name" : "why", "age" : 18] 2 var dict2 = ["height" : 1.88, "phoneNum" : "+86 110"] 3 4 for (key, value) in dict1 { 5 dict2[key] = value 6 } 7
1 let infoTuple = ("why", 18, 1.88, "+86 110") 2 // 使用元组描述一个人的信息 3 ("1001", "张三", 30, 90) 4 // 给元素加上元素名称,之后可以通过元素名称访问元素 5 (id:"1001", name:"张三", english_score:30, chinese_score:90)
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5779969.html