Putting to-do items into the checklists
Everything you’ve done in the previous section is all well and good, but checklists don’t actually contain any to-do items yet.
在前几期中所做过的一切都很好,但实际上核对清单不包含任何待办事项。
So far, the list of to-do items and the actual checklists have been separate from each other.
到目前为止,待办事项的列表项和实际核对清单已经彼此分开。
Let’s change the data model to look like this:
让我们像下面这样改变数据模型:
There will still be the lists array that contains all the Checklist objects, but each of these Checklists will have its own array of ChecklistItem objects.
仍然会有列表数组包含所有核对清单的对象,但这些核对清单将拥有自己的ChecklistItem对象的数组。
Add a new property to Checklist.swift:
keeps everything nice and compact
- 简洁紧凑
There you declared the array and initialized it in two different steps:
这里把上面一条语句拆分成 声明 一个数组并 初始化。
- 清晰明了:
- 坚持完整:
I personally don’t like this way of declaring variables because it violates the “DRY” principle – Don’t Repeat Yourself. Fortunately, thanks to Swift’s type inference, you can save yourself some keystrokes.
我个人不喜欢这种方式声明变量,因为它违反了“DRY”的原则(不要重复自己)。幸运的是,多亏了 Swift 的类型推断,你可以节省一些按键。
Remove the items instance variable from ChecklistViewController.swift
Then make the following changes in this source file:
Delete the following methods from ChecklistViewController.swift. (Tip: You may want to set aside the code from these methods in a temporary file somewhere; shortly you’ll be using them again in a slightly modified form.)
从 ChecklistViewController.swift 删除下列方法。(小贴士:你应该把这些方法复制到什么地方,我们之后还要使用,可以直接拖到 Xcode Code Snippet)
- func documentsDirectory()
- func dataFilePath()
- func saveChecklistItems()
- func loadChecklistItems()
Remove the lines that call saveChecklistItems().
Also delete init?(coder).
Press ⌘+B to make sure the app builds without errors.
Fake it util you make it 填充数据直到完工
Add the following to the bottom of AllListsViewController.swift’s init?(coder):
Using loops will often save you a lot of time. You could have written this code as follows:
使用循环往往会节省你很多时间。否则你可能要写如下这段代码:
Add the following to AllListsViewController.swift (you may want to copy this from that temporary file, but be sure to make the highlighted changes):
Change init?(coder) to:
Add the NSCoding protocol in Checklist.swift:
Add those methods to Checklist.swift:
Important: Before you run the app, remove the old Checklists.plist file from the Simulator’s Documents folder.
Doing saves differently
Parents and their children
There are three situations in which an app can terminate:
- While the user is running the app. This doesn’t happen very often anymore, but earlier versions of iOS did not support multitasking apps. Receiving an incoming phone call, for example, would kill the currently running app. As of iOS 4, the app will simply be suspended in the background when that happens. There are still situations where iOS may forcefully terminate a running app, for example if the app becomes unresponsive or runs out of memory.
- When the app is suspended in the background. Most of the time iOS keeps these apps around for a long time. Their data is frozen in memory and no computations are taking place. (When you resume a suspended app, it literally continues from where it left off.) Sometimes the OS needs to make room for an app that requires a lot of working memory – often a game – and then it simply kills the suspended apps and wipes them from memory. The apps are not notified of this.
- The app crashes. There are ways to detect crashes but handling them can be very tricky. Trying to deal with the crash may actually make things worse. The best way to avoid crashes is to make no programming mistakes! :-)
有三种使用程序终止的方案:
- 内存不足
- 挂起的给运行中的腾空间
- 程序崩溃
Fortunately for us, iOS will inform the app about significant changes such as, “you are about to be terminated”, and, “you are about to be suspended”.
application delegate
幸运的是,iOS将通知应用等重大变化,“你要终止”和“你要暂停”。
In fact, if you look inside AppDelegate.swift, you’ll see the methods:
func applicationDidEnterBackground(application: UIApplication)
and:
func applicationWillTerminate(application: UIApplication)
Add this new method to AppDelegate.swift:
Change the applicationDidEnterBackground() and applicationWillTerminate() methods to call saveData():
Xcode’s Stop button
Important note: When you press Xcode’s Stop button, the application delegate will not receive the applicationWillTerminate() notification. Xcode kills the app without mercy.
特别注意: 当你点击 Xcode 上面的 Stop 按钮时,应用程序将不会收到 applicationWillTerminate() 通知。Xcode 将毫不客气的干掉应用。
Therefore, to test the saving behavior, always simulate a tap on the home button to make the app go into the background before you press Stop. If you don’t press Shift+⌘+H first, you’ll lose your data. Caveat developer.
警告开发者
因此,如果想要测试保存行为,在按 Stop 之前问题要先按 Home 键使应用进后台。如果不先按 Shift+⌘+H,你将会丢失数据。
Improving the data model
Add a new file to the project using the Swift File template. Save it as DataModel.swift (you don’t need to make this a subclass of anything).
Change DataModel.swift to the following:
Cut the following methods out of AllListsViewController.swift and paste them into DataModel.swift:
- func documentsDirectory()
- func dataFilePath()
- func saveChecklists()
- func loadChecklists()
Add an init() method to DataModel.swift:
Also, you don’t have to call super.init() because DataModel does not have a superclass (it is not built on NSObject).
Switch to AllListsViewController.swift and make the following changes:
Remove the lists instance variable. Remove the init?(coder) method. Add a new instance variable:
Xcode still finds a number of errors in AllListsViewController.swift. You can no longer reference the lists variable directly, because it no longer exists. Instead, you’ll have to ask the DataModel for its lists property.
Everywhere the code for AllListsViewController says lists, replace this with dataModel.lists. You need to do this in the following methods:
- tableView(numberOfRowsInSection)
- tableView(cellForRowAtIndexPath)
- tableView(didSelectRowAtIndexPath)
- tableView(commitEditingStyle, forRowAtIndexPath)
- tableView(accessoryButtonTappedForRowWithIndexPath)
- listDetailViewController(didFinishAddingChecklist)
- listDetailViewController(didFinishEditingChecklist)
In AppDelegate.swift, add a new property:
Simplify the saveData() method to just this:
Crash
Change that method to:
Do a clean build (Product → Clean) and run the app. Verify that everything still works. Great!