Objective-C tutorial?

Member
Posts: 28
Joined: 2008.06
Post: #16
I mean what I do to let the compiler know that "myTextBox" is a reference to the textbox in my window.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #17
I think I might understand what you mean now (maybe). In your header you'll set the name of your text field in your interface declaration using IBOutlet, like so:

@interface MyClass : NSObject
{
IBOutlet NSTextField *myTextBox;
}

@end

Then IB would know there is an outlet named myTextBox in your class (which must be instantiated somehow in IB) which you can then hook up to your textField. When you control drag from your instantiated object from that class in IB to your textField, a little floating window will pop up with myTextBox listed as an outlet you can use.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #18
It worked!!!! THANK YOU!
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #19
Hehe. We aim to please. Wink
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #20
How do I get my class to receive delegate events?
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #21
It depends, but usually:

[someOtherClass setDelegate:self];

Which implies that you need to have access to an instance of the class you wish to receive delegate messages from.

You might need to also declare that your class supports the other class's delegate protocol. Assuming you know the name of the delegate protocol, you can do that something like so in your header:

@interface MyClass : NSObject <SomeOtherClassDelegate> {
}
@end

An alternative to receiving delegate messages is that you can often register for equivalent notifications from the class you're interested in. For notifications you won't have to have access to an instance of the class. I generally prefer notifications, but sometimes delegate is the only way, depending on the situation.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #22
So, if I do that, then any function I put in my .m file that has the same name/type/parameters as a delegate function gets called when event x happens?
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #23
It's technically referred to as a "method" not a "function", but yes, that's the general idea.

Some classes may assume that you implement *all* delegate methods declared in their delegate protocol, and some classes may check beforehand whether or not your class responds to them. I don't have time to lay the concept out explicitly here. You should definitely study up on delegates and protocols (informal and formal). Delegation and notification are hugely important (and infinitely handy) features to understand.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #24
Ok! I'll study about delegates and keep experimenting. Thanks for all the help! I've been putting off learning Cocoa and Obj-C for long enough.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #25
Why doesn't this work?
Code:
- (NSNumber)operate {
    float an, bn;
    float res;
    an = [op floatValue];
    bn = [working floatValue];
    if(oprt == 0) {
        res = an / bn;
    } else if(oprt == 1) {
        res = an * bn;
    } else if(oprt == 2) {
        res = an + bn;
    } else if(oprt == 3) {
        res = an - bn;
    }
    return [NSNumber numberWithFloat: res];
}
It says "error: can not use an object as parameter to a method" and
"error: incompatible types in return"
What is wrong?
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #26
Try:

- (NSNumber *)operate

instead. That is the first (perhaps only) major issue I see. It seems to me that it should probably work other than that. Instances are (represented by) pointers, and so require the * operator to label them as such. Remember that you are returning an instance of an NSNumber. Basically you will always (well, often) have to include the * next to your return type when dealing with Obj-C.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #27
Thank you! I've been grappling with that error for about 1.5 hours.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #28
What causes "warning: assignment from distinct Objective-C type"?
Edit: Never mind.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #29
This code:
Code:
- (NSNumber *)operate {
    float an, bn;
    float res;
    an = [op floatValue];
    bn = [working floatValue];
    if(oprt == 0) {
        res = an / bn;
    } else if(oprt == 1) {
        res = an * bn;
    } else if(oprt == 2) {
        res = an + bn;
    } else if(oprt == 3) {
        res = an - bn;
    }
    return [NSNumber numberWithFloat: res];
}
Receives signal EXC_BAD_ACCESS and crashes. Does anyone know what's wrong with it? I think it's the
Code:
an = [op floatValue];
line, but I'm not sure.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #30
It is entirely possible you forgot to retain op in some other section of code when it was created, so when you try to call it to get a float value from it, it ain't there. You can try checking to see if it's nil right before an = [op floatValue];

Code:
if (op == nil)
{
    NSLog(@"Apparently op doesn't exist, which likely means it wasn't retained properly.");
    exit(0);
}
else
    an = [op floatValue];

Incorrect retains are probably the root of most of the initial bugs I encounter with my own code. They're easy to forget about when you're pounding out code.
Quote this message in a reply
Post Reply