/// 参考Swift3.0.1文档
/// 摘录来自: Apple Inc. “The Swift Programming Language (Swift 3.0.1)”。 iBooks. import UIKit class ViewController: UIViewController { override func viewDidLoad() {
super.viewDidLoad()
aSwiftTour() } func aSwiftTour(){
//Swift中的输出语句
print("Hello World") //MARK: - 变量
/**var声明变量
Swift具备自动推导的能力
右边是什么类型这个变量或者是常量就自动的是什么类型了
*/
var variable =
variable =
//MARK: - let声明常量
let constant =
print(variable,constant) //指定变量的类型 隐式指定
let implictInteger = //Int
let implicitDouble = 66.0 //Double //显式地指定类型为Double
let explicitDouble: Double = //Double
print(implictInteger,implicitDouble,explicitDouble) let label = "The width is"
let width =
let widthLabel = label + String(width)
print(widthLabel) //MARK: - 数字和字符串的拼接
let apples =
let oranges =
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit." //MARK: - 数组操作
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[] = "bottle of water" //MARK: - 字典操作 key:value
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations" //创建空数组和空字典的方式
//创建一个元素类型为字符串的数组
let emptyArray = [String]()
//key为String类型value为Float类型的字典
let emptyDictionary = [String: Float]() //MARK: - 数组字典更简单的方式
/**
“If type information can be inferred, you can write an empty array as [] and an empty dictionary as [:]—for example, when you set a new value for a variable or pass an argument to a function.”
摘录来自: Apple Inc. “The Swift Programming Language (Swift 3.0.1)”。 iBooks.
*/
shoppingList = []//默认是[String]
occupations = [:]//默认是[String:String] //MARK: - 控制流
//注意for if else 的格式
let individualScores = [, , , , ]
var teamScore =
for score in individualScores {
if score > {
teamScore +=
} else {
teamScore +=
}
}
print(teamScore) //MARK: - ?是可选值类型
var optionalString: String? = "Hello"
print(optionalString == nil) var optionalName: String? = "John Appleseed" var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)" } //MARK: - “Another way to handle optional values is to provide a default value ”“using the ?? operator. If the optional value is missing, the default value is used instead.” let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)" /**
(lldb) po nickName
nil (lldb) po fullName
"John Appleseed" (lldb) po informalGreeting
"Hi John Appleseed"
*/ //MARK: - Switch Case的用法
let vegetable = "red pepper"
switch vegetable {
case "celery":
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
print("That would make a good tea sandwich.") //用x来标识vegetable 看看这个字符串的尾部是否有pepper Returns a Boolean value indicating whether the string ends with the specified suffix.
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
//Is it a spicy red pepper? default:
print("Everything tastes good in soup.") } //MARK: - 快速遍历数组 并且找出来最大值
/**
三个字典还是鄙视比较好玩的 素数 斐波那契数列 平方
*/
let interestingNumbers = [
"Prime": [, , , , , ],
"Fibonacci": [, , , , , ],
"Square": [, , , , ],
]
var largest =
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
print(largest) //25 //MARK: - 循环
var n =
while n < {
n = n *
}
print(n)
//
var m =
//就类似于do while
repeat {
m = m *
} while m <
print(m) // /** “Use ..< to make a range that omits its upper value, and use ... to make a range that includes both values.”
*/ var total =
for i in ..< {
total += i //1 2 3
} print(total)// var totalBoth =
for i in ... {
totalBoth += i//1 2 3 4
}
print(totalBoth)//10 //MARK: - 函数和闭包
//函数 函数参数 返回值
func greet(person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
let s = greet(person: "Bob", day: "Tuesday") //"Hello Bob, today is Tuesday." func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
var min = scores[]
var max = scores[]
var sum = for score in scores {
if score > max {
max = score
} else if score < min {
min = score
}
sum += score
} return (min, max, sum)
}
let statistics = calculateStatistics(scores: [, , , , ])
print(statistics.sum)
print(statistics.)
//120 120
/**
(lldb) po statistics.min
3 (lldb) po statistics.max
100 (lldb) po statistics.sum
120
*/ //可变参数的函数 zhu'yi
func sumOf(numbers: Int...) -> Int {
var sum =
for number in numbers {
sum += number
}
return sum
}
let sumOfEmptyParam = sumOf() let sumOfThree = sumOf(numbers: , , ) /**
(lldb) po sumOfEmptyParam
0 (lldb) po sumOfThree
651
*/ //函数A的内部有函数B 函数A内部还调用了函数B
func returnFifteen() -> Int {
var y =
func add() {
y +=
}
add()
return y
} let fifteenFunc = returnFifteen() //15 //参数为空 返回值为闭包类型
func makeIncrementer() -> ((Int) -> Int) {
//参数为整型 返回值为整数
func addOne(number: Int) -> Int {
return + number
}
return addOne
}
var increment = makeIncrementer()
increment() //8 //“A function can take another function as one of its arguments.
//函数的返回值是一个函数 而且不仅仅是一个函数那么简单 只可意会哈哈
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 <
}
var numbers = [, , , ]
let nubersB = hasAnyMatches(list: numbers, condition: lessThanTen) //true 7满足条件
print(nubersB) //MARK: - 闭包
//“You can write a closure without a name by surrounding code with braces ({}). ”
//到了闭包了 先停一下

