作者:fengsh998
原文地址:http://blog.csdn.net/fengsh998/article/details/32133809
转载请注明出处
如果觉得文章对你有所帮助,请通过留言或关注微信公众帐号fengsh998来支持我,谢谢!

每一种语言都有相应的关键词,每个关键词都有他独特的作用,来看看swfit中的关键词:

关键词:

用来声明的:

“ class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, var.”

用于子句的:

“ break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, while.”

表达式和类型的:

“ as, dynamicType, is, new, super, self, __COLUMN__, __FILE__, __FUNCTION__, __LINE__”

//特殊语境使用的:

“didSet, get, inout, mutating, override, set, unowned, unowned(safe), unowned(unsafe), weak , willSet”

class

用来定义一个类,相信大家并不陌生。

如果定义一个汽车类

[cpp] view plaincopy
  1. class Car
  2. {
  3. init()
  4. {
  5. //to do init something.
  6. }
  7. }

init

相对于类的构造方法的修饰。

deinit

相对于类的释构方法的修饰。

对于类的构造和释构在swift 中需要使用关键词来修饰,而很多高级语言并不需要特别的指定,便C++ 只需要类名与构造函数名相同就可以,不需要额外的关键词。

enum

枚举类型的声明,这个与很多语方都相通。

extension

扩展,有点像oc中的categories 。

Swift 中的可以扩展以下几个:
添加计算型属性和计算静态属性
定义实例方法和类型方法
提供新的构造器
定义下标
定义和使用新的嵌套类型
使一个已有类型符合某个接口

如下面扩展字符串:

[cpp] view plaincopy
  1. extension String{
  2. struct _Dummy {
  3. var idxVal: Int
  4. var _padding: Int
  5. var _padding2: Int
  6. var _padding3: Int
  7. }
  8. //过虑出数字
  9. func fitlerCharater() -> String
  10. {
  11. var numberstr : String = ""
  12. for character in self
  13. {
  14. let s :String = String(character)
  15. //println(s.toInt())
  16. if let hs = s.toInt()
  17. {
  18. numberstr += character
  19. }
  20. }
  21. return numberstr
  22. }
  23. //扩展使用下标访问
  24. subscript (i: Int) -> Character {
  25. var dummy: _Dummy = reinterpretCast(i >= 0 ? self.startIndex : self.endIndex)
  26. dummy.idxVal += i
  27. let idx: String.Index = reinterpretCast(dummy)
  28. return self[idx]
  29. }
  30. //扩展使用Range访问
  31. subscript (subRange: Range<Int>) -> String {
  32. var start: _Dummy = reinterpretCast(self.startIndex)
  33. var end = start
  34. start.idxVal = subRange._startIndex
  35. end.idxVal = subRange._endIndex
  36. let startIndex: String.Index = reinterpretCast(start)
  37. let endIndex: String.Index = reinterpretCast(end)
  38. return self[startIndex..endIndex]
  39. }
  40. }

测试:

[cpp] view plaincopy
  1. func testExtension()
  2. {
  3. var str : String = "1234ab5国6cd7中8i90"
  4. println(str.fitlerCharater())
  5. let china: String = "china operating system public to 世界"
  6. println("使用下标索引访问第13个字符 \(china[13])")
  7. println("使用负号下标即变为从右往左访问字符 \(china[-1])")
  8. println("使用负号下标即变为从右往左访问字符 \(china[-2])")
  9. println("使用下标Range来访问范围 \(china[2...6])")
  10. dump(china[1..5], name: "china[1:4]")              //使用dump输出
  11. dump(china[10...13], name: "china[10:13]")
  12. }

输出:

[cpp] view plaincopy
  1. 1234567890
  2. 使用下标索引访问第13个字符 n
  3. 使用负号下标即变为从右往左访问字符 界
  4. 使用负号下标即变为从右往左访问字符 世
  5. 使用下标Range来访问范围 ina o
  6. - china[1:4]: hina
  7. - china[10:13]: atin

func

用来修饰函数的关键词。

import

