-1

So, I'm in a OS, Xcode application, in Objective-C code. Here's what I have so far:

#import <Foundation/Foundation.h>

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

        /// IN THIS CHALLENGE, I HAVE TO MAKE MY OWN PLIST
        /// IT HAS TO INCLUDE ALL 8 TYPES: ARRAY, DICTIONARY, STRING, DATA, DATE, INTEGER, FLOAT, AND BOOLEAN...

        // Add the elements
        NSMutableArray *array;
        NSMutableDictionary *dictionary;
        NSString *string;
        NSData *data;
        NSDate *date;
        int integer;
        float floating;
        BOOL boolean;


        // Set up the elements
        [array addObject:[NSString stringWithFormat:@"\"Array\""]];
        [dictionary setObject:@"\"Dictionary\""
                       forKey:@"dic"];
        string = [NSString stringWithFormat:@"\"String\""];
        data = [NSData dataWithContentsOfFile:@"/tmp/cool.text"];
        date = [NSDate date];
        integer = 50;
        floating = 50.5;
        boolean = YES;


        // Log them out
        NSLog(@"Array: %@\n\n", array);
        NSLog(@"Dictionary: %@\n\n", dictionary);
        NSLog(@"String: %@\n\n", string);
        NSLog(@"Data: %@\n\n", data);
        NSLog(@"Date: %@\n\n", date);
        NSLog(@"Integer: %d\n\n", integer);
        NSLog(@"Float: %f\n\n", floating);
        NSLog(@"Boolean: %hhd\n\n", boolean);

    }
    return 0;
}

So, what this "challenge" (from a coding book I'm following) is supposed to do, is print out the values of each of these in the console. But, I have a problem here. When I run the application, the log prints out this:

2015-05-08 14:48:23.588 CHALLENGE 7. Plist[21817:1995500] Array: (null)

2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] Dictionary: (null)

2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] String: "String"

2015-05-08 14:48:23.590 CHALLENGE 7. Plist[21817:1995500] Data: <43687269 73746961 6e206973 20636f6f 6c210a43 68726973 7469616e 20697320 636f6f6c 210a4368 72697374 69616e20 69732063 6f6f6c21 0a436872 69737469 616e2069 7320636f 6f6c210a 43687269 73746961 6e206973 20636f6f 6c210a43 68726973 7469616e 20697320 636f6f6c 210a4368 72697374 69616e20 69732063 6f6f6c21 0a436872 69737469 616e2069 7320636f 6f6c210a 43687269 73746961 6e206973 20636f6f 6c210a43 68726973 7469616e 20697320 636f6f6c 21>

2015-05-08 14:48:23.596 CHALLENGE 7. Plist[21817:1995500] Date: 2015-05-08 19:48:23 +0000

2015-05-08 14:48:23.596 CHALLENGE 7. Plist[21817:1995500] Integer: 50

2015-05-08 14:48:23.596 CHALLENGE 7. Plist[21817:1995500] Float: 50.500000

2015-05-08 14:48:23.597 CHALLENGE 7. Plist[21817:1995500] Boolean: 1

So, this is OK, but I'd like some help on how to use the NSLog() to print out the values correctly. As you can see, both the array and the dictionary are printed out as (null), or rather, with no value. And the data from a file containing the words "Random Text" doesn't print out the String value. It seems to print out the non-human-readable value of the file itself. Is it because I used the %@ token with these? If so, what should I use?

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Christian Kreiter
  • 610
  • 1
  • 5
  • 16
  • 3
    They are printing out as `(null)` because they are `nil`. You never initialize them. – rmaddy May 08 '15 at 20:01
  • `(null)` in an NSLog output means that the pointer is `nil`. Remember that Objective-C will happily no-op many operations on nil pointers. – Hot Licks May 08 '15 at 20:04
  • You don't need all the escaped `\"`. Example: `[array addObject:@"Array"];` and `[dictionary setObject:@"Dictionary" forKey:@"dic"];` or using literal syntax: `dictionary[@"dic"] = @"Dictionary";`. – zaph May 08 '15 at 20:28
  • Actually, your file apparently contains the characters "Christian is cool!...". Google "ASCII table". – Hot Licks May 08 '15 at 20:33
  • Now what does this have to do with a property list? – Christian Schnorr May 08 '15 at 20:58
  • @HotLicks Yes, it does say that. I merely put in "Random Text" so I wouldn't have to specify the text. It was quicker. – Christian Kreiter May 09 '15 at 03:07
  • My point is that your data was displayed. You displayed the `description` of the NSData object. – Hot Licks May 09 '15 at 10:59

1 Answers1

1

You have to initialize the arrays before you add anything to it.

NSMutableArray *array = [[NSMutableArray alloc] init];
Jessica
  • 9,379
  • 14
  • 65
  • 136
  • More properly, you must *create* an *object* before you can operate on it. There is no need to "initialize" a pointer by assigning an `alloc/init` result to it if you are subsequently going to assign the address of a different object to that pointer. – Hot Licks May 08 '15 at 20:35
  • Umm, Jessica, I'm a little confused. When I **addObject:** I thought that was initializing it and, really, filling it with content. Same with my dictionary's **setObject:forKey:**. – Christian Kreiter May 09 '15 at 03:10
  • @ChristianKRider - `addObject` is a method that operates on an existing NSArray, to add an object to it. If the array doesn't exist (pointer is `nil`) then it's a no-op. – Hot Licks May 09 '15 at 11:00
  • @ChristianKRider - Again, you don't seem to understand the basic concepts of pointers and objects. – Hot Licks May 09 '15 at 11:02
  • @ChristianKRider Buy this book: https://www.bignerdranch.com/we-write/objective-c-programming/ It's really helpful! Good luck!! – Jessica May 10 '15 at 03:45