Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Exercises

1.Write a category method on NSString that reverses the order of words in a string. Your -reverseWords category method should turn the string “dog bites man” into the string “man bites dog.” (The original string is the receiver and the reversed string is the method’s return value.) Write a small program to test –reverseWords.
2.Write a class that has an extension and a program to test it. (Your class doesn’t have to do anything useful, you can just put in printf or NSLog statements for the method bodies.)
  • Temporarily omit the implementation of the method that you declare in the extension. Verify that the compiler warns you of the omission.

  • Add the implementation of the extension method and try to call it from the test program. Verify that the compiler warns you the object may not respond to the method. Then notice that you can successfully call the method anyway.

3.Verify the instance variable scope rules. Define a class that has one each of @private, @protected, and @public instance variables. Then:
  • Try to access each instance variable directly (object->instance variable) from outside the class.

  • Create a subclass of the original class. Try to access each instance variable from within a subclass method.

4.Write a category that snoops on the @private instance variable in the class you defined in Exercise 2. Use the approach shown in Listings 11.7 and 11.8.
5.Download class-dump (www.codethecode.com/projects/class-dump). Run it against some of the Objective-C programs that you have written for the exercises in this book to see what its output looks like. class-dump is a command line program that you run from a Terminal window with this command:
class-dump executable name

If you have been using Xcode, the executable for a project has the same name as the project and it is located in the build/[configuration name] directory inside the project directory. The default configuration name is Debug so unless you have created a different configuration, the executable will be in the build/Debug subdirectory of your project directory.

 


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint