This is my solution. Though it meets the challenge, I am still puzzled like many here on how to properly distinguish objects within a plist for reading. Specifying id type with fast enumeration does not yield specific object types, but the entire array of objects instead. My thinking was to use fast enumeration and discern each object type before logging as shown here but it does not work (it catches an exception), I believe because the methods (e.g. integerValue or floatValue) are being sent to ALL objects and not NSNumber type objects specifically. If anyone can shed some light on how to properly parse through a plist it would be most welcomed!
Parsing through a plist (FAILED!)
[code] // READ Plist
NSArray *inputPlist = [NSArray arrayWithContentsOfFile:@"/tmp/aPropertyList.plist"];
for (NSNumber *a in inputPlist) {
if (a.integerValue) {
NSLog(@“Here’s are some integers: %ld”, a.integerValue);
}
if (a.floatValue) {
NSLog(@"Here are some floats: %f", a.floatValue);
}
if (a.boolValue) {
NSLog(@"Here is a BOOL: %hhd", a.boolValue);
}
for (NSString *s in inputPlist) {
NSLog(@"Here are some strings: %@", s);
}
for (NSMutableArray *a in inputPlist) {
NSLog(@"Here are some arrays: %@", a);
}
for (NSArray *a in inputPlist) {
NSLog(@"Here's an array: %@", a);
}
}[/code] Obviously an inexperienced attempt. There must be a better way...
Challenge Solution:
[code]//
// main.m
// ObjC_PropertyLists_with-all-8-datatypes - Challenge
//
#import <Foundation/Foundation.h>
/// PROPERTY LISTS - writing a Property list that has all 8 types in it: array, dictionary, string, data, date, integer, float, and boolean ///
int main(int argc, const char * argv[]) {
@autoreleasepool {
// BUILD a container aray to house the various objects
//NSMutableArray *mainArray = [NSMutableArray alloc];
// an Array
NSArray *anArray = @[@"string1", @"string2", @"string3"];
// a Dictionary (literal) with various object types
NSDictionary *aDictionary = @{ @"aFloat" : @1.2,
@"anInteger1" : [NSNumber numberWithInt:122],
@"anInteger2" : @65,
@"aString" : @"Hello World",
@"anArray" : @[@"Yellow", @"Red", @"Blue"],
};
// a String
NSString *aString = @"This is a String";
// a Date
NSDate *aDate = [NSDate date];
// an Integer
NSNumber *anInt = @10;
// a Float
NSNumber *aFloat = [NSNumber numberWithFloat:3.1345];
//a BOOLean
NSNumber *aBool = [NSNumber numberWithBool:YES];
// an NSData
NSData *aData = [NSData dataWithContentsOfFile:@"/tmp/nsdatadummy.log"];
// an NSString
NSString *aStringFromData = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding]; // string with data (used to display nsdata as string instead of hex)
// ADD ALL objects TO array container
NSMutableArray *mainArray = [[NSMutableArray alloc] initWithObjects:anArray, aDictionary, aString, aDate, anInt, aFloat, aBool, aData, aStringFromData, nil];
// WRITE array container to plist
BOOL written = [mainArray writeToFile:@"/tmp/aPropertyList.plist"
atomically:YES];
if (!written) {
NSLog(@"Plist not written, exiting..");
return 1;
}
// READ Plist
NSArray *inputPlist = [NSArray arrayWithContentsOfFile:@"/tmp/aPropertyList.plist"];
NSLog(@"%@", inputPlist);
}
return 0;
}[/code]