突然兴起复习一下Swift3.0的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  3. Swift3.0变化分享

    Swift 3.0 做出的改变很大,在这篇文章中,我将尽我所能,利用代码样例给大家解释Swift 3.0最重要(要命)的改变,希望大家能够做好升级Swift 3.0 的准备.Swift 3.0的改变不 ...

  4. swift3.0变化总结

    Swift 3.0 做出的改变很大,在这篇文章中,我将尽我所能,利用代码样例给大家解释Swift 3.0最重要(要命)的改变,希望大家能够做好升级Swift 3.0 的准备.Swift 3.0的改变不 ...

  5. 关于for循环------swift3.0

    在程序开发当中,for循环使用的频率无疑是最高的.常用的swift循环是递增式遍历.当然各种循环,swift都能办到.但其大多采用关键字形式实现,大部分开发者更喜欢直接使用C式循环代码.在swift3 ...

  6. Swift2.3 --> Swift3.0 的变化

    Swift3.0语法变化 首先和大家分享一下学习新语法的技巧: 用Xcode8打开自己的Swift2.3的项目,选择Edit->Convert->To Current Swift Synt ...

  7. Swift3.0都有哪些变化

    从写第一篇Swift文章的时候到现在Swift已经从1.2发展到了今天的3.0,这期间由于Swift目前还在发展阶段并不能向下兼容,因此第一篇文章中的部分代码在当前的Xcode环境中已经无法运行.在W ...

  8. iOS开发 swift3.0中文版

    swift3.0中文版: http://pan.baidu.com/s/1nuHqrBb

  9. swift3.0的改变

    Swift在这2年的时间内,发展势头迅猛,在它开源后,更是如井喷一样,除了 iOS.mac 平台,还支持了 Linux. 而今年下半年, Swift 3.0 也会随之发布.https://github ...

随机推荐

  1. 如何将md文件转换成带目录的html文件

    配置环境node 去官网下一个node安装包,下一步下一步: 由于现在的node都自带npm,直接 npm install i5ting_toc 这样安装好了i5ting_toc这个包, 进入你实现准 ...

  2. 【SqlServer系列】表连接

    1   概述 1.1  已发布[SqlServer系列]文章 [SqlServer系列]MYSQL安装教程 [SqlServer系列]数据库三大范式 [SqlServer系列]表单查询 1.2  本篇 ...

  3. eclipse打开时提示:failed to create the java virtual machine

    Eclipse打开时提示: failed to create the java virtual machine 原因:C盘空间不够   编辑删除 处理:1.用金山清理临时文件: 2.用金山手机卫士连接 ...

  4. 【亲测】appium_v1.4.16版本自动化适配android7.0系统

    要解决的问题:appium在androidV7.0系统上运行时报错 Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.a ...

  5. Linux最小化安装

    1,linux安装网络自动配置: 2,linux硬盘分配 1,/boot 用来存放与 Linux 系统启动有关的程序,比如启动引导装载程序等,建议大小为 100-200MB . 2,swap 实现虚拟 ...

  6. Android之IPC(aidl)

    IPC(Inter-Process Conmunication) 进程间通讯 在同一进程中,各个组件进行通信是十分方便的,普通的函数调用就可以解决:但是,对于处于不同进程中的组件来说,要进行通信,就需 ...

  7. 钉钉 机器人接入 自定义webhook

    钉钉出了个webhook机器人接入,自定义的机器人支持随时post消息到群里: 昨天就尝试着用C#写了个: 一开始用python写,但是莫名的提示  {"errmsg":" ...

  8. synchronized优化

    重量级锁 synchronized关键字 前文解释了synchronized的实现和运用,了解monitor的作用,但是由于monitor监视器锁的操作是基于操作系统的底层Mutex Lock实现的, ...

  9. Jenkins2 实现持续交付初次演练(MultiJob,Pipeline,Blue Ocean)

    背景 项目需要用到自动部署,但可获取外网的节点机器只有一台,那只能同过主节点机器进行构建完成然后分发至对应服务器进行启动更新. 目前已尝试过三种方式: 1.Pipeline-Trigger param ...

  10. linux执行sh报错:$’\r’: 未找到命令的解决

    背景 执行.sh脚本时出现$'\r': 未找到命令, 原因 是因为命令直接从windows 复制过来导致的 解决 yum install dos2unix dos2unix **.sh 进行转换 再次 ...