设为首页 加入收藏

TOP

Swift 07.关键字(二)
2017-10-10 12:17:09 】 浏览:2542
Tags:Swift 07. 关键字
= "C" garage[3] = "D" garage[2] = "CC" println("index 1 = \(garage[0]) ,index 2 = \(garage[1]),index 3 = \(garage[2]) ,index 4 = \(garage[3])") }

输出

index 1 = A ,index 2 = B,index 3 = CC ,index 4 = D  

typealias

类型别名,就像typedef一样。借typedef  unsigned long int    UInt64 

同样在swift中也可能自定义类型。

 break

跳出循环,通常用于for,while,do-while,switch 

 

case

case相信大家并不陌生,常在switch中使用,但如今在swift中多了一个地方使用哪就是枚举类型。

 

continue

跳过本次循环,继续往后执行。

 

default

缺省声明。常见在switch中。

 

do, else,if, for, return, switch, while

这几个就不用多说了,越说越混。

in

范围或集合操作

let str = "123456"  
for c in str  
{  
     println(c)  
}  

 

fallthrough

由于swift中的switch语句中可以省去了break的写法,但在其它语言中省去break里,会继续往后一个case跑,直到碰到break或default才完成。在这里fallthrough就如同其它语言中忘记写break一样的功效。

let integerToDescribe = 1  
var description = "The number \(integerToDescribe) is"  
switch integerToDescribe {  
case 1, 3, 5, 7, 11, 13, 17, 19:  
    description += " a prime number, and also";  
    fallthrough  
case 5:  
    description += " an integer"  
default :  
    description += " finished"  
}  
  
println(description)  

 

输出:

The number 1 is a prime number, and also an integer  

 

where

swift中引入了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")  
}  

当switch的条件满足where 后面的条件时,才执行语句。

is

as

is 常用于对某变量类型的判断,就像OC中 isKindClass ,as 就有点像强制类型转换的意思了。

 for view : AnyObject in self.view.subviews  
{  
    if view is UIButton  
    {  
        let btn = view as UIButton;  
        println(btn)  
    }  
}  

OC的写法:

 

for (UIView *view  in self.view.subviews)  
{  
      if ([view isKindOfClass:[UIButton class]])         //is 操作  
     {  
             UIButton *btn =(UIButton *)view             //as 操作  
      }  
}  

 

super

基类的关键语,通常称父类

__COLUMN__, __FILE__, __FUNCTION__, __LINE__

是不是有点像宏定义啊。

println(__COLUMN__ ,__FILE__, __FUNCTION__, __LINE__)  

 

输出:

(17, /Users/apple/Desktop/swiftDemo/swiftDemo/ViewController.swift, viewDidLoad(), 62)  

 

set,get

 

常用于类属性的setter getter操作。

 

willSet,didSet

在swift中对set操作进行了扩展,willset 在set新值成功前发生,didset在设置新值成功后发生。

inout

对函数参数作为输出参数进行修饰。

 

func swapTwoInts(inout a: Int, inout b: Int) {  
    let temporaryA = a  
    a = b  
    b = temporaryA  
}  

 

 

mutating

具体不是很理解,好像是专为结构体使用而设置的变体声明

 

protocol ExampleProtocol {  
    var simpleDescription: String { get }  
    mutating func adjust()  
}  
  
class SimpleClass: ExampleProtocol {  
    var simpleDescription: String = "A very simple class."  
    func adjust() {  
        simpleDescription += "  Now 100% adjusted."  
    }  
}  
  
  
struct SimpleStructure: ExampleProtocol {  
    var simpleDescription: String = "A simple structure"  
    mutating func adjust() {                //如果去除mutating 报Could not find an overload for '+=' that accepts the supplied arguments  
        simpleDescription += " (adjusted)"  
    }  
}  

 

 

测试

func testMutating()  
{  
    var a = SimpleClass()  
    a.adjust()  
    let aDescription = a.simpleDescription  
    println(aDescription)  
      
    var b = SimpleStructure()  
    b.adjust()  
    let bDesc
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇窥探Swift系列博客说明及其Swift.. 下一篇博客园 Mac客户端 1.0 源码

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目