结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: first, “value types”, where each instance keeps a unique copy of its data, usually defined as a struct, enum, or tuple. The second, “reference types”,…
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例 2.is 类型检查 Use the type check operator (is) to check whether an instance is of a certain subclass type. 3. (as? or as!) 类型转化 Use the conditional f…
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNeeded: Int) case outOfStock } throw VendingMachineError.insufficientFunds(coinsNeeded: 5) 2.异常捕获与栈展开 Error handling in Swift resembles exception handlin…
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state. You c…
Overriding A subclass can provide its own custom implementation of an instance method, type method, instance property, type property, or subscript that it would otherwise inherit from a superclass. This is known as overriding. Overriding by accident…
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print("Bing!") } override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .whiteColor() let mySwitch : UISwitch = { view.addSubview($0) Cent…
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. 下标的形式和函数相同,并且set和get合一 subscript(row: Int, column: Int) -> Double 比较: In addition to simple propert…
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String 类型表示,对应着 Character 类型值的集合. Swift 中的 String 类型为你的编程提供了一个高速的,兼容 Unicode规范 的文本处理方式.Swift 创建和处理字符串的语法轻量可读,与 C 语言的字符串语法颇为相似.字符串的拼接非常简单,只需将两个字符串用 + 运算符相加.字符串的值是否…
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -> in } reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 }) Because the sorting closure is passed as an argumen…
结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing functionality. If you define an extension to add new functionality to an existing type, the new functionality will be available on all existing instan…