Objective-C tutorial?
I mean what I do to let the compiler know that "myTextBox" is a reference to the textbox in my window.
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.
@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.
It worked!!!! THANK YOU!
Hehe. We aim to please.
How do I get my class to receive delegate events?
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.
[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.
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?
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.
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.
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.
Why doesn't this work?
It says "error: can not use an object as parameter to a method" and
"error: incompatible types in return"
What is wrong?
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];
}"error: incompatible types in return"
What is wrong?
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.
- (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.
Thank you! I've been grappling with that error for about 1.5 hours.
What causes "warning: assignment from distinct Objective-C type"?
Edit: Never mind.
Edit: Never mind.
This code:
Receives signal EXC_BAD_ACCESS and crashes. Does anyone know what's wrong with it? I think it's the
line, but I'm not sure.
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];
}Code:
an = [op floatValue];
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];
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.
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.

