Mac OS X and iOS operating systems
Lecture 7
Saving data in iOS
Tomasz Idzi Agenda
NSString and NSData
NSFileManager
Property List
JSON
Core Data
Mac OS X and iOS operating systems Tomasz Idzi iOS Directories
App
Documents
Inbox Library
Caches
temp
Mac OS X and iOS operating systems Tomasz Idzi iOS Directories
Directory Description Backed up by iTunes
AppName.app This directory contains the app and all of its resources. NO
Documents/ Use this directory to store user-generated content. YES
Use this directory to access files that your app was asked to open Documents/Inbox YES by outside entities. Such as mail’s attachments.
This is the top-level directory for any files that are not user data Library YES files.
Library/Caches Subfolder of Library for any caching files. NO
Use this directory to write temporary files that don’t need to persist Tmp/ NO between launches of your app.
Mac OS X and iOS operating systems Tomasz Idzi Accessing File Paths
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths firstObject]; NSString *path = [docPath stringByAppendingComponent:@”myFile.txt”];
Mac OS X and iOS operating systems Tomasz Idzi Writing NSData
BOOL success = [data writeToFile:path options:NSDataWritingAtomic error:&error];
NSDataWritingAtomic - create temp file first
NSDataWritingWithoutOverwriting - prevent overwriting an existing file
Mac OS X and iOS operating systems Tomasz Idzi Reading NSData
[NSData alloc] initWithContentsOfFile: path options:NSDataReadingMappedIfSafe error:&error];
NSDataReadingMappedIfSafe - file should be mapped into virtual memory, if possible and safe
NSDataReadingUncached - file should not be stored in the file-system caches
NSDataReadingMappedAlways - map the file, if possible.
Mac OS X and iOS operating systems Tomasz Idzi Writing NSString
BOOL success = [myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSUTF8StringEncoding - 8-bit representation of Unicode characters
NSASCIIStringEncoding - Strict 7-bit ASCII encoding within 8-bit chars.
Mac OS X and iOS operating systems Tomasz Idzi Reading NSString
[NSData alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
Mac OS X and iOS operating systems Tomasz Idzi Writing/Reading NSString and NSData
DEMO
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
Providing a convenient way to:
create
read
move
copy
delete
files and directories.
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDir ectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"file.txt"];
BOOL fileExists = [fileManager fileExistsAtPath:filePath];
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager - list of files
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSArray *files = [fileManager contentsOfDirectoryAtURL: [urls firstObject] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:&error];
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager - list of files
[fileManager contentsOfDirectoryAtURL:[urls firstObject] includingPropertiesForKeys:nil options:NSFileBusy error:&error];
NSFileAppendOnly - The key in a file attribute dictionary whose value indicates whether the file is read-only.
NSFileBusy - The key in a file attribute dictionary whose value indicates whether the file is busy.
NSFileCreationDate - The key in a file attribute dictionary whose value indicates the file's creation date
NSFileType - The key in a file attribute dictionary whose value indicates the file's type.
NSDirectoryEnumerationSkipsHiddenFiles - Do not enumerate hidden files.
. . .
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager - list of files
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension == 'png'"]; for (NSURL *fileURL in [contents filteredArrayUsingPredicate:predicate]) { // Enumerate each .png file in directory }
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager - create/delete directory NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagesPath = [documentsPath stringByAppendingPathComponent:@"images"]; if (![fileManager fileExistsAtPath:imagesPath]) { [fileManager createDirectoryAtPath:imagesPath withIntermediateDirectories:NO attributes:nil error:nil]; }
BOOL deleteSucess = [fileManager removeItemAtPath:imagesPath error:&error];
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager - copy/move directory
BOOL success = [fileManager moveItemAtPath:source toPath:destination error:&error];
BOOL success = [fileManager copyItemAtURL:source toURL:destination error:&error];
Mac OS X and iOS operating systems Tomasz Idzi NSFileManager
DEMO
Mac OS X and iOS operating systems Tomasz Idzi Property List
Mac OS X and iOS operating systems Tomasz Idzi Property List
XML Property List
Standard Property List
Editable by hand
Device Independent
Binary Property List
Fast loading
ASCII Legacy Property List
Read only
Mac OS X and iOS operating systems Tomasz Idzi Property List - supported types
Abstract type XML element Cocoa class Core Foundation type
array
dictionary
string
data NSData CFData
date
number - integer
number - floating point
NSNumber (boolValue == Boolean
Mac OS X and iOS operating systems Tomasz Idzi Property List - reading
[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:&error];
NSPropertyListImmutable - Specifies that the property list should be immutable.
NSPropertyListMutableContainers - Causes the returned property list to have mutable containers but immutable leaves.
NSPropertyListMutableContainersAndLeaves - Causes the returned property list to have mutable containers and leaves.
Mac OS X and iOS operating systems Tomasz Idzi Property List - reading
[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:&error];
NSPropertyListOpenStepFormat - Specifies the ASCII property list format inherited from the OpenStep APIs.
NSPropertyListXMLFormat_v1_0 - Specifies the XML property list format.
NSPropertyListBinaryFormat_v1_0 - Specifies the binary property list format.
Mac OS X and iOS operating systems Tomasz Idzi Property List - writing
[NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
Property list must have one object as the root object.
The option parameter is currently unused.
Unsupported types will fail with an error.
Mac OS X and iOS operating systems Tomasz Idzi Property List
DEMO
Mac OS X and iOS operating systems Tomasz Idzi JSON
JSON stands for JavaScript Object Notation.
It is a human readable format used to transmit data.
JSON is formatted in attribute-value pairs.
It is an alternative to XML for sending data across the network.
Mac OS X and iOS operating systems Tomasz Idzi JSON - types
Primitive JavaScript Types: String, Boolean, Number, Null
Arrays are indicated by brackets - []
Objects are indicated by braces - {}
Objects are collection of name-value pairs.
Use commas to add additional fields.
Mac OS X and iOS operating systems Tomasz Idzi JSON - NSJSONSerialization
Convert JSON to foundation objects or can convert foundation to JSON
The top level object is an NSArray or an NSDictionary
All objects are insentence of NSString, NSNumber, NSArray, NSDictionary or NSNull
All dictionary keys are NSString
Mac OS X and iOS operating systems Tomasz Idzi JSON - Decoding
+JSONObjectWithData:options:error:
+JSONObjectWithStream:options:error:
Mac OS X and iOS operating systems Tomasz Idzi JSON - Encoding
+dataWithJSONObject:options:error
+writeJSONObject:toStream:options:error
Mac OS X and iOS operating systems Tomasz Idzi JSON
DEMO
Mac OS X and iOS operating systems Tomasz Idzi Core Data
Managed Object Context
Presistent Store
Managed Objects
Modeling Objects
Fetch Request
Mac OS X and iOS operating systems Tomasz Idzi