func onLine2(age : Int) -> Bool { guard age >= 18 else{ print("不能上网") return false } print("可以上网") return true } //调用函数 onLine2(19)
let direction = 0 switch direction { case 0: print("north") case 1: print("south") case 2: print("east") default : print("west") }
switch direction { case 0, 1: print("north--east") default: print("south--west") }
let r = 1.23 switch r { case 1.23: print("lian") default: print("jie") }
let m = 20 let n = 10 let opration = "/" var result : Int switch opration { case "+": result = m + n case "-": result = m - n case "*": result = m * n case "/": result = m / n default: print("非法操作符") }
let scores = 35 switch scores { case 0..<60: print("不及格") case 60..<70: print("及格") case 70..<90: print("良好") case 90...100: print("优秀") default: print("成绩错误") }
for var i = 0; i < 10; i++ { print(i) }
for i in 0..<10 { print(i) } for i in 0...10 { print(i) }
for _ in 0..<10 { print("MJHee") }
var w = 10 while w < 20 { w++ print("MJHee") }
repeat { w++ print("YHee") } while w < 30
var str = "MJHee" for s in str.characters { print(s) }
let str1 = "I Love" var str2 = " RiZhao" str2 = str1 + str2
let iage = 18 let iheight = 1.89 let str3 = "age is \(iage), height is \(iheight)"
let min = 2 let second = 3 let time = "0\(min) : 0\(second)" let itime = String(format: "%02d : %02d", arguments: [min, second])
let urlString = "www.MJBaby.com" let prefixString = (urlString as NSString).substringToIndex(3) //www let middleString = (urlString as NSString).substringWithRange(NSRange(location: 4, length: 6)) //MJBaby let subfixString = (urlString as NSString).substringFromIndex(11) //com
let names : Array<String> = ["Jack", "Lucy", "Sara"]
let nameArr : [String] = ["MJHee", "YHee"]
let nameArray = ["MJHee", "hmj", "HMJ"]
var names1 : [String] = ["hmj", "lxx"] names1.append("HYH")
var names2 : [String] = [String]() names2.append("HMJ")
names3.append("MJHee") names3.append("HeeYH") names3.append(12) names3.append(true)
names3.removeLast() names3.removeAtIndex(0)
names3[1] = "MJBaby"
names3[1]
for i in 0..<names3.count{ print(names3[i]) }
for item innames3{ print(item) }
for i in 0..<2{ print(names3[i]) } for item innames3[0..<2]{ print(item) }
let array1 = ["hmj", 23, "+86 12344555"] let array2 = [1.70, "China"] let array3 = array1 + array2 //不同类型的数组合并 let name1 = ["hyh", "mj"] let ages1 = [12, 23, 18] var array4 = [AnyObject]() for item in name1{ array4.append(item) } for item in ages1{ array4.append(item) }
let dict1 : Dictionary<String, AnyObject> = ["name" : "hmj", "age" : 23]
let dict2 : [String : AnyObject] = ["name" : "hmj", "age" : 23]
let dict3 = ["name" : "hmj", "age" : 23]
var dictA1 = Dictionary<String, AnyObject>()
var dictA2 = [String : AnyObject]()
var dictA3 = ["name" : "hmj", "age" : 23]
dictA["name"] = "hmj" dictA["age"] = 23 dictA["height"] = 1.70 dictA["like"] = 13
//dictA.removeAll() dictA.removeValueForKey("like")
dictA["age"] = 18
dictA["age"]
for key in dictA.keys{ print(key) }
for value indictA.values{ print(value) }
for (key, value) indictA{ print(key) print(value) }
var dict1A = ["name" : "hmj", "age" : 23] let dict2A = ["phone" : "+86 1234556"] for (key ,value) in dict2A{ dict1A[key] = value }
let tem1 = ("hmj", 23, 1.70)
let tem2 = (name : "hmj", age : 23, height : 1.70)
let (name, age, height) = ("hmj", 23, 1.70);
let error = (404, "Not Found") error.0 error.1
let error1 = (errorCode : 404, errorInfo : "Not Found") error1.errorCode error1.errorInfo
let(errorCode1, errorInfo1) = (404, "Not Found") errorCode1 errorInfo1
optional<String> class Person { //定义属性 var name : Optional<String> } var avaName : Optional<String> //给可选类型赋值avaName = "HMJ"//打印可选类型:Optional("HMJ") print(avaName) //取出可选类型中的值 //取值: 可选类型 + !,强制解包print(avaName!) //拼接let info = "My name is " + avaName!
let phoneNum : String? phoneNum = "+86 123556"
let phoneInfo = "My phoneNum is " + phoneNum!
if phoneNum != nil { let phoneInfo = "My phoneNum is " + phoneNum! }
if let tempPhoneNum = phoneNum { print(phoneNum)//显示: Optional("+86 123556") print(tempPhoneNum)//显示: +86 123556 }
let urlstring = "http://www.baidu.com" //如果写类型那么必须写成可选类型 let url : NSURL? = NSURL(string: urlstring) //如果有中文就会报错 NSURLRequest(URL: url!) //进行判断 if url != nil { NSURLRequest(URL: url!) } //可选绑定 if let tempUrl = url { NSURLRequest(URL: tempUrl) }
func about1() -> Void{ print("手机型号是iPhone6s Plus玫瑰金") } about1() 注意: 如果一个函数没有返回值,那么:(-> Void)可省略 func about2() { print("手机型号是iPhone6s Plus玫瑰金") }
func readMessage() -> String { return"吃饭了吗?" }
func sum(num1 : Int, num2 : Int) -> Int { return num1 + num2 } sum(20, num2: 30)
func callPhone(phoneNum : String) -> Void { print("打电话给: " + phoneNum) } callPhone("+86 13355")
func sum(num1 : Int, num2 : Int, num3 : Int) -> Int { return num1 + num2 + num3 } sum(1, num2: 0, num3: 3)
func sum(num1 num1 : Int, num2 : Int, num3 : Int) -> Int { return num1 + num2 + num3 } sum(num1: 1, num2: 2, num3: 3)
func sum(num1 num1 : Int, _ num2 : Int, num3 : Int) -> Int { return num1 + num2 + num3 } sum(num1: 1, 3, num3: 3)
func makeCoffee(coffeeName : String = "拿铁") -> String { return"已经制作好一杯\(coffeeName)" } makeCoffee("卡布奇诺") makeCoffee()
func sumNum(nums : Int...) -> Int { var result = 0 for num in nums { result += num } return result } sumNum(19, 20, 1, 3, 8, 15)
var aa = 10 var bb = 20 //默认情况下,形参是let类型func swapNum(var num1 : Int, var num2 : Int) { let temp = num1 num1 = num2 num2 = temp } //默认是值传递swapNum(aa, num2: bb) aa//10 bb//20 //地址传递: 需要加关键字inout func swapNum(inout num1 : Int, inout num2 : Int) { let temp = num1 num1 = num2 num2 = temp } swapNum(&aa, num2: &bb) aa//20 bb//10
func test() {
func demo() {
func operation() {
}
}
}
class Student : NSObject { //类的属性 //**存储属性: //注意: //1>如果类型是结构体或者类,通常定义为可选类型 //2>如果类型是基本数据类型,可以在定义时直接初始化为0或者0.0 var name : String? var age : Int = 0 var chineseScore : Double = 0.0 var mathScore :Double = 0.0 //**计算属性: 并不存储实际的值; 必须提供get方法 var averageScore : Double { get { return (chineseScore + mathScore) * 0.5 } //可选的setter方法,并且在setter方法中有一个系统的标示符,用于记录外界传入的值 //newValue: 用于记录外界传入的值 set { print(newValue) //没有意义 self.averageScore = newValue } } //类属性 static var courseCount : Int = 0 } //创建对象 //<__lldb_expr_37.Student: 0x7fb771f210b0> //命名空间:__lldb_expr_282 //存储地址:0x7fff1b65d700 let stu = Student() //设置或获取其中的值时,必须通过类名调用 Student.courseCount = 2 stu.name = "hyh" stu.age = 10 stu.chineseScore = 98 stu.mathScore = 100 print(stu.averageScore) //stu.averageScore = 100 print(stu.averageScore)
class Person : NSObject { //Swift中提供了属性监听器,可以监听属性的改变 var name : String? { //即将改变 //在willSet方法中,系统提供一个标示符 //newValue: 用于记录新传入的数据 //自定义newValue的名称 //willSet(new) willSet { print(name)//nil print(newValue)//Optional("hmj") //重命名后调用时 print(new) } //已经改变 //在didSet方法中,系统提供一个标示符 //oldValue: 用户记录之前的值 //自定义oldValue的名称 //didSet(old) didSet { print(name)//Optional("hmj") print(oldValue)//nil //重命名后调用时 print(old) } } var age : Int = 0 } let p = Person() p.name = "hmj"p.age = 18 //OC监听属性的改变: 重写set方法
原文:http://www.cnblogs.com/HMJ-29/p/5003354.html