Sarkha
1
Hello,
Below is my code for the challenge seems to run nicely
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *challange = [[NSMutableArray alloc] init];
// Array
NSArray *itemArray = [[NSArray alloc] initWithObjects:@"Hyper",@"Little",@"Thingy", nil];
[challange addObject:itemArray];
// Dictionary
NSDictionary *sayWhat = @{@"Mind":@"like water",@"Water":@"Under the bridge"};
[challange addObject:sayWhat];
// String
NSString *wowzers = @"Batman";
[challange addObject:wowzers];
// Data
NSData *youGotMe = [NSData dataWithContentsOfFile:@"<pulled the path for the cool.txt file created somewhere in this book>"];
[challange addObject];
// Date
NSDate *hereComesToday = [NSDate date];
[challange addObject:hereComesToday];
// Integer
NSNumber *intMyBent = [NSNumber numberWithInt:100];
[challange addObject:intMyBent];
// Float
NSNumber *floatMyboat = [NSNumber numberWithFloat:10010.10010];
[challange addObject:floatMyboat];
// Boolean
NSNumber *bohemian = [NSNumber numberWithBool:NO];
[challange addObject];
// write to a file oooooh
[challange writeToFile:@"<Where you want it to go>/Challange30.plist"
atomically:YES];
}
return 0;
}[/code]
I added my code to the chapter’s example:
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *stocks = [[NSMutableArray alloc] init];
NSMutableDictionary *stock;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[comps setYear:2012];
[comps setDay:15];
[comps setMonth:07];
[comps setHour:12];
[comps setMinute:37];
[comps setSecond:5];
stock = [NSMutableDictionary dictionary];
[stock setObject:@"AAPL" forKey:@"symbol"];
[stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"];
[stock setObject:[NSNumber numberWithBool:YES] forKey:@"futureBuyDecision"];
[stock setObject:[NSNumber numberWithFloat:6.5] forKey:@"returnInPercent"];
[stock setObject:[cal dateFromComponents:comps] forKey:@"dateOfPurchase"];
[stock setObject:[NSData dataWithContentsOfFile:@"/tmp/apple-stockz-description.txt"] forKey:@"companyDescription"];
[stocks addObject:stock];
[comps setYear:2013];
[comps setDay:2];
[comps setMonth:11];
[comps setHour:9];
[comps setMinute:55];
[comps setSecond:43];
stock = [NSMutableDictionary dictionary];
[stock setObject:@"GOOG" forKey:@"symbol"];
[stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"];
[stock setObject:[NSNumber numberWithBool:NO] forKey:@"futureBuyDecision"];
[stock setObject:[NSNumber numberWithFloat:-2.8] forKey:@"returnInPercent"];
[stock setObject:[cal dateFromComponents:comps] forKey:@"dateOfPurchase"];
[stock setObject:[NSData dataWithContentsOfFile:@"/tmp/google-stockz-description.txt"] forKey:@"companyDescription"];
[stocks addObject:stock];
[stocks writeToFile:@"/tmp/stocks.plist" atomically:YES];
NSArray *stockList = [NSArray arrayWithContentsOfFile:@"/tmp/stocks.plist"];
for (NSDictionary *d in stockList) {
NSLog(@"I have %@ shares of %@", [d objectForKey:@"shares"], [d objectForKey:@"symbol"]);
}
}
return 0;
}[/code]
Hi, this was my approach for the Challenge from all types. It prints every value from the property list.
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *allTypes = [NSMutableArray array];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"This is an example" forKey:@"Dictionary"];
NSString *string = @"This is a string";
NSData *data = [[NSData alloc] initWithContentsOfFile:@"/tmp/cool.txt"];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDate *date = [NSDate date];
NSNumber *integrr = @5;
NSNumber *floatrr = @10.4;
NSNumber *boolT = [[NSNumber alloc] initWithBool:YES];
[allTypes addObject:dic];
[allTypes addObject:string];
[allTypes addObject:dataString];
[allTypes addObject:date];
[allTypes addObject:integrr];
[allTypes addObject:floatrr];
[allTypes addObject:boolT];
[allTypes writeToFile:@"/tmp/plistEx.plist" atomically:YES];
NSArray * dataPlist = [[NSArray alloc] initWithContentsOfFile:@"/tmp/plistEx.plist"];
NSLog(@"%@", dataPlist);
}
return 0;
}
[/code]
Can we write to .plist files using types other than NSMutableArray?
You can use NSDictionary and NSSet as well.