1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Help with Amateur Coder :/

Discussion in 'Mac Programming' started by afornander, Apr 9, 2009.

  1. macrumors 6502

    hi everybody, im really trying to learn Objective-C and i am having a hard time.

    This is my Bajilianth time trying to learn and i really dont want to give up this time because i think i can do it... anyway, this is what i am doing

    #import <Foundation/Foundation.h>
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	NSMutableDictionary * PHBookmark = [[PHBookmark:NSString] retain];
    [QUOTE]syntax error before NSString[/QUOTE]
    	PHBookmark = [NSMutableDictionary];
    [QUOTE]syntax error before "]" String[/QUOTE]
    	NSString * PHStanU = @"Stanfard University";
    	PHStanU = [PHBookmark "http://StanfardUniversity"];
    [QUOTE]syntax error before string constant[/QUOTE]
        NSLog(@"Hello, World!");
        [pool drain];
        return 0;
    i am Partialy using Stanfard U on itunes and also using CocoaDevCentral for more help. i am trying to do this: http://www.stanford.edu/class/cs193p/downloads/Assignment1B.pdf

    FYI: i am not a stanford student i am just using there free lessons. i am not being graded or getting credit so i am not cheating on anything

    Any help would be amazing, thanks guys :D
  2. macrumors newbie

    what section are you having trouble with? I am not a stanford student either, just along with the class. i haven't completed part 4 but my other parts are finished and i believe them to be correct.
  3. macrumors 6502

    I dont understand how to Make a NSMutableDictionary. thats really the brick wall that i have hit at the moment, i have tried a lot of different ways to go about it but i just cant seem to get it. :confused:

    (thanks BTW)
  4. Moderator emeritus


    Well there are several syntax errors. Have you began learning the Objective-C language first? Those PDFs don't seem to be teaching the language, just diving right in. I'd read and study the 3rd link in the Hints section in that PDF.
  5. macrumors newbie

  6. macrumors newbie

    void PrintBookmarkInfo(){
    	NSMutableDictionary *urls = [NSMutableDictionary dictionary];
    	[urls setObject:[NSURL URLWithString:@"stanford.edu"] forKey:@"Stanford University"];
    	[urls setObject:[NSURL URLWithString:@"www.apple.com"] forKey:@"Apple"];
    	[urls setObject:[NSURL URLWithString:@"http://cs193p.stanford.edu"] forKey:@"CS193P"];
    	[urls setObject:[NSURL URLWithString:@"http://itunes.standford.edu"] forKey:@"Stanford on iTunes U"];
    	[urls setObject:[NSURL URLWithString:@"http://stanfordshop.com"]  forKey:@"Stanford Mail"];
    	NSString *key;
    	for( key in urls){
    		if([key hasPrefix:@"Stanford"]){
    			NSLog(@"Key: '%@'  Value: '%@'", key, [urls valueForKey:key]);
    and here is my code, but be sure to let us know what is wrong so we can help you. don't just copy
  7. macrumors regular

    Get a book on Objective-C. Both Hillegass and Kochan are on Amazon and recommended.

    I'll be frank: your code up there has so many basic syntax errors you obviously don't even have a grasp of the fundamentals of the language. It doesn't mean you are stupid. It just means you are jumping over your head in going straight to Stanford iTunes...
  8. macrumors newbie

    i'm in the same position, i know PHP and Java and I completely dont understand the formatting of the language like what @,*,[] means etc. its very annoying! i am gonna try read this:


    its a obj-c guide for the beginner, assuming no knowledge of C/C++, which is what i need anyway!
  9. macrumors G5


    In XCode, right-click on NSMutableDictionary, then "Find Selected Text in API reference". Make sure you get the Objective-C documentation, not the Java one. So either:

    NSMutableDictionary * PHBookmark = [NSMutableDictionary dictionaryWithCapacity: 0];

    NSMutableDictionary * PHBookmark = [[NSMutableDictionary alloc] initWithCapacity: 0];
  10. macrumors 6502

    Just for reference, here's how I would expect to see the code.

    #import <Foundation/Foundation.h>
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        NSMutableDictionary * bookmark = [NSMutableDictionary dictionary];
        NSString * key = @"Stanford University";
        NSString * object = @"http://www.stanford.edu";
        [bookmark setObject:object forKey:key];
        NSLog(@"%@", bookmark);
        [pool drain];
        return 0;
    - methods are called with a class or instance then the method and arguments
    - you call the class when first creating an instance, but then you work with the instance
    - class names typically start with a capital letter but instance names do not

    Think of classes as teachers and instances and students. When creating the instance above, you use the teacher "NSMutableDictionary" to train the student "bookmark" to do his/her own work in the future. The student is then able to do the "setObject:forKey:" work.

    Autorelease pools can be difficult to grasp, but they are relatively simple in concept. There are two ways to tell instances that you are finished with them. Here is an illustration:

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSMutableDictionary * dictionary = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
    // note that the capacity will be adjusted automatically as objects are added
    // do something with the dictionary...
    [pool drain];
    NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
    // note that the capacity will be adjusted automatically as objects are added
    // do something with the dictionary...
    [dictionary release];
    The difference is that in the first example, the pool is created and the instance is marked for release once the pool is drained. Think of it as hiring temporary employees for specific work. Once the job is finished, they know their jobs are complete. In the second example, you hire and fire employees one at a time.

    If you read the documentation, you will find notes about class methods returning autoreleased objects. This means that the following calls do the same thing:

    [NSMutableDictionary dictionaryWithCapacity:0];
    [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
    I also recommend the Hillegass book. You can find the newest version here:

    I am also a big fan of http://cocoadevcentral.com/

    Kind regards and good luck with your programming.

    Luke Gladding
  11. macrumors 6502

    The @ symbol is usually used when defining strings. It has to do with the fact that you're using Objective-C string objects instead of C strings. You'll also see the character when marking object placeholders in formatted strings. With some practice, you get used to typing @"" instead of "".

    The * symbol is more complicated as it indicates that the object is a pointer. This is actually from C rather than Objective-C. That said, you don't need to know much about pointers to work with Cocoa.

    The square brackets are wrapped around method calls. They require more thinking ahead than the dot syntax of other languages, but editors like TextMate will automatically enter the opening bracket when you type the closing bracket.
  12. macrumors newbie


Share This Page