swift语言点评一
一、变量定义
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.
let appleSummary = "I have \(apples) apples."
4、集合类型:
let emptyArray = [String]()
let emptyDictionary = [String: Float]()
总结:(1)类型写在变量后,(2)类型可推断,(3)便捷的字符串化操作。
二、控制流
1、控制流综述
Use if
and switch
to make conditionals, and use for
-in
, while
, 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、类的初始化函数化
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、枚举值的直接引用
let hearts = Suit.hearts
4、结构型枚举
enum ServerResponse {
case result(String, String)
case failure(String)
}
let success = ServerResponse.result("6:00 am", "8:09 pm")
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.
let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
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语言点评一的更多相关文章
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- swift语言点评十四-继承
Overriding A subclass can provide its own custom implementation of an instance method, type method, ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift语言点评八-枚举
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...
- swift语言点评五-Function
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...
随机推荐
- ffmpeg常用指令
在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundation -list_devices true -i "" -f 指定的是输入输出格式, -i指定输入的 ...
- JAVA 日期工具类的总结
一般,在项目中,我们会会经常使用到日期的各种方式的处理,在各个业务逻辑操作中,都需要相关的日期操作,因此,实现项目中的日期工具类的提出,还是十分重要的,下面,就项目中常用到的日期的相关操作方式,做了一 ...
- JDBC程序实例
实例 ( Statement ): public class JDBC { public static void main(String[] args) throws Exception { Conn ...
- css——样式的继承
css的样式继承 在上面这段代码中,p{}为父类,b{}为子类.b{}将继承p{}. 因为<b>在<p>里面,p{}为父类,b{}为子类. 继承方式: 如果子类没有,父类有,则 ...
- pyqt5的QWebEngineView 使用方法
说明1:关于QWebEngineView pyqt5 已经抛弃 QtWebKit和QtWebKitWidgets,而使用最新的QtWebEngineWidgets. QtWebEngineWidget ...
- 小学生都能学会的python(运算符 和 while循环)
---恢复内容开始--- 小学生都能学会的python(运算符和编码) 一.格式化输出 #占位:"%s"占位,占得是字符串,"%d"占位,占的是数字. # 让用 ...
- STM32 HAL库利用DMA实现串口不定长度接收方法
参考:https://blog.csdn.net/u014470361/article/details/79206352 我这里使用的芯片是 F1 系列的,主要是利用 DMA 数据传输方式实现的,在配 ...
- 常用js方法封装
常用js方法封装 var myJs = { /* * 格式化日期 * @param dt 日期对象 * @returns {string} 返回值是格式化的字符串日期 */ getDates: fun ...
- Java基础学习总结(59)——30 个java编程技巧
1.return 一个空的集合,而不是 null 如果一个程序返回一个没有任何值的集合,请确保一个空集合返回,而不是空元素.这样你就不用去写一大堆 "if else" 判断null ...
- POJ 3904
第一道莫比乌斯反演的题. 建议参看http://www.isnowfy.com/mobius-inversion/ 摘其中部分 证明的话感觉写起来会比较诡异,大家意会吧说一下这个经典题目:令R(M,N ...