DOC PREVIEW
SDSU CS 696 - Data

This preview shows page 1-2-3-4-26-27-28-53-54-55-56 out of 56 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 15 DataOct 14, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.References2Stanford iPhone Course, Winter 2010, CS193P - Lecture 9Beginning iPhone 3 Development: Exploring the iPhone SDK by Jeff LaMarche, and David Markhttp://www.sqlite.org/Local Data3Property Lists, NSUserDefaults & SettingsiPhone filesArchiving ObjectsSQLiteCore DataProperty Iists4Allowed data typesNSArrayNSDictionaryNSStringNSDataNSDateNSNumber (int, float, bool)FormatsXMLBinaryNSArray & NSDictionary Connivence methods5- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag; - (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag; - (id)initWithContentsOfFile:(NSString *)aPath; - (id)initWithContentsOfURL:(NSURL *)aURL;WritingReadingExample6NSArray * data = [[NSArray alloc] initWithObjects:@"Cat", @"Dog", nil];[data writeToFile:@"data.plist" atomically:YES];NSArray * readData = [[NSArray alloc] initWithContentsOfFile:@"data.plist"];NSLog(@"%@", readData);NSPropertyListSerialization7File formatMore descriptive errorsMutability+ (NSData *)dataFromPropertyList:(id)plistformat:(NSPropertyListFormat)format errorDescription:(NSString **)errorString;+ (id)propertyListFromData:(NSData *)datamutabilityOption:(NSPropertyListMutabilityOptions)opt format:(NSPropertyListFormat *)formaterrorDescription:(NSString **)errorString;Example8 NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; NSArray * data = [[NSArray alloc] initWithObjects:@"Cat", @"Dog", nil]; NSString * error; NSData * plist = [NSPropertyListSerialization dataFromPropertyList:(id)dataformat:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; if (plist) { [plist writeToFile: path atomically:YES]; } else { NSLog(@"%@",error); [error release]; }Archives9Reads/Writes objects to filesAny object that implement NSCode interfaceSupports numeric typesSupports network of objectsPerson Example10@interface Person : NSObject <NSCoding, NSCopying>{}@property (nonatomic, retain) NSString * name;@property (nonatomic, assign) int age;@endTwo data types to illustrate differencesNSCoding methods11- (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:name forKey:@"name"]; [encoder encodeInt:age forKey:@"age"]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.name = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"]; } return self;}NSCopying method12- (id)copyWithZone:(NSZone *)zone { Person *copy = [[[self class] allocWithZone: zone] init]; copy.name = [[self.name copyWithZone:zone] autorelease]; copy.age = self.age; return copy;}Archiving Single object13Person * roger = [Person new];roger.name = @"Roger";roger.age = 29;NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject: roger forKey: @"roger"]; [archiver finishEncoding]; [data writeToFile: @"PersonFile" atomically:YES]; [roger release]; [archiver release]; [data release];UnArching Object14NSData *data = [[NSMutableData alloc] initWithContentsOfFile: @"PersonFile"];NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];Person * recoveredPerson = [unarchiver decodeObjectForKey: @"roger"]; [unarchiver finishDecoding];[unarchiver release]; [data release];Multiple Objects15One archive can contain many different objectsAll objects are read from disk when archivingClass Versions16What happens when class changesAdds/removes instance variablesArchiving framework does not provide support for changesYour code has to deal with changesNew version of class reading data17- (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.name = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"];if ([decoder containsValueForKey: @"address"]) self.address = [decoder decodeObjectForKey:@"address"];elseself.address = nil; } return self;}File System18Each App has separate file system<Application Home>AppName.appAppNameMainWindow.nibDocumentsLibraryPreferencesCachestmpCan only read/write files in home directorySecurityPrivacyEase of removing appMostly backed up withiTunes syncFoundation Functions19Functions forAssertionsByte OrderingException handlingLogging output (NSLog)Managing File PathsetcFile Foundation Functions20NSString *homePath = NSHomeDirectory(); NSString *tmpPath = NSTemporaryDirectory();File Foundation Functions21NSString *fooPath = [documentsPath stringByAppendingPathComponent:@“foo.plist”];NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0];NSArray * NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);NSFileManager22More file operationsCreate files/directoriesMove filesCopy filesDirectory contentsWriteable Files & Nib23Files included in the nib are read onlyIf you want the file to be read/write copy it to users documents directoryChech on startup if file has been copiedSQLite24Free/Open sourceEmbedded in applicationCompact, fast, reliablehttp://www.sqlite.org/about.htmlStorage ClassesNULLINTEGERSigned integer in 1, 2, 3, 4, 6, or 8 bytesREAL8-byte IEEE floating point number TEXTText stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE)BLOBStored as it is entered into the databaseDynamic TypingAny column may be used to store a value of any storage classexcept an INTEGER PRIMARY KEY columnSo what happens when you insert text into an integer column?Each column has its preferred datatype (affinity)if data can be converted losslesslySQLite stores data using the column preferred datatypeelse stores type as it isSQLTypeSQLite AffinityINTINTEGERTINYINTSMALLINTMEDIUMINTBIGINTUNSIGNED BIG INTINT2INT8INTEGERCHARACTER(20)VARCHAR(255)VARYING CHARACTER(255)NCHAR(55)NATIVE CHARACTER(70)NVARCHAR(100)TEXTCLOBTEXTSQLTypeSQLite AffinityBLOBno datatype specifiedNONEREALDOUBLEDOUBLE PRECISIONFLOATREALNUMERICDECIMAL(10,5)BOOLEANDATEDATETIMENUMERICCREATE TABLE "main"."students" ("firstname" CHAR NOT NULL , "lastname" CHAR, "phone" CHAR, "code"


View Full Document

SDSU CS 696 - Data

Download Data
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?