Swift入门(一)

Swift入门(一),第1张

概述常量、变量 声明常量:let 声明变量:var eg. let conNum = 1; var num = 2; 基本数据类型 Int、 Float、Double、Bool、tuples、optional 其中 tuples 元祖,可以包含不同类型值的集合; optional 可选值,表示一个值可以为空(nil),如果一个变量没有声明为optional,那么必须给它一个确切的值; 类型转换 需要指

常量、变量
声明常量:let
声明变量:var
eg. let conNum = 1; var num = 2;

基本数据类型
Int、 float、Double、Bool、tuples、optional
其中
tuples 元祖,可以包含不同类型值的集合;
optional 可选值,表示一个值可以为空(nil),如果一个变量没有声明为optional,那么必须给它一个确切的值;

类型转换
需要指定转换类型:Sometype(val)

运算符
与其他语言有相似的规则
特殊规则

可以对float求余,8 % 2.5 // 等于 0.5; 空值运算符 a ?? b,对可选类型a进行空判断,如果a包含一个值就进行解封,否则就返回一个默认值b,表达式a必须是Optional类型,默认值b的类型必须要和a存储值的类型保持一致 区间运算符 闭区间运算符(a...b); 半开区间(a..<b

字符串
Swift 的String类型是值类型。 如果您创建了一个新的字符串,那么当其进行常量、变量赋值 *** 作或在函数/方法中传递时,会进行值拷贝。 任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值 *** 作。

数组

var shopPingList: [String] = ["Eggs","Milk"]shopPingList.append("Flour")shopPingList += ["Chocolate Spread","Cheese","Butter"]shopPingList[0] = "Six eggs"shopPingList.insert("Maple Syrup",atIndex: 0)let mapleSyrup = shopPingList.removeAtIndex(0)for item in shopPingList {    println(item)}
字典
var airports: [String:String] = ["TYO": "Tokyo","dub": "dublin"]for (airportCode,airportname) in airports {    println("\(airportCode): \(airportname)")}for airportCode in airports.keys {    println("Airport code: \(airportCode)")}// Airport code: TYO// Airport code: LHRfor airportname in airports.values {    println("Airport name: \(airportname)")}// Airport name: Tokyo// Airport name: London Heathrowlet airportCodes = Array(airports.keys)// airportCodes is ["TYO","LHR"]let airportnames = Array(airports.values)// airportnames is ["Tokyo","London Heathrow"]

数组和字典都是在单个集合中存储可变值。如果我们创建一个数组或者字典并且把它分配成一个变量,这个集合将会是可变的。这意味着我们可以在创建之后添加更多或移除已存在的数据项来改变这个集合的大小。与此相反,如果我们把数组或字典分配成常量,那么它就是不可变的,它的大小不能被改变。

for 循环
let names = ["Anna","Alex","Brian","Jack"]for name in names {    println("Hello,\(name)!")}let numberOfLegs = ["spIDer": 8,"ant": 6,"cat": 4]for (animalname,legCount) in numberOfLegs {    println("\(animalname)s have \(legCount) legs")}for var index = 0; index < 3; ++index {    println("index is \(index)")}
while、do while
while condition {    statements}do {    statements} while condition
if
var temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 {    println("It's very cold. ConsIDer wearing a scarf.")} else if temperatureInFahrenheit >= 86 {    println("It's really warm. Don't forget to wear sunscreen.")} else {    println("It's not that cold. Wear a t-shirt.")}
switch
let someCharacter: Character = "e"switch someCharacter {case "a","e","i","o","u":    println("\(someCharacter) is a vowel")case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":    println("\(someCharacter) is a consonant")default:    println("\(someCharacter) is not a vowel or a consonant")}

这里default语句是必须的,否则编译不通过;不需要break,因为只会匹配一种情况。

case 中还可以加条件判断where

let yetAnotherPoint = (1,-1)switch yetAnotherPoint {case let (x,y) where x == y:    println("(\(x),\(y)) is on the line x == y")case let (x,y) where x == -y:    println("(\(x),\(y)) is on the line x == -y")case let (x,y):    println("(\(x),\(y)) is just some arbitrary point")}// 输出 "(1,-1) is on the line x == -y"
continue、break、fallthrough
continue、break与其他语言用法相同;
fallthrough用在switch语句中,实现条件贯穿,相当于Java或者其他语言case块中没有写break的情况:
let integerToDescribe = 5var description = "The number \(integerToDescribe) is"switch integerToDescribe {case 2, 3, 5, 7, 11, 13, 17, 19:    description += " a prime number,and also"    fallthroughdefault:    description += " an integer."}println(description)// 输出 "The number 5 is a prime number,and also an integer."
总结

以上是内存溢出为你收集整理的Swift入门(一)全部内容,希望文章能够帮你解决Swift入门(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存