PDA

View Full Version : Help with Amateur Coder :/




afornander
Apr 9, 2009, 10:37 PM
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];
syntax error before NSString
PHBookmark = [NSMutableDictionary];
syntax error before "]" String

NSString * PHStanU = @"Stanfard University";
PHStanU = [PHBookmark "http://StanfardUniversity"];
syntax error before string constant

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
Apr 9, 2009, 11:11 PM
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
Apr 9, 2009, 11:14 PM
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
Apr 9, 2009, 11:16 PM
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
Apr 9, 2009, 11:33 PM
try looking here http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableDictionary

danmanstx
Apr 9, 2009, 11:35 PM
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
Apr 10, 2009, 06:19 AM
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
Apr 16, 2009, 06:04 AM
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
Apr 16, 2009, 06:56 AM
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:

NSMutableDictionary * PHBookmark = [NSMutableDictionary dictionaryWithCapacity: 0];

or

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

lucasgladding
Apr 16, 2009, 08:00 AM
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:
http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_bbs_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
Apr 16, 2009, 08:15 AM
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
Apr 16, 2009, 08:17 AM
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