Swift 练习

前端之家收集整理的这篇文章主要介绍了Swift 练习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation println("Hello,World!") let floatValue:Float = -1.3 let intValue = 1234 + Int(floatValue) NSLog("intValue = %d",intValue); typealias MyInt = Int16 println("MyInt的最小值是:\(MyInt.max)") let numOfCourse:MyInt = 200 println("课程数量:\(numOfCourse)"); let b : Bool = false,b1 : Bool = true println("b is :\(b) -- b1 is \(b1)") //元组 var classInfo = (className:"ios1309",classId:"20150302",classNum:50) println("班级名称 is \(classInfo.className),班级编号 is \(classInfo.classId),班级人数 is \(classInfo.classNum)") var (name,id,num) = classInfo println("班级 is \(name),编号 is\(id),人数 is \(num)") let (className,_,_) = classInfo println("班级名称 is \(className)") //可选类型 var str : String? = "123" if str != nil { println(str) println(str!.toInt()) } else { println(str) } let str1 : String = "Hello",str2 : String = " World" let str3 = str1 + str2 println(str3) var age = 10 if age < 0 { println("年龄不合法") } //三目运算符 println(age < 0 ? "年龄不合法":"年龄值合法\(age)") //区间运算符 - 闭区间 for a in 0...5 { println(a) } println("---------------") //- 半闭区间 for a in 0 ..< 5 { println("\(a)") } var sex = "male" if age < 18 && sex == "male" { println("boy") } let distance = 1 assert(distance >= 0,"两点的距离不能小于0") var str5 = "ios,@%^&&*(你好gniogier" println("字符的数量:\(countElements(str5))") var str6 : String = "12.1" let ns: NSString = str6 let d = ns.doubleValue println(d) var intList = [12,35,56] var intList2 : [Int] intList2 = [] intList2.append(1245) println(intList2) var doubleList:[Double] = [] doubleList.append(151) println(doubleList) var list = [Int]() list.append(13) var list1 = [Int](count: 5,repeatedValue: 4) for n in list1 { println(n) } var list2 = Array(count: 3,repeatedValue:"hello") for b in list2 { println(b) } var list3 = ["hello","swift","学习","欢迎"] println("第一个元素 is :\(list3[0])") println("第二个元素 is :\(list3[1])") println("元素的个数 is :\(list3.count)") var list4 : [String] = [] if list4.count == 0 { println("数组是空的") } if list4.isEmpty { println("数组果然是空的") } list3[3] = "iOS开发" println(list3) list3[0...2] = ["曹操","刘备","孙权"] println(list3) var dic1 = [1:1,2:2,3:3] println(dic1.count) var dic2 :Dictionary<String,String> = [:] var dic3 = Dictionary<String,String>() var dic4 = [String:String]() var dic = ["key1":"iOS","key2":"Swift","key3":"Android"] var dicStr = dic["key2"] println(dicStr) var strNotExists = dic["key"] println(strNotExists) dic["key"] = "Objective-C" println(dic) //修改字典 dic["key"] = "OC" println(dic) let dicStr1 = dic.updateValue("Objective-c",forKey: "key2") println(dic) for (key,value) in dic { println("key:\(key)","value:\(value)") } for str7 in dic.keys { println(str7 + "") } var keys = Array(dic.keys) println(keys) for str8 in dic.values { println(str8 + "") } var values = Array(dic.values) println(values) /* * 控制语句和函数 */ var age1 = 10 if age1 > 18 { println("他已经是成年人") } else if age1 > 12 { println("少年") } else if age1 > 3 { println("童年") } else { println("幼年") } //switch var mode = "飞行" switch mode { case "户外": println("现在为户外模式,音量最大") case "飞行": println("现在为飞行模式,电话无法接通") case "静音": println("现在为静音模式,没有声音") default: println("其他") } var age3 = 3 switch age3{ case 0...3: println("幼年") case 3...12: println("童年") case 12...17: println("少年") case 18...200: println("成年") default: println("该年龄值不合法") } //元组 用小括号 let boy = (10,"boy") switch boy { case (0...20,"boy"): println("男孩") case (21...100,"man"): println("男人") default: println("其他") } let woman = (10,"gril") switch woman { case (let age,"boy"): println("男孩:\(age)") case (let age,"man"): println("男人:\(age)") case (let age,let sex): println("年龄:\(age),性别:\(sex)") } let boy2 = (50,"man") switch boy2 { case let (age,sex) where age < 12 && sex != "boy" && sex != "man": println("女孩:\(age)岁") case let (age,sex) where age > 12 && sex == "man": println("男人:\(age)") case let (age,sex): println("\(sex):\(age)") } //循环结构 var sum = 0 for var i = 1; i <= 100; i++ { sum += i; } println("sum : \(sum)") //for in var sum1 = 0 for i in 1...100 { sum1 += i } println(sum1) //遍历数组 var strArray = ["Swift","Objective","Java"] for str9 in strArray { print(str9 + " ") } //while var sum3 = 0 var i = 0 while i <= 100{ sum3 += i i++ } println("sum3 = \(sum3)") //do-while var sum4 = 0 var j = 0 do{ sum4 += j j++ }while j <= 100 println("sum4 = \(sum4)") var strArray1 = ["Hello","Swift","iOS","Java"] //for str in strArray1{ // if str.hasPrefix("S") // { // continue // } // print(str + " ") //} for str in strArray1 { if str.hasPrefix("S") { break } println(str + " ") } //fallthrough let boy3 = (1,"boy") switch boy3 { case (0...20,"boy"): println("男孩") fallthrough case (2...100,"man"): println("男人") fallthrough default: println("其他") } //函数 func address(a:Int,b:Int) -> Int { return a + b } println("address = \(address(12,36))") var sum5 = address(34,40) println(sum5) var num1 = 12,num2 = 34,num3 = 40 var sum6 = address(num1,address(num2,num3)) println("sum6 = \(sum6)") //无参函数 func showWarnint() { println("警告") } showWarnint() //带参函数 func showWarning(str:String) { println("警告:" + str) } showWarning("内存不足") //参数标签 func showStudentInfo(sName name:String,sAge age:Int,sHeigh heigh:Int,sPhoneNum phoneNum:Int) { println("学生:\(name) 年龄:\(age) 身高:\(heigh) 电话:\(phoneNum)") } showStudentInfo(sName: "张三",sAge: 23,sHeigh: 176,sPhoneNum: 13723561245) func showStudentInfo(#name:String,#age:Int,#heigh:Int,#phoneNum:Int) { println("学生:\(name) 年龄:\(age) 身高:\(heigh) 电话:\(phoneNum)") } showStudentInfo(name: "李四",age: 28,heigh: 180,phoneNum: 13526453212) //最大公约数 func greatestCommonDivisor(var a:Int,var b:Int) -> Int { var localC = a % b while localC != 0 { a = b b = localC localC = a % b } return b } println(greatestCommonDivisor(72,68)) func leaseCommonMultiple(a:Int,b:Int) -> Int { return a * b / greatestCommonDivisor(a,b) } func getResOf(#a:Int,#b:Int,funcAction:(Int,Int) -> Int) -> Int { return funcAction(a,b) } println(getResOf(a:12,b:16,leaseCommonMultiple)) //函数的嵌套 func someCalcAction(#a:Int,#type:Int) -> Int { func greatestCommonDivisor(var a:Int,var b:Int) -> Int { var c = a % b while c != 0 { a = b b = c c = a % b } return b } func leaseCommonMultiple(a:Int,b:Int) -> Int { return a * b / greatestCommonDivisor(a,b) } //根据type的值返回相应的结果,0表示最大公约数,1表示最小公倍数 switch type { case 0: return greatestCommonDivisor(a,b) case 1: return leaseCommonMultiple(a,b) default: -1 } return -1 } println(someCalcAction(a:12,type:0)) println(someCalcAction(a:12,type:1)) println(someCalcAction(a:12,type:2)) //枚举 enum toward : Int { case forward = 10 case back case left = 30 case right = 40 } enum Toward:Int { case Forward,Back,Left,Right } let goto = toward.back println(goto.rawValue) //指定枚举为String类型 enum Toward1: String { case forward = "f",Back = "b",Left = "l",Right = "r" } println(Toward1.forward.rawValue) println(Toward1.Back.hashValue) //例如:按钮的枚举 enum TouchEvent { case TouchUpinSide case TouchDown case TouchDoubleClick } //定义一个事件,并指定其类型为TouchEvent var event:TouchEvent //定义event的事件类型为单击 event = TouchEvent.TouchUpinSide //处理事件 switch event { case .TouchUpinSide: println("按钮被点击啦") case .TouchDown: println("按钮被按下") case .TouchDoubleClick: println("按钮被双击") default: println("其他") } //结构体 struct Student { var stuId:Int; var stuName:String var stuSex:Bool var stuPhone:String } struct student { var stuid = 0 var stuname = "" var stusex = true var stuphone = "" } //空的构造方法 var zhang = student() println(zhang) var xiaoLi = Student(stuId: 2014050902,stuName: "李雷",stuSex: true,stuPhone: "1380000000") println(xiaoLi.stuId) var Perry = student() Perry.stuname = "涩涩的小丑" println(Perry.stuname) //结构体嵌套 struct Teacher{ var tId = 0 var tName = "" } struct ClassRoom { var classId = 0 var className = "" var classCharge = Teacher() //班主任 } struct Student2 { var stuId = 0 var stuName = "" var stuSex = true var stuPhone = "" var classInfo = ClassRoom() } var zhao = Student2() zhao.stuName = "赵敏" zhao.classInfo.className = "iOS开发" zhao.classInfo.classCharge.tName = "张三丰" println("学生:\(zhao.stuName) 所在班级:\(zhao.classInfo.className) 班主任是:\(zhao.classInfo.classCharge.tName)") 原文链接:https://www.f2er.com/swift/327308.html

猜你在找的Swift相关文章