一、变量定义

1、常量与变量

Use let to make a constant and var to make a variable.

2、类型与推测

However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type.

3、类型转换与字符串

Write the value in parentheses, and write a backslash (\) before the parentheses.

  1. let appleSummary = "I have \(apples) apples."

4、集合类型:

  1. let emptyArray = [String]()
  2. let emptyDictionary = [String: Float]()

总结:(1)类型写在变量后,(2)类型可推断,(3)便捷的字符串化操作。

二、控制流

1、控制流综述

Use if and switch to make conditionals, and use for-inwhile, and repeat-while to make loops.

2、optional value ?

An optional value either contains a value or contains nil to indicate that a value is missing.

3、default value ??

4、if let 判断是否为非空

You can use if and let together to work with values that might be missing.

5、if 后只能跟bool

6、swich 可以使用任意值

7、

let x where x.hasSuffix("pepper")

将switch值赋给x,如果复合范式,最终会赋给x

where vegetable.hasSuffix可以用本体;

8、for-in 使用了类型推断技术

for (kind, numbers) in interestingNumbers 可以用于字典;

总结:

if的特别性、?、??、switch支持任意类型。

三、函数与闭包

1、函数形式

func greet(person: String, day: String) -> String{}

简化表示:()->(){}

2、no argument label

参量无名调用;向c++靠近;

3、多输出函数

func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int)

4、高阶函数

5、闭包及其简化。

总结:

Functions are a first-class type

三、类

1、类的初始化函数化

  1. var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")

2、 属性:willSet and didSet getter and a setter

3、 If the value before the ? is nil, everything after the ? is ignored and the value of the whole expression is nil.

总结:类的使用函数化,属性的访问控制便捷化。

四、枚举与结构

1、类与结构的区别

One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.

2、枚举Use the init?(rawValue:) initializer

Use the init?(rawValue:) initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil if there is no matching Rank

if let convertedRank = Rank(rawValue: 3)

3、枚举值的直接引用

  1. let hearts = Suit.hearts

4、结构型枚举

  1. enum ServerResponse {
  2. case result(String, String)
  3. case failure(String)
  4. }
  5. let success = ServerResponse.result("6:00 am", "8:09 pm")
  6. let failure = ServerResponse.failure("Out of cheese.")

五、协议与扩展

与oc语言无区别;

扩展可以针对任何类型。

六、异常捕获

Another way to handle errors is to use try? to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is nil. Otherwise, the result is an optional containing the value that the function returned.

  1. let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
  2. let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")

七、defer

Use defer to write a block of code that is executed after all other code in the function, just before the function returns.

八、范型

<>

swift语言点评一的更多相关文章

  1. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  2. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  3. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  4. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  5. swift语言点评十七-Designated Initializers and Convenience Initializers

    Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...

  6. swift语言点评十四-继承

    Overriding A subclass can provide its own custom implementation of an instance method, type method, ...

  7. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  8. swift语言点评八-枚举

    总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...

  9. swift语言点评五-Function

    一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...

随机推荐

  1. [OS][ linux ] 建立新帳號, 變更密碼, 加入 sudoer

    新增 linux , 設定預設 password, 新增 user 到 sudoers 1. 新增 User sudo useradd aa97 2. 設定 User password sudo pa ...

  2. SQL Server 2014 中,新建登录用户,分配权限,并指定该用户的数据

    一.运行环境 系统:Windows 10数据库:SQL Server 2014数据库名: APP     新建的用户名: app 二.操作步骤 1.打开 MS SQL Server Managemen ...

  3. C#测量程序运行时间及cpu使用时间实例方法

    private void ShowRunTime() { TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime; Stopwatc ...

  4. Markdown标记语言

    Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber).它允许人们“使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者HTML)文档”.这种语言吸收了很 ...

  5. ONGUI->NGUI->UGUI (Unity UI史)

    各GUI的介绍 ONGUI:Unity自带的绘制界面工具,它的成像原理是基于表层的,所以执行效率非常的低,并且没有提供复杂的UI的接口,就算开发者硬着头皮写上去只能让UI的执行效率更低. NGUI:第 ...

  6. Day 07 -02 拷贝 浅拷贝 深拷贝

    必考 存一个值还是多个值 一个值:整型/浮点型/字符串 多个值:列表/元祖/字典/集合 有序or 无序 有序:字符串/列表/元祖 无序:字典/集合 可变or 不可变 可变:列表/字典/集合 不可变:整 ...

  7. jpa自定义条件分页查询

    主要依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  8. static和extern的作用域--题目

    #include <stdio.h> ; int main(void) { , sum = , count = ; ,count++) // count = 2 { ; count++; ...

  9. js实现点击复制网页内容(基于clipboard.js)

    浏览网页过程中会遇到点击复制链接地址的情况,下面就介绍一种实现方法,该方法是基于clipboard.js: 官网地址:https://clipboardjs.com/: clipboard.js使用比 ...

  10. android开发面试题

    找了将近两个星期的工作,面试了5家公司,罗列一下笔试或者面试时的问题,祝大家好运 1,handler机制 答:handler执行机制:1).在主线程中创建handler 2).子线程中借助主线程的ha ...