Widgets Exercise 19.1 - 19.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 16, 2014, 09:37:52 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 19
| | | |-+ Exercise 19.1 - 19.3
Pages: [1] Go Down
Author Topic: Exercise 19.1 - 19.3 (Read 1129 times)
Posts: 23

on: February 04, 2012, 09:36:56 PM

19.1 and 19.2 incorrectly ask us to use a prime displaying program in program 15.7. 15.7 is a simple 0-9 display program so I used that instead.

Code: (Objective-C)
#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        NSMutableArray  *numbers = [NSMutableArray array];
        NSNumber        *myNumber;
        int             i;
        // Create an array with the numbers 0-9
        for (i = 0; i < 10; ++i) {
            myNumber = [NSNumber numberWithInteger: i];
            [numbers addObject: myNumber];
        // Archive it in an XML Property List
        if ([numbers writeToFile: @"" atomically: YES] == NO)
            NSLog(@"Save to file failed");
        // Exercise 19.2
        // Read from the XML Property List
        NSArray         *numbersXML = [NSArray arrayWithContentsOfFile: @""];
        if (numbersXML) {
            NSLog(@"Reading from XML Property List");
            for ( NSString *integer in numbersXML )
                NSLog(@"%@", integer);
        else {
            NSLog(@"Could not find XML Property List");
        // Exercise 19.3
        // OS X does not seem to read files from /Library/Preferences (probably for security). This code works if the .plist file is in the directory of the exercise executable
//      NSString        *file = @"~/Library/Preferences/AddressBookMe.plist";  // this doesn't work
        NSString        *file = @"AddressBookMe.plist";
        NSDictionary    *plist = [NSDictionary dictionaryWithContentsOfFile: file];
        if (plist) {
            NSLog(@"<< Contents of %@ >>", file);
            for ( NSDictionary *key in plist)
                NSLog(@"%@: %@", key, [plist objectForKey: key]);
            NSLog(@"Could not read file %@.", file);
    return 0;
Posts: 13

Reply #1 on: May 09, 2012, 04:54:04 PM

Regarding Exercise 19.1:

The "Corrections to First Printing" for the 4th Edition states:


Chapter 19 - Exercise 1 (Page 441)
Change the exercise to read:
"In Chapter 6, Program 6.10A generated a table of prime numbers. Modify that program to store the numbers inside an NSMutableArray object. Then write the resulting array as an XML property list to the file Examine the contents of the file."


Regarding Exercise 19.3:

The exercise actually asks you to read a .plist file from the /Library/Preferences folder.

I am able to read the .plist files in my Mac's /Library/Preferences folder with my Exercise 19.3 just fine. (My user account is an admin account.)

Moreover, I believe these .plist files in/Library/Preferences are all "read only" for non-admin users, so one would think the program would be able to read them, even as a non-admin user.

tknp states that reading from his Home directory ("~/Library/Preferences/AddressBookMe.plist") doesn't work.

The file read also fails for me if I use the "~" character for the path to my home directory.

However, using "/Users/user_short_name/Library/Preferences/AddressBook,plist" works OK.

So it looks like you have to use the full path to point to your home directory, i.e. not the shortcut "~".


Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 All rights reserved.