Swift -- 第一章

常量和变量

常量和变量把一个名字(比如maximumNumberOfLoginAttempts或者welcomeMessage)和一个指定类型的

值(比如数字10或者字符串"Hello")关联起来。常量的值一旦设定就不能改变,而变量的值可以随意更

1.声明一个 常量 

let a = "Hello World"

如果想明确这个常量的类型

( 在swift 里每句结束后不是必须使用“;” 分号 ,  有一种情况下必须要用分号,即你打算在同一行内写多条独立的语句 ) 

let  a : string =  "Hello World"        ;    let myVariable  :float_t  =  42

1.声明一个 变量

var a = "Hello World"

a = "Haha"

打印信息

print( a )    //  print( \( a ) )

//        初始化空字符串

let stringA = ""

let stringB = String()

if stringA.isEmpty && stringB.isEmpty{

print("他俩是空的,Nothing to see here")

}

//字符串的拼接    直接用" + "

let a = "The width is "

let b = 94

let widthLabel = a + String(b) // 转换成string 类型

print(widthLabel)

创建一个空数组

var emptyArray = [String]()

向数组中插入一个数据   append

emptyArray.append(a)

print(emptyArray)

//遍历数组   和判断  

遍历一般用  for in      判断则不用if(){}   直接 用if  {}  

let value = [22,11,77,33,44,55]

for scoree in value{

if scoree > 30{

print("aaaaaaa")

}else{

print("bbbbbbb")

}

if scoree > 20 {

let name = "Hello World"

print(name)

}

}

//两个数组相加

let oneArray = ["1","2","3"]

let  twoArray  = ["4","5","6"]

var threeArray = oneArray + twoArray

//  遍历数组  然后根据下表删除

for (index,value) in threeArray.enumerated() {

if value == "3" {

threeArray.remove(at: index)

}

}

print(threeArray)



Tuples 元组

元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。

let http404Error = (404, "Not Found")

// http404Error 的类型是 (Int, String),值是 (404, "Not Found")

你可以把任意顺序的类型组合成一个元组,这个元组可以包含所有类型。只要你想,你可以创建一个类型为( Int , Int ,Int)或者 (string, Bool) 或者其他任何你想要的组合的元组。

你可以将一个元组的内容分解(decompose)成单独的常量和变量,然后你就可以正常使用它们了:

let (statusCode, statusMessage) = http404Errorprint("The status code is \(statusCode)")

// 输出 "The status code is 404"

print("The status message is \(statusMessage)")// 输出 "The status message is Not Found"

如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记:

let (justTheStatusCode, _) = http404Error

print("The status code is \(justTheStatusCode)")

// 输出 "The status code is 404"

此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:

print("The status code is \(http404Error.0)")

// 输出 "The status code is 404"

print("The status message is \(http404Error.1)")// 输出 "The status message is Not Found"

你可以在定义元组的时候给单个元素命名:

let http200Status = (statusCode: 200, description: "OK")

给元组中的元素命名后,你可以通过名字来获取这些元素的值:

print("The status code is \(http200Status.statusCode)")

// 输出 "The status code is 200"

print("The status message is \(http200Status.description)")// 输出 "The status message is OK"

作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个(Int, String)元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用


可选类型

基本就是 返回值的时候如果不确定 是 不是空 或者返回来的是不是 一个确定的类型 就用一个? 

var surveyAnswer: String?       // surveyAnswer 被自动设置为 nil


if  判断语句 基本语法跟Objective-C  一样   在|| 的语句中  如果想强调某个条件是首先判断的话    

let a = true

let b = true

let c = true

if a || b || (c){

print("I don't want  say something。。。")

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,768评论 19 139
  • 关于 Swift 重要这个文档所包含的准备信息, 是关于开发的 API 和技术的。这个信息可能会改变, 根据这个文...
    无沣阅读 9,917评论 1 27
  • 基础部分(The Basics) 当推断浮点数的类型时,Swift 总是会选择Double而不是Float。 结合...
    gamper阅读 5,211评论 0 7
  • 我背叛了朋友……是谁在雨中从背后后袭击我?为什么我醒来时在一个巷子的垃圾箱旁,一只手被锁铐在垃圾箱的铁横栏...
    天下第一帅s阅读 2,905评论 1 0
  • 玉卿嫂是不同于祥林嫂的,比祥林嫂更隐忍,但也更残忍。 任何一部作品,人物和情节都会带有特定时代的色彩。反映同一个时...
    林易子阅读 3,085评论 0 1