views:

457

answers:

3

I'm having trouble with section 4 of the stanford iphone class on assignment 1b.

I am having trouble understanding how I will build the array and what the assignment expects.

Should the array be a "global" variable? Where should I define that? Will each of the other subfunctions add their variables to the array? Is the PrintIntrospectionInfo function just there to enumerate through and print all introspective info?

Where can I read up on the things they are asking for in this assignment? What should I read up on?

I'm not taking the class so can someone send me their code? The lessons build up on each other and I want to be able to proceed.

Here's the assignment :

Objective-C has a number of facilities that add to its dynamic object-oriented capabilities.  Many of these facilities deal with determining and using an object's capabilities at runtime.  Create a mutable array and add objects of various types to it. Create instance of the classes we’ve used elsewhere in this assignment to populate the array: NSString, NSURL, NSProcessInfo, NSDictionary, etc. Create some NSMutableString instances and put them in the array as well.
Feel free to create other kinds of objects also.
Iterate through the objects in the array and do the following: 1. Print the class name of the object. 2. Log if the object is member of class NSString. 3. Log if the object is kind of class NSString. 4. Log if the object responds to the selector "lowercaseString". Page 5 of 6 5. If the object does respond to the lowercaseString selector, log the result of asking the object to perform that selector (using performSelector:) CS193P Assignment 1B Spring 2009 Doll/Cannistraro

+1  A: 

The first assignment wasn't so much about design as it was about feeling around Objective-C. A global variable is fine, but a local variable would be much easier.

Malaxeur
In this case, how do I create the global variable? It's only a .m file.Also, what pdfs (and pages) should I read? Or online doc?
Neo42
It's very much like C if you're familiar with that. You can do something as simple as:NSArray * array; (or NSMutableArray if need be)
Malaxeur
Where? Outside the main function?
Neo42
You can place it there if you want, but anywhere really.
Malaxeur
Outside any function would be file scope—a global. Inside a function would be a local.
Peter Hosey
+2  A: 

You can find your answers in cocoa and objective-c manuals that come with Xcode.

#import <Foundation/Foundation.h>

void printIntrospectionInfo()
{
    NSMutableArray * array = [NSMutableArray arrayWithCapacity:5];
    [array addObject: [NSString stringWithString:@"Example NSString object"]];
    [array addObject: [NSMutableString stringWithString:@"Example NSMutableString object"]];
    [array addObject: [NSURL URLWithString:@"http://apple.com.au"]];
    [array addObject: [NSProcessInfo processInfo]];
    [array addObject: [NSDictionary dictionaryWithObject: @"DictObject" forKey: @"KeyObject"]];
    [array addObject: [NSNumber numberWithInt:123456]];

    SEL sel_lowercase = @selector(lowercaseString);

    int i;
    for (i = 0; i < [array count]; ++i)
    {
        id o = [array objectAtIndex:i];

        NSLog(@"%@", o);
        NSLog(@"Class name: %@", [[o class] className]);
        NSLog(@"Is Member of NSString: %@", ([o isMemberOfClass: [NSString class]] ? @"YES" : @"NO"));
        NSLog(@"Is Kind of NSString: %@", ([o isKindOfClass: [NSString class]] ? @"YES" : @"NO"));
        NSLog(@"Responds to lowercaseString: %@", ([o respondsToSelector: sel_lowercase] ? @"YES" : @"NO"));

        if ([o respondsToSelector: sel_lowercase])
            NSLog(@"lowercaseString: %@", [o performSelector: sel_lowercase]);

        NSLog(@"===================");
    }

}


int main(int argc, const char* argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    printIntrospectionInfo();
    [pool release];
    return 0;
}
stefanB
Good example. You probably want to replace [o respondsToSelector: @selector(lowercaseString)] with [o respondsToSelector: lowercase]
mahboudz
fixed .................
stefanB
Thank you! I see you interpreted the assignment differently. But that's great. It's a demonstration of exactly the kind of thing they want you to learn.
Neo42
+1  A: 

Where can I read up on the things they are asking for in this assignment? What should I read up on?

Aside from the local-vs.-global issue (locals are better), read the NSObject docs. All four questions pertain to things you'll do with methods implemented by NSObjects.

Don't forget to also read up on Objective-C.

Peter Hosey
That NSObject doc looks useful.
Neo42
All of the framework class references are. It's worth downloading the docs and then bookmarking your local copies of the Foundation and AppKit (Mac)/UIKit (iPhone) framework references for easy access from your browser.
Peter Hosey