导入头文件,相信大家都不陌生,但在swift 中好像被用来导入包,如import UIKit。 因为swift中没有了头文件的概念。

let

用来修改某一常量的关键词。像const 限定差不多

var

用来声明变量。

protocol

协议,也有称为接口,这个往往在很多高级语言中不能多重继承的情况下使用协议是一个比较好的多态方式。

static

用来修饰变量或函数为静态

struct

用来修饰结构体。

subscript

下标修饰,可以使类(class),结构体(struct),枚举(enum) 使用下标访问。

[cpp] view plaincopy
  1. class Garage
  2. {
  3. var products : String[] = Array()
  4. subscript(index:Int) -> String
  5. {
  6. get
  7. {
  8. return products[index]
  9. }
  10. set
  11. {
  12. if index < products.count  //&& !products.isEmpty
  13. {
  14. products[index] = newValue
  15. }
  16. else
  17. {
  18. products.append(newValue)
  19. }
  20. }
  21. }
  22. }

测试:

[cpp] view plaincopy
  1. func testSubscript()
  2. {
  3. var garage = Garage()
  4. garage[0] = "A"
  5. garage[1] = "B"
  6. garage[2] = "C"
  7. garage[3] = "D"
  8. garage[2] = "CC"
  9. println("index 1 = \(garage[0]) ,index 2 = \(garage[1]),index 3 = \(garage[2]) ,index 4 = \(garage[3])")
  10. }

输出

[cpp] view plaincopy
  1. 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

范围或集合操作

[cpp] view plaincopy
  1. let str = "123456"
  2. for c in str
  3. {
  4. println(c)
  5. }

fallthrough

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

[cpp] view plaincopy
  1. let integerToDescribe = 1
  2. var description = "The number \(integerToDescribe) is"
  3. switch integerToDescribe {
  4. case 1, 3, 5, 7, 11, 13, 17, 19:
  5. description += " a prime number, and also";
  6. fallthrough
  7. case 5:
  8. description += " an integer"
  9. default :
  10. description += " finished"
  11. }
  12. println(description)

输出:

[cpp] view plaincopy
  1. The number 1 is a prime number, and also an integer

where

swift中引入了where 来进行条件判断。

[cpp] view plaincopy
  1. let yetAnotherPoint = (1, -1)
  2. switch yetAnotherPoint {
  3. case let (x, y) where x == y:
  4. println("(\(x), \(y)) is on the line x == y")
  5. case let (x, y) where x == -y:
  6. println("(\(x), \(y)) is on the line x == -y")
  7. case let (x, y):
  8. println("(\(x), \(y)) is just some arbitrary point")
  9. }

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

is

as

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

[cpp] view plaincopy
  1. for view : AnyObject in self.view.subviews
  2. {
  3. if view is UIButton
  4. {
  5. let btn = view as UIButton;
  6. println(btn)
  7. }
  8. }

OC的写法:

[cpp] view plaincopy
  1. for (UIView *view  in self.view.subviews)
  2. {
  3. if ([view isKindOfClass:[UIButton class]])         //is 操作
  4. {
  5. UIButton *btn =(UIButton *)view             //as 操作
  6. }
  7. }

super

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

__COLUMN__, __FILE__, __FUNCTION__, __LINE__

是不是有点像宏定义啊。

[cpp] view plaincopy
  1. println(__COLUMN__ ,__FILE__, __FUNCTION__, __LINE__)

输出:

[cpp] view plaincopy
  1. (17, /Users/apple/Desktop/swiftDemo/swiftDemo/ViewController.swift, viewDidLoad(), 62)

set,get

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

willSet,didSet

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

inout

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

[cpp] view plaincopy
  1. func swapTwoInts(inout a: Int, inout b: Int) {
  2. let temporaryA = a
  3. a = b
  4. b = temporaryA
  5. }

mutating

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

