Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

afornander

macrumors 6502
Original poster
Apr 5, 2006
286
0
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


Code:
#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
 

danmanstx

macrumors newbie
Mar 23, 2009
18
0
Lexington, Ky
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.
 

afornander

macrumors 6502
Original poster
Apr 5, 2006
286
0
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.

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)
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

danmanstx

macrumors newbie
Mar 23, 2009
18
0
Lexington, Ky
Code:
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
 

yoavcs

macrumors regular
Apr 7, 2004
218
92
Israel
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...
 

IrelandinBeta

macrumors newbie
Mar 14, 2008
4
0
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:

http://gnustep.made-it.com/BG-objc/

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

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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)

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:

Code:
NSMutableDictionary * PHBookmark = [NSMutableDictionary dictionaryWithCapacity: 0];
or

Code:
NSMutableDictionary * PHBookmark = [[NSMutableDictionary alloc] initWithCapacity: 0];
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
Just for reference, here's how I would expect to see the code.

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:

Code:
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];

Code:
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:

Code:
[NSMutableDictionary dictionaryWithCapacity:0];
Code:
[[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];

I also recommend the Hillegass book. You can find the newest version here:
http://www.amazon.com/Cocoa-Program...bs_sr_1?ie=UTF8&s=books&qid=1239886751&sr=8-1

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

Kind regards and good luck with your programming.

Luke Gladding
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
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:

http://www.otierney.net/objective-c.html

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

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.
 

IrelandinBeta

macrumors newbie
Mar 14, 2008
4
0
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 "" pretty quickly.

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.

:D THANKS
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.