Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
@邻家的橘猫
Iosswift
提交
73de9c62
I
Iosswift
项目概览
@邻家的橘猫
/
Iosswift
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Iosswift
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
73de9c62
编写于
3月 06, 2019
作者:
J
John Hampton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Persists Data
上级
a937823c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
3 deletion
+66
-3
iOSTemplate/Meal.swift
iOSTemplate/Meal.swift
+40
-1
iOSTemplate/MealTableViewController.swift
iOSTemplate/MealTableViewController.swift
+26
-2
未找到文件。
iOSTemplate/Meal.swift
浏览文件 @
73de9c62
...
...
@@ -6,13 +6,27 @@
//
import
UIKit
import
os
.
log
class
Meal
{
class
Meal
:
NSObject
,
NSCoding
{
// MARK: Properties
var
name
:
String
var
photo
:
UIImage
?
var
rating
:
Int
// MARK: Archiving Paths
static
let
DocumentsDirectory
=
FileManager
()
.
urls
(
for
:
.
documentDirectory
,
in
:
.
userDomainMask
)
.
first
!
static
let
ArchiveURL
=
DocumentsDirectory
.
appendingPathComponent
(
"meals"
)
// MARK: Types
struct
PropertyKey
{
static
let
name
=
"name"
static
let
photo
=
"photo"
static
let
rating
=
"rating"
}
// MARK: Initialization
...
...
@@ -32,4 +46,29 @@ class Meal {
self
.
photo
=
photo
self
.
rating
=
rating
}
// MARK: NSCoding
func
encode
(
with
aCoder
:
NSCoder
)
{
aCoder
.
encode
(
name
,
forKey
:
PropertyKey
.
name
)
aCoder
.
encode
(
photo
,
forKey
:
PropertyKey
.
photo
)
aCoder
.
encode
(
rating
,
forKey
:
PropertyKey
.
rating
)
}
required
convenience
init
?(
coder
aDecoder
:
NSCoder
)
{
// The name is required. If we cannot decode a name string, the initializer should fail.
guard
let
name
=
aDecoder
.
decodeObject
(
forKey
:
PropertyKey
.
name
)
as?
String
else
{
os_log
(
"Unable to decode the name for a Meal object."
,
log
:
OSLog
.
default
,
type
:
.
debug
)
return
nil
}
// Because photo is an optional property of Meal, just use conditional cast.
let
photo
=
aDecoder
.
decodeObject
(
forKey
:
PropertyKey
.
photo
)
as?
UIImage
let
rating
=
aDecoder
.
decodeInteger
(
forKey
:
PropertyKey
.
rating
)
// Must call designated initializer.
self
.
init
(
name
:
name
,
photo
:
photo
,
rating
:
rating
)
}
}
iOSTemplate/MealTableViewController.swift
浏览文件 @
73de9c62
...
...
@@ -19,8 +19,14 @@ class MealTableViewController: UITableViewController {
// Use the edit button item provided by the table view controller.
navigationItem
.
leftBarButtonItem
=
editButtonItem
// Load the sample data.
loadSampleMeals
()
// Load any saved meals, otherwise load sample data.
if
let
savedMeals
=
loadMeals
()
{
meals
+=
savedMeals
}
else
{
// Load the sample data.
loadSampleMeals
()
}
}
// MARK: - Table view data source
...
...
@@ -61,6 +67,7 @@ class MealTableViewController: UITableViewController {
if
editingStyle
==
.
delete
{
// Delete the row from the data source
meals
.
remove
(
at
:
indexPath
.
row
)
saveMeals
()
tableView
.
deleteRows
(
at
:
[
indexPath
],
with
:
.
fade
)
}
else
if
editingStyle
==
.
insert
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
...
...
@@ -118,6 +125,7 @@ class MealTableViewController: UITableViewController {
@IBAction
func
unwindToMealList
(
sender
:
UIStoryboardSegue
)
{
if
let
sourceViewController
=
sender
.
source
as?
MealViewController
,
let
meal
=
sourceViewController
.
meal
{
if
let
selectedIndexPath
=
tableView
.
indexPathForSelectedRow
{
// Update an existing meal.
meals
[
selectedIndexPath
.
row
]
=
meal
...
...
@@ -130,6 +138,9 @@ class MealTableViewController: UITableViewController {
meals
.
append
(
meal
)
tableView
.
insertRows
(
at
:
[
newIndexPath
],
with
:
.
automatic
)
}
// Save the meals.
saveMeals
()
}
}
...
...
@@ -154,4 +165,17 @@ class MealTableViewController: UITableViewController {
meals
+=
[
meal1
,
meal2
,
meal3
]
}
private
func
saveMeals
()
{
let
isSuccessfulSave
=
NSKeyedArchiver
.
archiveRootObject
(
meals
,
toFile
:
Meal
.
ArchiveURL
.
path
)
if
isSuccessfulSave
{
os_log
(
"Meals successfully saved."
,
log
:
OSLog
.
default
,
type
:
.
debug
)
}
else
{
os_log
(
"Failed to save meals..."
,
log
:
OSLog
.
default
,
type
:
.
error
)
}
}
private
func
loadMeals
()
->
[
Meal
]?
{
return
NSKeyedUnarchiver
.
unarchiveObject
(
withFile
:
Meal
.
ArchiveURL
.
path
)
as?
[
Meal
]
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录