Property Challenge - A question about my output


#1

I have the following output from my challenge solution - not entirely sure if this is what was requested, but is what I have produced.

[code]<?xml version="1.0" encoding="UTF-8"?>

Stock Portfolio e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2FydGYxMjY1XGNvY29hc3VicnRmMjEw CntcZm9udHRibFxmMFxmc3dpc3NcZmNoYXJzZXQwIEhlbHZldGljYTt9CntcY29sb3J0 Ymw7XHJlZDI1NVxncmVlbjI1NVxibHVlMjU1O30KXHBhcGVydzExOTAwXHBhcGVyaDE2 ODQwXG1hcmdsMTQ0MFxtYXJncjE0NDBcdmlld3cxMDgwMFx2aWV3aDg0MDBcdmlld2tp bmQwClxwYXJkXHR4NTY2XHR4MTEzM1x0eDE3MDBcdHgyMjY3XHR4MjgzNFx0eDM0MDFc dHgzOTY4XHR4NDUzNVx0eDUxMDJcdHg1NjY5XHR4NjIzNlx0eDY4MDNccGFyZGlybmF0 dXJhbAoKXGYwXGZzMjQgXGNmMCBub3cgaXMgdGhlIHRpbWUgZm9yIGFsbCBnb29kIG1l biB0byBnaXZlIHdoYXRldmVyIGl0IGlzIHRoZXkgd2FudCB0byBnaXZlXCc4NX0= 2014-12-13T20:41:09Z 1000 2.0099999904632568 shares 200 symbol AAPL shares 200 symbol GOOG [/code]

My question is … I know how to produce this, but how would I go about reading this into my code? Before the challenge it would be a simple for loop to read in the array of dictionaries, but how do you separate out the other items of the array?

Would it be simply to go through the array manually detecting the type of data held by each element in the array?

How would you detect what the object type is for each individual element in the array, for example…
Array item 1 is a NSString type, item 2 is a NSData type etc.?


#2

Reading it back in by hand would be a fun exercise to do. However, you can probably use one of NSArray’s convenience methods.

From NSArray Class Reference:

+ (NSArray *)arrayWithContentsOfFile:(NSString *)aPath
+ (NSArray *)arrayWithContentsOfURL:(NSURL *)aURL

See Also: 
- (BOOL)writeToFile:(NSString *)path
- (BOOL)writeToURL:(NSURL *)aURL

If those methods prove unhelpful, then you can summon NSPropertyListSerialization class to the rescue.


#3

This is an interesting question. I have attempted the below to try and read specific object types from an array fed by a property List but, for some reason fast enumeration does not yield the specific object types I am requesting, say for example in this case I am trying to log NSNUmber objects -only- (which I could then narrow down further to type via NSNumber methods such as [anNSNumber integerValue] or [anNSNumber floatValue], etc, but “for (NSNumber *n in inputPlist)” isn’t narrowing the output down to NSNumber objects, instead, it gives me ALL object types in the plist. The same is true if I specify NSStrings objects only like “for (NSString *s in inputPlist)”. Can someone explain the reason?. Is Serialization a better approach? I haven’t tried it yet but it seems to read from the plist directly?

[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"];
        for (NSNumber *a in inputPlist) {
            NSLog(@"%@", a);

        }

    
        }


return 0;

}[/code]


#4

Thanks for your thoughts guys. I’ll go over the ideas and suggestions. I’m sure I’ll have further questions.

Apologies for not getting back sooner, life got in the way :open_mouth: