//变量声明var num = 22;var m:Int = 10;num = 15;常量的声明
let count = 50;let wIDth:Double = 12.0;//count = 13;//count为常量 不能被改变字符串声明
let label = "The wIDth is";var label1:String = "你好";变量类型转换
swift语言中类型没有隐式转换,所有类型只能强制转换
let wIDthLabel = label + String(count);var d:Double = wIDth + Double(count);在字符串中打印值
println(label + "\(count)");println("this is \(count)");println("this is \(m)");//m为Double类型println("this is \(m + n)");//m和n都为Double类型数组声明与使用
var shopPing = ["water","paper","tulips"];//定义一个数组println(shopPing[0]);var emptyArry = [String]();//声明一个空的String数组shopPing = [];//如果类型可以被推断的话可以这样声明一个空数组字典的声明是使用
var occupation = [ "hello" : "world","good" : "morning"]var str = occupation["good"];var emptyDic = [String:float]();//声明一个空的字典var dic = [:];//类型知道的话可以这样声明一个空字典控制语句 for语句
//for控制语句for(var i = 0; i < shopPing.count; i++) { println(shopPing.count); println(shopPing[i]);}
//新型for循环for i in 0..<3 { forstLoop += i;}相等于for var i=0; i<3; i++ { forstLoop += i;}switch语句
switch语句不需要加break语句,语句执行以后会自动跳出。
//switchlet str1 = "hello";switch str1 {case "hello": println("hello world");case "good": println("good morning");default://default语句必须有,不然会报错 println("error");}使用for in便利字典和数组
let intNumbers = [ "one" : [2,3,4,5,5],"two" : [1,6,8],"three" : [6,89,3]]var largest = 0;for(kind,numbers) in intNumbers {//number前不用加var for number in numbers { if(number > largest) { largest = number; } }}println("the largerst number is \(largest)");带有?号的变量声明
var optionString:String? = "hello";//表示optionString可以为nil,可以是确定值println(optionString == nil);var optionname:String? = "John Appleseed";var greeting = "Hello";if let name = optionname {//使用if let处理值缺失的情况 greeting = "Hello,\(name)";//"Hello,John Appleseed"}函数 返回一个参数
//后面的String是返回值类型func greet(name:String,day:String) -> String { return "Hello \(name),Today is \(day)";}println(greet("ttf","frIDay"));返回多个参数(返回元组)
//参数是一个数组,返回值为最小值,最大值,总和func calculte(scores:[Int]) ->(min: Int,max: Int,sum: Int) { var min = scores[0]; var max = scores[0]; var sum = scores[0]; for score in scores { if score < min { min = score; }else if score > max { max = score; } sum += score; } return (min,max,sum);}let scores = [1,2,7];calculte(scores);可变参数的函数
//有不确定个参数func sumOf(numbers: Int...) -> Int { var sum = 0; for number in numbers { sum += number; } return sum;}sumOf(1,3);函数里面可以嵌套函数
//函数里面嵌套一个函数func returnFifteen() -> Int { var y = 10; func add() { y += 5; } add(); return y;}returnFifteen();函数返回一个函数
函数是swift里面的一级成员,最大,所以他也可以返回一个函数
//返回值是一个函数func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { return 1 + number; } return addOne;}var increment = makeIncrementer();increment(2);//给这个函数传递参数函数作为参数传递
func hasAnyMatches(List: [Int],condition: Int -> Bool) -> Bool { for item in List { if condition(item) { return true; } } return false;}//要传递的函数func lessthanTen(number: Int) -> Bool { return number < 10;}var numbers = [20,10,56];hasAnyMatches(numbers,lessthanTen);函数是特殊的闭包?
//让每一个元素都乘以3numbers.map({ (number: Int) -> Int in let result = 3 * number; return result;})
如果参数类型都已经知道,下面这种方法等价
//让每一个元素都乘以3,并打印let mappednumber = numbers.map({number in 3 * number});println(mappednumber);
//从大到小排列数组let sortednumbers = sorted(numbers) { > class };println(sortednumbers);类的声明与使用 声明一个类
Shape var { 0 numberOfSIDe = (); func simpleDescriptionreturn -> String { "this is \(numberOfSIDe)" var; }}类对象创建与使用
//创建类对象 shape = Shape();2shape.numberOfSIDe = //给类的成员变量赋值;var//调用类的成员函数 shapeDes = shape.simpleDescription();var类的构造方法
= shape . Shape();shape=numberOfSIDe 2 var;= shapeDes . shapevarsimpleDescription();class nameShape { = numberOfSIDes: Int 0 var; String name: String; init(name: //构造方法) {self .=name -> name; } func simpleDescription() Stringreturn { "this is \(numberOfSIDes)" var; }}= nameShape "ttf" nameShape(name://方法传递参数时,前面要加标记);.nameShapevarsimpleDescription();类的继承
class Square: nameShape { String sIDeLength: Double; init(sIDeLength: Double,name: self) { .=sIDeLength . sIDeLength; super//不能写在前面,要保证本类中先定义的先初始化init(name: name);self .=numberOfSIDes 4 -> ; } func area() returnDouble { self .*sIDeLength self .-> sIDeLength; } overrIDe func simpleDescription() Stringreturn { "square sIDes of length is \(self.sIDeLength)" var; }}= square 10.0 Square(sIDeLength: "ttf",name: .);square.area();squareclasssimpleDescription();类方法传参的时候需要显示说明
类中的方法和一般的函数不同,类中的方法传递参数的时候需要有标记,显示说明(第一个可以省略)
Counter Int { var count: amout = 0; func increment(Int: times,Int: Counter) { count += amout * times; } } var counter = ()times; counter.increment(2,enum: 4);//必须要带标记,显示声明
枚举和结构体 枚举的声明,里面也可以有方法 case Suit { let one,two,tree}var number = Suit.one;结构体创建
结构体和类很想同,结构体是传值,类是传引用
协议和扩展 协议声明protocol EcampleProtocol { get simpleDescri: String{extension Int: ExampleProtocol { mutating func adjust() { self +}; mutating func adjust();}
类只要继承协议就可以实现里面的方法了
扩展 42; } }=
总结 以上是内存溢出为你收集整理的【swift总结】swift语言总览全部内容,希望文章能够帮你解决【swift总结】swift语言总览所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)