[cpp] view plaincopy
  1. protocol ExampleProtocol {
  2. var simpleDescription: String { get }
  3. mutating func adjust()
  4. }
  5. class SimpleClass: ExampleProtocol {
  6. var simpleDescription: String = "A very simple class."
  7. func adjust() {
  8. simpleDescription += "  Now 100% adjusted."
  9. }
  10. }
  11. struct SimpleStructure: ExampleProtocol {
  12. var simpleDescription: String = "A simple structure"
  13. mutating func adjust() {                //如果去除mutating 报Could not find an overload for '+=' that accepts the supplied arguments
  14. simpleDescription += " (adjusted)"
  15. }
  16. }

测试

[cpp] view plaincopy
  1. func testMutating()
  2. {
  3. var a = SimpleClass()
  4. a.adjust()
  5. let aDescription = a.simpleDescription
  6. println(aDescription)
  7. var b = SimpleStructure()
  8. b.adjust()
  9. let bDescription = b.simpleDescription
  10. println(bDescription)
  11. }

override
父子类之间的函数重写,即复盖。

unowned, unowned(safe), unowned(unsafe)

无宿主引用。

[unowned self] 或[unowned(safe) self] 或[unowned(unsafe) self]

weak

弱引用,使得对象不会被持续占有

初探swift语言的学习笔记七(swift 的关健词)相关推荐

  1. 初探swift语言的学习笔记十(block)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/35783341 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  2. 初探swift语言的学习笔记九(OC与Swift混编)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/34440159 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  3. 初探swift语言的学习笔记一(基本数据类型)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28258805 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  4. 初探swift语言的学习笔记四(类对象,函数)

    2019独角兽企业重金招聘Python工程师标准>>> swift扩展了很多功能和属性,有些也比较奇P.只有慢慢学习,通过经验慢慢总结了. 下面将初步学习一下类的写法. 码工,最大爱 ...

  5. 初探swift语言的学习笔记八(保留了许多OC的实现)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/32715833 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  6. 初探swift语言的学习笔记五(线程)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/30354127 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...

  7. 初探swift语言的学习笔记十一(performSelector)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/35842441 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  8. 初探swift语言的学习笔记六(ARC-自动引用计数,内存管理)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/31824179 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  9. 初探swift语言的学习笔记三(闭包-匿名函数)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/29353019 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

最新文章

  1. 《炉石传说》架构设计赏析(2):Scene管理
  2. DL论文第一周-Deep learning
  3. android 按钮中断,android – 如何“中断”在AccessibilityService中执行的操作?
  4. C#调用SSIS包及读取DataReader目标
  5. java zip文件操作,java 关于 zip 文件 的 基本操作
  6. 字符串连接“+”int、char、string
  7. BestCoder Round #39 解题报告
  8. oracle临时表空间地址,Oracle认证:详解OracleTemp临时表空间处理方法
  9. sarscape 将dem文件转化成stl_SARscape与SARProz软件中的重要缩写
  10. The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  11. effective C++ 条款 11:在operator= 处理‘自我赋值’
  12. git添加多远端服务器并且实现push代码
  13. 计算机it岗考试试题,计算机IT职称考试试题及其答案.doc
  14. jquery[学习心得]ajax的注意点
  15. 拓端tecdat|R语言用WinBUGS 软件对学术能力测验(SAT)建立层次(分层)贝叶斯模型
  16. 未来能源互联网技术革命,能源产业大数据应用解析
  17. TensorFlow入门操作
  18. matlab 回声状态网络ESN的时间序列预测
  19. R语言文字云神器wordcloud2包
  20. iOS6和iOS7代码的适配(2)——status bar

热门文章

  1. linux内核全局变量重定位,关于可重定位文件中全局变量的一个重定位疑惑,借各位牛刀一用^...
  2. 化学反应工程 第一章 均相单一反应动力学和理想反应器
  3. 自然语言处理python进阶(二)
  4. keras从入门到放弃(五)独热编码和顺序编码
  5. centos安装mysql步骤,CentOS安装mysql超详细步骤
  6. 物体检测轻松上手:精度与速度实现兼得
  7. 决策树是如何选择特征和分裂点?
  8. 详解如何充分发挥先验信息优势,用MRC框架解决各类NLP任务
  9. HDU4809 Wow! Such City! Dijkstra算法
  10. 终于不会看不懂LSTM了