代写 C html Java javascript SQL swift XML concurrency database graph security Announcements

Announcements
• Lab 3 is due on Wednesday by 11:59 PM • Lab 4 is due on Monday October 21st
1E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 1
Today’s Topics
• Property Lists
• iPhone’s File System • Archiving Objects
• SQLite
• Web Services
2E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 2

Storage on the iPhone
Property Lists
3E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 3
Property Lists
• Convenient way to store a small amount of data – Arrays, dictionaries, strings, numbers, dates, raw
data
– Human-readable XML or binary format
• UserDefaults class uses property lists under the hood
4E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 4

When Not to Use Property Lists
• More than a few hundred KB of data – Loading a property list is all-or-nothing
• Complex object graphs • Custom object types
5E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 5
Property List Demo
6E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 6

iPhone’s File System
7E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 7
Keeping Applications Separate
8E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 8

Why Keep Applications Separate?
• Security
• Privacy
• Cleanup after deleting an app
9E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 9
Home Directory Layout
• Each app has its own set of directories
– MyApp.app
• MyApp
• MainViewController.storyboard • SomeImage.png
– Documents – Library
• Caches
• Preferences
• Applications only read and write within their home directory
10E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 10

Demo
11E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinlicgatPiolnatDfoevrmelopment 11
File Paths in Your Application
// Basic directories
let homeDir = NSHomeDirectory()
let tmpDir = NSTemporaryDirectory()
// Documents directory
let paths =
NSSearchPathForDirectoriesInDomains(. documentDirectory,
let documentsPath = paths[0]
// /Documents/foo.plist
let fooPath = docsDir + “/foo.plist”
.userDomainMask, true)
12E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 12

Including Writable Files with Your App
• Many applications want to include some starter data • But application bundles are code signed
– You can’t modify the contents of your app bundle
• To include a writable data file with your app… – Build it as part of your app bundle
– On first launch, copy it to your Documents directory
13E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 13
Archiving Objects
14E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 14

Archiving Objects
• Next logical step from property lists – Include arbitrary classes
– Complex object graphs
• Utilize the Codable protocol introduced in Swift 4
15E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 15
Archiving Example
struct Book: Codable { var title: String
var author: String var pageCount: Int
enum CodingKeys: String, CodingKey {
} case title; case author; case pageCount
init(title: String, author: String, pageCount: Int) {
} self.title = title; self.author = author; self.pageCount = pageCount
func encode( to encoder: Encoder) throws { }…
init(from decoder: Decoder) throws { }}…
16E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 16

Archiving Example
func encode (to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(title, forKey: “title”)
try container.encode(author, forKey: “author”)
try container.encode(pageCount, forKey: “pageCount”)
}
17E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 17
Archiving Example
init (from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) title = try container.decode(String.self, forKey: .title)
author = try container.decode(String.self, forKey: .author) pageCount = try container.decode(Int.self, forKey: .pageCount)
}
18E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 18

Archiving & Unarchiving using Codable
• Creating an archive
let data = try PropertyListEncoder().encode(books)
let success = NSKeyedArchiver.archiveRootObject(data, toFile:”path/to/archve”) • Decoding an archive
guard let data= NSKeyedUnarchiver.unarchiveObject(withFile: “path/to/archive”) as? Data else { return }
let books = try PropertyListDecoder.decode([Book].self,from data)
19E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 19
SQLite
20E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 20

SQLite
• Complete SQL database in an ordinary file • Simple, compact, fast, reliable
• No server
• Great for embedded devices – Included on the iPhone platform
21E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 21
When Not to Use SQLite
• Multi-gigabyte databases
• High concurrency (multiple writers) • Client-server applications
• 􏰀Appropriate Uses for SQLite􏰁 http://www.sqlite.org/whentouse.html
22E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 22

More on SQLite
• 􏰀SQLite in 5 Minutes Or Less􏰁
– http://www.sqlite.org/quickstart.html
• 􏰀Intro to the SQLite C Interface􏰁 – http://www.sqlite.org/cintro.html
• FMDB SQLite Wrapper
– https://github.com/ccgus/fmdb
23E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 23
SQLite Demo – FMDB
24E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 24

Core Data
• Object-graph management and persistence framework
– Makes it easy to save and load model objects
• Properties
• Relationships
– Higher-level abstraction than SQLite or property lists
25E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 25
Web Services
26E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 26

Your Application & The Cloud
• Store & access remote data
• May be under your control or someone else’s
• Many Web 2.0 apps/sites provide developer API
27E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 27
Integrating with Web Services
• Non-goal of this class: teach you all about web services
– Plenty of tutorials accessible, search on Google
• Many are exposed with XML or JSON
• High level overview of parsing these types of data
28E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 28

XML
29E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 29
Options for Parsing XML
• XMLParser
– Event-driven API
– https://developer.apple.com/reference/foundation
/xmlparser
• SWXMLHash
– Simple Swift XML Parsing
• https://github.com/drmohundro/SWXMLHash
30E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 30

JSON
31E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 31
JavaScript Object Notation
• More lightweight than XML
• Looks a lot like a property list
– Arrays, dictionaries, strings, numbers
• Open source json-framework wrappers for Swift and Objective-C
32E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 32

What does a JSON string look like?
{
}
􏰀instructor􏰁 : 􏰀Todd Sproull􏰁, 􏰀students􏰁 : 20,
􏰀itunes-u􏰁 : true, 􏰀midterm-exam􏰁 : null, 􏰀assignments􏰁 : [ 􏰀WhatATool􏰁,
􏰀HelloPoly􏰁]
33E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 33
More on JSON
• 􏰀Introducing JSON􏰁
– http://www.json.org/
• Encoding and Decoding Custom Types
– https://developer.apple.com/documentation/foundati on/archives_and_serialization/encoding_and_decodin g_custom_types
• JSON Editor
– https://www.jsoneditoronline.org/
34E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 34

Recap
• Property lists
– Quick & easy, but limited
• Archived objects
– More flexible, but require writing a lot of code
• SQLite and Core Data
– Elegant solution for many types of problems
• XML and JSON
– Low-overhead options for talking to 􏰀the cloud􏰁
35E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 35
JSON Demo
36E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 36

More on JSON
• 􏰀Introducing JSON􏰁
– http://www.json.org/
• SwiftyJSON
– https://github.com/SwiftyJSON/SwiftyJSON
• JSON Editor
– http://www.jsoneditoronline.org/
37E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 37
Recap
• Property lists
– Quick & easy, but limited
• Archived objects
– More flexible, but require writing a lot of code
• SQLite and Core Data
– Elegant solution for many types of problems
• XML and JSON
– Low-overhead options for talking to 􏰀the cloud􏰁
38E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 38

Firebase Demo
39E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 39
Cloud Firestore
40E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 40