Improvements, ideas and questions for challenge

Hello,

At first I was thinking that it would be a good idea to cast any type into an object and send that object to a method that will first get the object, then would try to find its type and finally add it to the pList as correctly.
Something like :


#import <Foundation/Foundation.h>

static NSMutableArray *arrayForPList;

void makeA8TypePList(id *p){
    if (type_of(p) == NSArray)
    {
        NSLog(@"Should add an array!");
        [arrayForPList addObjectsFromArray:anArray];
    } else if (type_of(p) ...
 
[........]
    
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        // Initialize if not already done 
        if (!arrayForPList) {
            arrayForPList = [[NSMutableArray alloc] init];
        }

        // Adding a NSDate to the pList
        NSDate *now = [NSDate date];
        makeA8TypePList(now);

[........]

        BOOL result = [arrayForPList writeToFile:@"/tmp/an8Type.plist"
                   atomically:YES];
        if (result) {
            NSLog(@"\n\n/tmp/an8Type.plist successfully written\n\n");
        } else {
            NSLog(@"\n\n/tmp/an8Type.plist not written.\n");
            return 1;
        }
    }
    return 0;
}

I did not succeeded in doing this. Maybe it is still beyond my current knowledge. Is there a way to simply “reference to/point at” an “anonymous” object and determine the type of this object later ?
Or maybe because it is already written and embedded somewhere in an API I do not know well enough or not at all ?

Then I wrote each type to be treated in the argument of the method but, as there is no default value in C, the message to the method was very long. Is there a way to do :

[code]
//instead of

makeA8TypePList(NULL, NULL, NULL, aData, NULL, NULL NULL, NULL);

//maybe something like ?

makeA8TypePList(aData:anotherData); [/code]

So finally I ended with this code that I do not find to be a state of the art and worse …


#import <Foundation/Foundation.h>

static NSMutableArray *arrayForPList;

void makeA8TypePList(NSArray *anArray){
    if (anArray)
    {
        NSLog(@"Should add an array!");
        [arrayForPList addObjectsFromArray:anArray];
    }
// At first I want to differentiate all types but the call to the method was too long : makeA8TypePList(NULL, NULL, NULL, aData, NULL, NULL NULL, NULL) 

//    , NSDictionary *aDic, NSString *aString, NSData *aData, NSDate *aDate, NSInteger *anInt, float *aFloat, bool *aBool)

//    if (aDic)
//    {
//        NSLog(@"Should add a dic!");
//        [arrayForPList addObject:aDic];
//    }
//    if (aString)
//    {
//        NSLog(@"Should add a string!");
//        [arrayForPList addObject:aString];
//    }
//    if (aData)
//    {
//        NSLog(@"Should add a Data!");
//        [arrayForPList addObject:aData];
//    }
//    if (aDate)
//    {
//        NSLog(@"Should add a date!");
//        [arrayForPList addObject:aDate];
//    }
//    if (anInt)
//    {
//        NSLog(@"Should add an Integer!");
//        [arrayForPList addObject:[NSNumber numberWithInteger:*anInt]];
//    }
//    if (aFloat)
//    {
//        NSLog(@"Should add a float!");
//        [arrayForPList addObject:[NSNumber numberWithFloat:*aFloat]];
//    }
//    if (aBool)
//    {
//        NSLog(@"Should add a bool!");
//        [arrayForPList addObject:[NSNumber numberWithBool:*aBool]];
//    }
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        // Initialize if not already done the array for pList
        if (!arrayForPList) {
            arrayForPList = [[NSMutableArray alloc] init];
        }
        // Create an array temp.
        NSMutableArray *garbageArray = [[NSMutableArray alloc] init];

        // Create a dictionnary
        NSMutableDictionary *stock;

        // Create an NSDate and add it to the temp array
        NSDate *now = [NSDate date];
        [garbageArray addObject:now];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
        [garbageArray addObject];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
        [garbageArray addObject:yesterday];

        // Create an array
        NSArray *arrayDeDate = @[now, tomorrow, yesterday];
        [garbageArray addObject:arrayDeDate];

        // Initiate the dictionnary
        stock = [NSMutableDictionary dictionary];
        // First time to see if the sotk underneath will be added to this object
        [garbageArray addObject:stock];
        [stock setObject:@"AAPL"
                  forKey:@"symbol"];
        [stock setObject:[NSNumber numberWithInt:200]
                  forKey:@"shares"];
        // Second time to check if there is a difference 
        [garbageArray addObject:stock];

        // Creating and adding a string
        [garbageArray addObject:@"This is a test!"];

        // Create an NSData to store
        NSURL *url = [NSURL URLWithString:
                        @"http://www.google.com/images/logos/ps_logo2.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:NULL
                                                         error:&error];


        //2 ways of doing it : INTEGER
        [garbageArray addObject:@1];
        //or
        NSInteger anInt = 2;
        [garbageArray addObject:[NSNumber numberWithInteger:anInt]];

        //2 ways of doing it : FLOAT
        [garbageArray addObject:@3.4];
        //or
        float aFloat = 5.6789;
        [garbageArray addObject:[NSNumber numberWithFloat:aFloat]];

        //2 ways of doing it : BOOLEAN
        [garbageArray addObject:[NSNumber numberWithBool:0]];
        //or
        bool aBool = TRUE;
        [garbageArray addObject:[NSNumber numberWithBool:aBool]];

        // Store the NSData at the end of the plist
        [garbageArray addObject:data];

        //Using the function just for fun
        makeA8TypePList(garbageArray);

        // Write the file
        BOOL result = [arrayForPList writeToFile:@"/tmp/an8Type.plist"
                   atomically:YES];
        if (result) {
            NSLog(@"\n\n/tmp/an8Type.plist successfully written\n\n");
        } else {
            NSLog(@"\n\n/tmp/an8Type.plist not written.\n");
            return 1;
        }
    }
    return 0;
}

At the end of it I wanted to read the pList I’ve just made like it is done in the book and I realised that I don’t know how as there is so much different data in that plist !!!
Could anyone give me a tip / hint that I can use to do this ?

Thanks by advance