Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Beginner Cocoa Problem

Beginner Cocoa Problem
Thread Tools
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 5, 2002, 02:02 PM
 
Ok, I'm teaching myself cocoa and obj-c on the fly here, my previous experience is in C/C++, Java, Scheme, and SML. Essentially all I'm trying to do at the moment is set member variables of a class based on what I type in to fields in a window, and then have it spit those values back at me. I can get it to work when dealing with a single field with the following code:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(IBAction)refresh id)sender {
[handleField setStringValue:[Character getHandle]];
}

-(IBAction)set id)sender {
[Character setHandle:[handleField stringValue]];
}
</font>[/code]

However, if I try to do it two two fields at once:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(IBAction)refresh id)sender {
[nameField setStringValue:[Character getName]];
[handleField setStringValue:[Character getHandle]];
}

-(IBAction)set id)sender {
[Character setName:[nameField stringValue]];
[Character setHandle:[handleField stringValue]];
}
</font>[/code]

it crashes. I don't have a clue why this is. Can anyone help?
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 5, 2002, 04:51 PM
 
Well, I realized that that way of doing things was stupid and I'm now doing it a much better way. I'm still curious as to what was going on, although it's not particularly important anymore.
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 5, 2002, 09:52 PM
 
Ok, I've run into another problem. As far as I can tell this one isn't just me doing things poorly though. I have this function:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>-(void)setType NSString*) newType {
type = newType;
}</font>[/code]

and it's being called here:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>-(IBAction)updateType id)sender {
[Character setType:[typeField stringValue]];
}</font>[/code]

and it's giving me an error "incompatible type for argument 1 of 'setType'.

I'm using the exact same code (only interacting with different methods) in several other places that work just fine. Is this really as whacked as it seems?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 6, 2002, 05:04 PM
 
Wooooah! You got some serious issues with your code.

From the looks of things, Character is the name of your class. When you do this: [ClassName sig:arg sig:arg ...], you're calling the method statically. In that case, you use a + sign instead of a -. + =&gt; off of your class, - =&gt; off of your instance object of a type of class.

Honestly, I don't see the point of the code, but maybe finals are clogging my head.

F-bacher
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 6, 2002, 07:01 PM
 
Originally posted by Ghoser777:
<STRONG>Wooooah! You got some serious issues with your code.

From the looks of things, Character is the name of your class. When you do this: [ClassName sig:arg sig:arg ...], you're calling the method statically. In that case, you use a + sign instead of a -. + =&gt; off of your class, - =&gt; off of your instance object of a type of class.

Honestly, I don't see the point of the code, but maybe finals are clogging my head.

F-bacher</STRONG>
So the [ClassName sig:arg] crap is only for class methods and not instance methods? Argh... So I want to be doing this like I would in C++? Sigh, I really need to get a book or something.
     
Forum Regular
Join Date: Jan 2002
Location: New York
Status: Offline
Reply With Quote
May 6, 2002, 08:19 PM
 
What you would do with setType is use a Character instance, and call it like this:

Character stick = [[Character alloc] init];

//Do stuff with stick

[stick setType:[NSString stringWithString:@"whoa"]];

Your setType function is wacky too. It should be:
[type autorelease];
type = [newType copy];

HTH
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 6, 2002, 08:27 PM
 
Originally posted by ksuther:
<STRONG>Your setType function is wacky too. It should be:
[type autorelease];
type = [newType copy];</STRONG>
I'm confused, what does autorelease do? Why can't I just do type = newType? Objective-C is not as intuitive as I would have expected it to be coming from a mostly C-based background.

Thanks for all the help, guys.
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
May 6, 2002, 09:09 PM
 
Originally posted by nonhuman:
<STRONG>

I'm confused, what does autorelease do? Why can't I just do type = newType? Objective-C is not as intuitive as I would have expected it to be coming from a mostly C-based background.

Thanks for all the help, guys.</STRONG>
It's very intuitive if you use malloc and free in C (or new and delete in C++). All of the variables representing Objective-C objects are really pointers to those objects. You need to dynamically allocate them in the heap, and release them when you're done with them. If you just assign the new object to the old object's pointer, you'll leave the old object floating around in the heap, with no way to ever get rid of it since you don't have a pointer to it anymore, and thus you have a memory leak.

Why autorelease? Well, you need to deallocate the memory for the old object one way or another. You could use release, but that releases it immediately, as opposed to autorelease, which releases it at the end of the current event loop, so that anything else that might have been using that object in the meantime won't have the object pulled out from underneath. So autorelease is safer and less likely to cause a crash.

Why copy? Because the object passed in could have been created by a convenience constructor or otherwise autoreleased. If that were the case, and you just used type = newType, everything would work, up until the end of the current event loop, when the object would be released, and all of a sudden the object doesn't exist anymore, although your program still wants to use it as if it did. The result? El crasho! The copy method creates and returns a copy of the object which is retained and *not* autoreleased, so it will stick around until you release (or autorelease) it.

Hopefully that wasn't too confusing. If you don't have a clue what I was just talking about, read the article on memory management at Stepwise, or even better, get a book on Cocoa and read it. It will make more sense, trust me.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
May 6, 2002, 09:18 PM
 
Actually makes lots of sense. Thanks.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2