Swift--控制流与oc不同的地方
1.For-in循环中...
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
for _ in 1...5 {
可以用下划线忽略当前值
}
2.字典通过元祖返回
3.do while循环变成repeat
repeat {
statements
} while condition
4.switch不需要break
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
5.switch case的body不能为空
6.case可以带区间
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
7.case的元祖表示
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("(0, 0) is at the origin")
case (_, 0):
print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
8.case加额外条件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
9.case fallthrough贯穿
fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件。fallthrough简单地使代码继续连接到下一个 case 中的代码
10.while加标签
gameLoop: while square != finalSquare {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// diceRoll will move us to the final square, so the game is over
break gameLoop
case let newSquare where newSquare > finalSquare:
// diceRoll will move us beyond the final square, so roll again
continue gameLoop
default:
// this is a valid move, so find out its effect
square += diceRoll
square += board[square]
}
}
print("Game over!")
11.guard与if的区别
像if语句一样,guard的执行取决于一个表达式的布尔值。我们可以使用guard语句来要求条件必须为真时,以执行guard语句后的代码。不同于if语句,一个guard语句总是有一个else从句,如果条件不为真则执行else从句中的代码。
guard let name = person["name"] else {
return
}
12.检测 API 可用性
Swift内置支持检查 API 可用性,这可以确保我们不会在当前部署机器上,不小心地使用了不可用的API。
if #available(iOS 10, macOS 10.12, *) {
// 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API
} else {
// 使用先前版本的 iOS 和 macOS 的 API
}
在它一般的形式中,可用性条件使用了一个平台名字和版本的列表。平台名字可以是iOS,macOS,watchOS和tvOS——请访问声明属性来获取完整列表。除了指定像 iOS 8的主板本号,我们可以指定像iOS 8.3 以及 macOS 10.10.3的子版本号。
Swift--控制流与oc不同的地方的更多相关文章
- 用Swift重写公司OC项目(Day1)--程序的AppIcon与LaunchImage如何设置
		公司之前的APP呢经过了两次重写,都是使用OC由本人独立开发的,不过这些东西我都不好意思说是自己写的,真心的一个字:丑!!! 客观原因来说主要是公司要的特别急,而且注重的是功能而非效果,公司的美工之前 ... 
- 如何在swift中实现oc中的分类
		在oc中为了增强已有类的功能,我们经常使用分类.使用分类,我们可以在不破坏原有类的结构的前提下,对原有类进行模块化的扩展. 但是在swift中没有分类这种写法了.相对应的是swift中只有扩展(Ext ... 
- swift实现与OC的混编
		swift与OC的混编 现在写swift,很多的类库还不是很全,很多的第三方还是只有OC版的,这个时候swift想用,通常都是采用的swift和OC混编的方式.这里给大家演示一下混编是如何做的. sw ... 
- Swift: 在Swift中桥接OC文件(自己创建的类文件、第三方库文件)
		一.介绍 随着Swift的逐渐成熟,使用swift开发或者混合开发已经成为了一个趋势,本身苹果公司也十分推荐使用Swift这门新语言.目前Swift已经更新到了3.0,估计没有多久4.0就要出来了.那 ... 
- Swift基础之OC文件调用Swift代码(在上次的基础上写的)
		前两天刚写过Swift调用OC,今天在原来的基础上,实现OC调用Swift. 首先,创建一个OneSwiftFile.swift文件,创建一个继承于NSObject的类(这个地方你可以自己选择继承的父 ... 
- swift混编oc碰到的问题
		在swift中混编苹果官方的Reachability OC文件. 因为swift工程的target是生成framework而非app,framework中调用oc与app中使用桥接文件还不一样,参考: ... 
- Swift控制流
		本文简单的介绍swift一些基本语法的使用,在本文中不会做更深的剖析,只提及一些语法的简单的使用,快速学会编写swift程序.高手请绕路走嘿嘿 常量与变量: swift中定义所有的变量使用var,定义 ... 
- 用Swift重写公司OC项目(Day2)--创建OC与Swift的桥接文件,进而调用OC类库
		昨天把项目中的图标以及启动转场图片弄好了,那么今天,我们可以开始慢慢进入到程序的编写当中了. 由于swift较新,所以类库还不够完善,但是不用担心,苹果早就出了解决方案,那就是使用桥接文件,通过桥接文 ... 
- iOS开发:在Swift中调用oc库
		先列举这个工程中用到的oc源码库: MBProgressHUD:半透明提示器,Loading动画等 SDWebImage:图片下载和缓存的库 MJRefresh: 下拉刷新,上拉加载 Alamofir ... 
- 在swift中使用oc 的代码
		就是需要一个桥文件, 方法一:在swift项目中,新建一个oc的类,这时候,会弹出一个对话框,你点默认的那个选项就行了.然后在新生成的桥文件中导入你所需要的oc代码的头文件就行了. 方法二:但是有时候 ... 
随机推荐
- 1104解决ecos挂件中数组传递的相关问题。
			1.挂件综述: 挂件组成:_config.html 后台配置,即点添加时的弹出框. default.html 前台显示,即在前台显示出来的页面. widget.php 设置挂件的基本信息.. ... 
- 解压Taobao手机客户端发现的东西
			今天解压了Taobao手机客户端发现了几个.so文件, 其中有两个挺感兴趣的,查了一下,以后去研究下. libBSPatch.so 是用于支持增量更新功能的库文件. libwebp.so 好像是We ... 
- 转:支撑Github的开源技术
			原文来自于:http://www.infoq.com/cn/news/2014/03/projects-power-github Github在3月19号开放了新的项目展示页面(Showcase),S ... 
- Baidu Map Web API 案例
			<html> <head> <meta http-equiv="Content-Type" content="text/html; char ... 
- use "man rsyslogd" for details. To run rsyslog interactively, use "rsyslogd -n"to run it in debug mo
			zjtest7-frontend:/root# service rsyslog start Starting system logger: usage: rsyslogd [options] use ... 
- Linux 安装字体
			把XP下的字体C:\WINDOWS\FONTS\simsun.ttc(也就是宋体,大小为10M),把他重命名为 simsun.ttf 拷贝simsun.ttf 字体到 /usr/share/fonts ... 
- 【HDOJ】4541 Ten Googol
			打表的大水题. /* 4541 */ #include <cstdio> #include <cstdlib> #include <cstring> , , , } ... 
- http协议使用实例
			#include <stdio.h>#include <windows.h>#include <wininet.h> #define MAXSIZE 1024#pr ... 
- 黑马程序员_Java_collections and Arrays(工具类)
			collections collections工具类方法 1,static <T extends Comparable<? super T>> void sort(List&l ... 
- HER COFFEE夜场代金券【1折】_北京美食团购_360团购导航
			HER COFFEE夜场代金券[1折]_北京美食团购_360团购导航 HER COFFEE夜场代金券 
