极客班线下翻转课堂第六周
孔祥波
iOS 测试题解答:
- Static cell or dynamic
- Different identifiers for cells
- 学习 iOS 开发的目标是提升工资,还是爱好,还是进入一个好的团队?
- 应聘1-3年工作经验要求时,就报极客班孔老师的名字😎
- 学习曲线摆在那里,一点点进步。
- apple-ios-samples
- iOS-Swift-Demos
iOS 测试题解答:
From Swift Tutorial - Core Data by rm2kdev
Add some comments into the project.
1 | // |
1 | // AppDelegate.swift |
Debug:
1 | p applicationDocumentsDirectory |
1 | ssh root@192.168.0.101 |
From:Design Patterns: Delegation by Bart Jacobs
The definition of the delegation pattern is short and simple. This is how Apple defines the pattern.
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
An important difference between Swift and Objective-C is the possibility to mark protocol methods as optional. In Objective-C, the methods of a protocol are required by default. The methods of the UITableViewDelegate protocol, however, are optional. In other words, it is possible for a class to conform to the UITableViewDelegate protocol without implementing any of the protocol’s methods.
In Swift, however, a class conforming to a particular protocol is required to implement every method defined by the protocol. This is much safer since the delegating object doesn’t need to verify whether the delegate implements a protocol method. This subtle, but important, difference is illustrated later in this tutorial when we implement the delegation pattern.
The data source pattern fits nicely in the Model-View-Controller or MVC pattern. Why is that? A table view, for example, is part of the view layer. It doesn’t and shouldn’t know about the model layer and isn’t in charge of handling the data that is coming from the model layer. This implies that the data source of a table view, or any other view component that implements the data source pattern, is often a controller of some sort. On iOS, it’s usually a UIViewController subclass.
recipient:
1 | // AddItemViewController.h |
We declare a class, AddItemViewController, which extends UIViewController. The class declares a property, delegate, of type id
1 | // AddItemViewController.m |
Sender:
1 | // ViewController.m |
In Swift, the delegation pattern is just as easy to implement and you’ll find that Swift makes delegation slightly more elegant. Let’s implement the above example in Swift. This is what the AddItemViewController class looks like in Swift.
recipient:
1 | // AddItemViewController.swift |
The protocol declaration looks a bit different in Swift. Note that the AddItemViewControllerDelegate protocol extends the NSObjectProtocol instead of the NSObject protocol. In Swift, classes and protocols cannot have the same name, which is why the NSObject protocol is named differently in Swift.
Let’s now look at the ViewController class, which implements the AddItemViewControllerDelegate protocol. The interface shows us that the ViewController class extends the UIViewController class and adopts the AddItemViewControllerDelegate protocol.
Sender:
1 | // ViewController.swift |
Delegation is a pattern you’ll come across frequently when developing iOS and OS X applications. Cocoa relies heavily on this design pattern so it’s important to become familiar with it.
Since the introduction of blocks, a few years ago, Apple has slowly offered an alternative blocks-based API to some delegation implementations. Some developers have followed Apple’s lead by offering their own blocks-based alternatives. The popular AFNetworking library, for example, relies heavily on blocks instead of delegation, resulting in an elegant, intuitive API.
当对象没有拥有者时,指针变量的内存就该被释放。故 ARC 就是为了解决什么时候释放内存的问题。对应的就是引用计数为零时。
ARC 四个特性的典型用法:
1 | // |
学以致用,不如说:用到再学。这样可以保证能用到。记得 Introduction to HTTP 以前就看多,但是没有认真的分析,只是达到了了解的地步。现在 iOS 开发,也就认真仔细,尤其是能用到的部分。
老师讲的比较快,真的每个关键词都查文档,才会完成明白每一步什么意思。顺便加了注释(没忍住再次吐槽了段老师的代码没注释😅)。
Option 键快被我按坏了,今天才发现三只轻拍也可以 Quick Help。
提炼常用类,可以拆分代码,方便复用,逻辑清晰。
可以按照 MVC 或者其他方式整理项目文件。
安装配置 XAMPP
以微博 API:users/show 说明 Http 通信过程。
整个过程还算清晰主要是下载和登陆类添加代理,方便与对应的控制器通信。
服务器比较简陋,无法 POST 什么信息,都能从 http://localhost/login.xml 返回 user 信息。
看 CS193P 更优雅的方案,就是 NSJSONSerialization:转化为字典或数组,顺序如下面代码所示,最后使用 valueForKeyPath: 访问数组即可。
1 |
|
GWLoginRequest.m 文件代码如下:
1 | // |
Introduction to HTTP by Tealeaf Academy
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查,改,增,删4个操作。到这里,大家应该有个大概的了解了,GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
Design Patterns: Delegation by Bart Jacobs
@property (weak, nonatomic) id<AddItemViewControllerDelegate> delegate;
We declare a class, AddItemViewController, which extends UIViewController. The class declares a property, delegate, of type id. Note that the property is marked as weak, which means that an AddItemViewController instance keeps a weak reference to its delegate.
nil / Nil / NULL / NSNull by Mattt Thompson
1 | // 举个例子,这个表达... |
Symbol | Value | Meaning |
---|---|---|
NULL | (void *)0 | literal null value for C pointers |
nil | (id)0 | literal null value for Objective-C objects |
Nil | (Class)0 | literal null value for Objective-C classes |
NSNull | [NSNull null] | singleton object used to represent null |
重点在通知模式流程图,大致了解几种通知模式的用途的区别。都是理论的东西,还是希望实际写代码时联系理论,仔细考虑选择。
课件下载:https://github.com/gewill/GeekBand-iOS-Demo/tree/master/Design%20Patterns
以 Master-Detail Application 模板详细介绍了委托模式。孔老师喜欢直接看类的定义。
应用场景:
代码实现参看李久寧的文章:iOS 设计模式之四:观察者模式
可在 Xcode 中 Open Quickly(⇧⌘O),查看NSKeyValueCoding.h
协议的内容。
典型的例子 NSOperation and NSOperationQueue
1 |
|
###延伸阅读:
This document describes the NSKeyValueCoding informal protocol, which defines a mechanism allowing applications to access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables.
Dot Syntax and Key-Value Coding: Objective-C’s dot syntax and key-value coding are orthogonal technologies. You can use key-value coding whether or not you use the dot syntax, and you can use the dot syntax whether or not you use KVC. Both, though, make use of a “dot syntax.” In the case of key-value coding, the syntax is used to delimit elements in a key path. Remember that when you access a property using the dot syntax, you invoke the receiver’s standard accessor methods.
我们会常常提及“接收者”和“发送者”。它们在消息传递中的意思可以通过以下的例子解释:一个 table view 是发送者,它的 delegate 就是接收者。Core Data managed object context 是它所发出的 notification 的发送者,获取 notification 的就是接收者。一个滑块 (slider) 是 action 消息的发送者,而实现这个 action (方法)的是它的接收者。任何修改一个支持 KVO 的对象的对象是发送者,这个 KVO 对象的观察者就是接收者。明白精髓了吗?
基于不同消息传递机制的特点的流程图
###NSCoding
是一个简单的协议,有两个方法: -initWithCoder: 和 encodeWithCoder:。遵循NSCoding协议的类可以被序列化和反序列化,这样可以归档到磁盘上或分发到网络上。
1 | @interface Book : NSObject <NSCoding> |
提供了很方便的API把对象读取/写入磁盘。一个基于NSCoding的table view controller可以通过file manager设置它的属性集合。
1 | [NSKeyedArchiver archiveRootObject:books toFile:@"/path/to/archive"]; |
每个应用程序都有自己的user preferences,它可以存储和检索遵循NSCoding协议的对象或者是C类型数据。
1 | NSData *data = [NSKeyedArchiver archivedDataWithRootObject:books]; |
###延伸阅读:
- initWithDictionary:copyItems
就是个典型例子,可深可浅。
参看 MicroCai 的文章:iOS 集合的深复制与浅复制
Core Data 有点复杂,明明一个数据库,被苹果整的这么复杂。结果了还是看翻译的书才明白,太多新的概念需要消化。
Core Data 提供了直接使用 SQLite 的大部分大部分灵活性,同时无需关心关系数据库使用机制。
托管对象(Managed object)是 NSManagedObject 实例,应用主要与之交互。可视为字典。包含一组键值对。托管对象之间可以建立关系。
托管对象是在托管对象模型(NSManagedObjectModel)中定义的。托管对象模型包含一系列实体、实体的特性、特性和实体的有效性约束以及实体之间的关系。通常在 Xcode 中可视化模型编辑器创建的。
托管对象只能存在于托管对象上下文中(NSManagedObjectContext),即 Core Data 的工作区。托管对象只能在托管对象上下文中创建或获取。
Core Data 需要指定托管对象对应的实体,可使用 NSEntityDescription
对象的检索:直接使用 objectID;编写检索请求
检索请求可包含:排序描述符(NSSortDescriptor)、谓词(NSPredicate)、返回聚合函数(如 sum 和 count)的结果。
检索结果控制器(fetched results controller)可以讲检索请求与 UITableView 关联起来。使用委托方法可以更新表视图。
Core Data 环境
函数(Functions)是用来完成特地任务的独立的代码块;方法(Methods)是与某些特定类型相关联的函数。所以 Swift 中都用的 func 关键词。 结构体和枚举能够定义方法是 Swift 与 C/Objective-C 的主要区别之一。
Functions are self-contained chunks of code that perform a specific task.
Methods are functions that are associated with a particular type: classes, structures, and enumerations.
Sketch 3 Tutorials by LevelUpTuts: https://www.youtube.com/watch?v=-1BMzQLq7zk
A very basic tutorial, and very detailed. But lots of plugins usage.
Just like CSS style, change it every text in the style will update.
Star and Polygon can change Points.
Symbols is one of the brand new features in Sketch 3.0 and it allows you to re-use Groups, Layer and Text Styles in your document. Once added, simply insert them via Insert > Symbol in the toolbar.
We use collections of symbols and text styles inside Sketch: File -> New From Template.
Sketch App Sources is a website that focuses on providing resources for Sketch 3 by Bohemian Coding.
We aim to provide top quality resources that designers love to use, and make their workflow simpler and more enjoyable.
http://www.sketchappsources.com/
Generate-GIF: plugin for generating animated GIFs