Swift 学习小结:简单值 及流程控制
发布时间:2020-12-14 02:20:50 所属栏目:百科 来源:网络整理
导读:import UIKitvar str = "Hello,playground"//简单值//let 声明常量 var 声明变量var value = 123;var i:Int = 0;var num = 0;var maVariable = 42;maVariable = 50;let myConstant = 42;let implicitInteger = 70let implicitDouble = 70.0let explicitDoubl
import UIKit var str = "Hello,playground" //简单值 //let 声明常量 var 声明变量 var value = 123; var i:Int = 0; var num = 0; var maVariable = 42; maVariable = 50; let myConstant = 42; let implicitInteger = 70 let implicitDouble = 70.0 let explicitDoublie:Double = 70 let label = "This width is" let width = 94 let widthLabel = label + String(width) let apples = 3 let oranges = 5 let applesSummary = "I have (apples) apples" let orangesSummary = "I have (oranges) oranges" var shoppingList = ["catfishi","water","tulips","blue paint"] shoppingList[1] = "bottle of water" var occupations = [ "Malcolm":"Captain","Kaylee":"Mechanic" ] occupations["Jsyne"] = "Public Relations" let emptyAraay = String[]() let emptyDictionary = Dictionary<String,Float>() shoppingList = [] //流程控制 switch ("abc") { case "123": println("123"); case "456","abc": println("123 abc") default: println("没有找到合适的匹配") } var i:Int = 0; while(i<10) { i++; println(i); } do { i--; println(i); }while (i>0); for index in 1...5 { println("index = (index)"); } let indicidualScores = [75,43,103,87,12] var teamSore = 0 for score in indicidualScores { if score > 50 { teamSore += 3; }else { teamSore += 1; } } teamSore var optionalString:String? = "Hello" optionalString == nil var optionalName:String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName{ greeting = "hello,(name)" } let vegetable = "red pepper" switch vegetable { case "celery": let vegetableCommment = "Add some raisins and make ant on a log" case "cucumber","watercress": let vegetablComment = "That would make a good tea sandwich" case let x where x.hasSuffix("pepper"): let vegetableCommment = "Is it a spicy (x)" default: let vegetableComment = "Everything tastes good in soup" } let interestingNumbers = [ "Prime":[2,3,5,7,11,13],"Fibonnacci":[1,1,2,8],"Square":[1,4,9,16,25],] var largrst = 0 for(kind,numbers) in interestingNumbers{ for number in numbers{ if number > largrst{ largrst = number } } } largrst var n = 2 while n < 100 { n = n * 2 } n var m = 2 do { m = m * 2 } while m < 100 m var firstForLoop = 0 for i in 0..3 { firstForLoop += i } firstForLoop var secondForLoop = 0 for var i = 0; i < 3; ++i { secondForLoop += i } secondForLoop (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |