Skip to Content
Swift游戏开发经典实例
book

Swift游戏开发经典实例

by Jonathon Manning, Paris Buttfield-Addison
January 2020
Intermediate to advanced
356 pages
6h 4m
Chinese
China Electric Power Press Ltd.
Content preview from Swift游戏开发经典实例
存储数据
159
4.
最后你又到
iPhone
上玩这个游戏,你惊讶地发现你的进度“丢失”了。你立马
删除了这个
App
,然后在苹果商店中给这个
App
打分为“
1
星”。这个
App
开发者也许就破产了,并且在贫民窟中孤独终老。
要解决这个问题,我们可以将数据保存在本地
UserDefaults
中,只有在和键值存
储中的数据进行对比之后才更新数据。在保存改变时,将它和本地
UserDefaults
中的值进行比较;如果键值存储中的值更小,将本地
NSUserDefault
中的值提交
到键值存储,覆盖旧值。如果键值存储中的值更大,将键值存储中的值覆盖本地
UserDefaults
中的值。在读取数据时,总是和本地存储中的值进行比较。
每个应用程序能够存储的键值存储被限制在
1MB
。如果你试图存储比这个更多的
数据,那么这个值将不会保存。每个值的限制为
1MB
,最多允许存储
1024
个值。
5.5
何时使用文件
何时使用数据库
问题
什么时候用文件,什么时候用数据库来存储数据?
解决方案
下列情况可以考虑文件存储:
我们需要一次读取文件的全部内容。
文件不大。
文件的处理和读取都非常简单,读取信息不会占用太多的
CPU
资源。
下列情况可以考虑数据库:
文件非常大,而且我们不需要一次加载所有内容。
我们仅需要访问文件中的少量信息。
我们需要快速加载文件中指定的部分内容。
在读取文件的过程中我们还需要修改它。
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron
FreeBSD® Unleashed

FreeBSD® Unleashed

Brian Tiemann, Michael C. Urban
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Swift学习手册

Swift学习手册

Jon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787519840129