Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value. These are known as designated initializers and convenience initializers. Designated:指定的:特指的 全初始化与部分初始化 Designated Initializers and…
总结:整个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…
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Doubleand Float for floating-point values 2.新类型 Swift introduces advanced types not found in Objective-C, such as tuples. Tuples…
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…
结论: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”,…
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type. You can define a c…
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return type. Function Types Every function has a specific function type, made up of the parameter types and the return type of the function. For example: func…
一.变量定义 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…
定义有什么,及哪些必须实现. A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Property Requirements The protocol doesn’t specify whether the property should be a stored property or…
结论:扩展无法修改原来的数据结构. 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…
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…
Lazy Stored Properties A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration. let kScreenWidth = UIScreen.ma…
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…
Assigning to self Within a Mutating Method Mutating methods can assign an entirely new instance to the implicit self property. The Point example shown above could have been written in the following way instead: struct Point { var x = 0.0, y = 0.0 mut…
Topics Logical Values struct Bool A value type whose instances are either true or false. Numeric Values struct Int A signed integer value type. struct Double A double-precision, floating-point value type. struct Float A single-precision, floating-poi…
1.Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren’t equal. (1, "zebra") < (2, "apple") You can compare two tuples if they have the same type and the same number of values.…
更新记录: 该Storyboard教程由Caroline Begbie更新iOS 8和Swift相关内容.原文作者为教程编纂组的成员Matthijs Hollemans. 2014/12/10更新: 更新至Xcode 6.1.1. Storyboard是最先在iOS 5引入的一项振奋人心的特性,大幅缩减构建App用户界面所需的时间. 要介绍Storyboard是什么,我打算从这张图讲起.下面是您将会在本教程中构建的Storyboard: 或许你现在并不清楚这个App是用来做什么的,但其中有哪些页…
Swift语言教程中文文档 Swift语言教程(一)基础数据类型 Swift语言教程(二)基础数据类型 Swift语言教程(三)集合类型 Swift语言教程(四) 集合类型 Swift语言教程(五)控制流 Swift语言教程(六)函数 Swift语言教程(七)闭包 Swift语言教程(八) 枚举类型 Swift语言教程(九)类与结构 Swift语言教程(十)属性 Swift语言教程(十一)方法 Swift语言教程(十二)下标 Swift语言教程(十三)继承 Swift语言教程(十四)初始化 Sw…
Swift语言快速入门(首部同步新版官方API文档和语法的Swift图书,确保代码可编译,作者专家在线答疑,图书勘误实时跟进) 极客学院 编著   ISBN 978-7-121-24328-8 2014年10月出版 定价:59.00元 428页 16开 编辑推荐 本书内容翔实,实例丰富,同步新版官方API文档和语法 语法|词法|框架覆盖全面,实战案例|配套习题丰富 多位Swift专家联合推荐,不仅是Swift入门书籍,也可做案头工具书 作者携手Swift专家在线答疑,方式如下: 1.专属勘误交流…
今天我们要讲的就是函数[对于函数,在最后面还有几道题,喜欢的博友可以看了自己做一下,和我交流一下] 当然这与我们的c语言还是有一定的共同之处的,对于有一些c语言或者是java基础的童鞋,我觉得是很容易的. 定义函数的语法为:[注意一些书写格式] func 函数名(参数1: 类型1,参数2:类型2)-> 返回类型{代码块} 在本节课我们将涉猎到函数的参数为函数,返回值为函数等情况 第二课时: //: Playground - noun: a place where people can play…
经过不断的努力,小哥也买了台苹果设备,终于可以开始我的IOS之旅了,说来确实令人苦恼,为了学习IOS我这着贫农阶级,省了几个月的零花钱,外加向亲朋好友求救,最终痛下心扉,卖了台MAC pro128G版.苦逼的事情就不在多说,下面聊一聊,初次使用的心得体会,IOS的做工确实没说的,铝合金的机身,看上去就很显身份,用起来确实流畅,而且附带了很多有趣的应用,其中最让我惊奇的是段视频制作软件,提供了很多模板,让我制作小视频方便了很多.其次就是它的多手指操作了,炫酷的很,不过初次拿到本本时,确实很头疼,不…
因为最近事情比较多,有一段时间没有发表博客了.前一段时间,利用空余时间翻译了一本关于Swif的书籍,过一段时间就会出版吧.通过翻译此书,英语水平没提高多少,不过Swift算是系统的学习了一下. 在翻译此书的时候,被Swift语言的优雅所迷住.感觉Swift是iOS开发的未来,Swift保留了Objective-C的优点,除此之外还引入了好多好多Objective-C中没有的新特性,比如运算符重载,泛型,闭包等等,一些现代编程语言中比较好用的特性.同时Swift中不允许使用隐式类型转换,这样是类型…
一份关于Swift语言学习资源的整理文件     周银辉 在这里下载 https://github.com/ipader/SwiftGuide…
因为想要学Mac os x编程,中文教材太少了,看了很多厉害的英文教材,很多都是swift语言的了,所以决定先要大体学一下swift语言. 学习一门语言,第一件事看swift官方文档,这里附上CocoaChina翻译小组翻译自苹果的官方文档:The Swift Programming Language.本篇译者:葛布林大帝(博客)  :http://www.cocoachina.com/industry/20140613/8818.html 好啦,下面开始学习吧~…
Swift语言有很多特性,其中之一就是类型方法,相对于其他比较流行的编程语言(C#.Java),在Swift中类型方法最大的特征在于它的可继承性,我们举个例子说明: 俗话说,龙生龙凤生凤老鼠生儿会打洞:虽说龙和鼠都属于动物,但毕竟不是同类,所以生出来的当然不一样. 同理,对于Dragon和Mouse两个类而言,它们的newInstance类型方法返回的结果应该是各自类的实例: 我们先创建一个名为Animal的协议: protocol Animal { class func newInstance…
Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. Swift builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern feat…
这节课需要讲的就是可选链,内存管理,引用计数,unowned解决 //: Playground - noun: a place where people can play import UIKit //可选链(optional chain) //class A { // var p: B? // //} // //class B //{ // var p: C? //} // //class C { // func cm() -> String { // print("cm")…