View Full Version : Obj-C: Why can't I use my newly added method
emileej
2005.06.14, 07:35 AM
<emileej|work> Verify: Process of adding a method to a class in cocoa: add "- (BOOL)myFunc:(int)a b:(int)b c:(int)c;" to the interface definition in my .h file, add "- (BOOL)myFunc:(int)a b:(int)b c:(int)c{ return NO; }" to my implementation declaration in my .m file
<emileej|work> Then how come I get "*** -[MyClass myFunc:b:c:]: selector not recognized"?
<emileej|work> I use it like so: "[myPtr myFunc:0 b:0 c:0];"
<emileej|work> Where myPtr is defined like this: "MyClass *myPtr;"
Note: MyClass is the custom class for my Custom View in IB.
Andrew
2005.06.14, 08:43 AM
I don't quite understand your question. Are you asking about objective-C categories, or are you just wondering how to write your own methods in objective-C?
You said "Note: MyClass is the custom class for my Custom View in IB". Where did you create this NSView subclass? Did you do it all in IB? If so, did you create files for the class and add them to your project?
Maybe you could try to rephrase your question so that it's a bit more clear/detailed? ;)
emileej
2005.06.14, 08:54 AM
Andrew: Yes I created the class in IB and how exactly do you expect me to try implementing methods without having files to do so in?
Andrew
2005.06.14, 09:07 AM
I was just trying to figure out how you went about creating the class (you could have started with code, then imported the .h into IB).
Anyway, let's have a look at your code.
emileej
2005.06.14, 09:10 AM
Sorry. Can't paste the code here. You haven't heard of this problem before then?
Andrew
2005.06.14, 09:27 AM
ok...
How about I paste some code then, and you tell me you if your code looks similar ;)
MyView.h
#import <Cocoa/Cocoa.h>
@interface MyView : NSView {
}
- (BOOL) myFunc: (int)a b: (int)b c: (int)c;
@end
MyView.m
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (void)drawRect:(NSRect)rect {
}
- (BOOL) myFunc: (int)a b: (int)b c: (int)c {
return NO;
}
@end
Andrew
2005.06.14, 09:31 AM
It just occurred to me... did you make a connection in IB between your controller instance and the custom view (by control-clicking and dragging from the app controller instance to the custom view)?
emileej
2005.06.14, 09:42 AM
Thats how my code looks. Yea. And no I didn't since I don't instantiate the class - I set it as custom class for my Custom View component. Also - I have another method implemented in the class and it works just fine.
Andrew
2005.06.14, 09:57 AM
By setting the view's custom class to your class, you ARE instantiating your class. If you didn't hook up the connection in IB, how are you accessing the instance in your controller code? Your controller class should have an IBOutlet for the view.
Andrew
2005.06.14, 09:59 AM
Also, this other method which does work... are you calling it directly in code, or is it automatically called via the init or awakeFromNib methods?
emileej
2005.06.14, 10:12 AM
Its a direct call.
Yea I know I'm instantiating via IB :)
The class is subclassed from NSView and can therefore be set as custom class for a Custom View component - its in this class that the method is defined. I have then created a controller class, inheriting from NSObject, with an outlet for the view class with which it is calling the methods.
emileej
2005.06.14, 10:38 AM
Hmm... XCode is being silly... I tried copy/pasting, renaming and altering the call to the method to mach the new name and for some reason XCode likes this better. Afterwards I deleted the original method and renamed the new method to the name of the original method and violá! This one XCode likes better...
Don't look at me - I can't explain it either :p
Andrew
2005.06.14, 12:35 PM
my guess is that you had a typo in your method name
emileej
2005.06.14, 02:24 PM
Heh. Thanks for the trust in my skills. One might suspect that my first reaction to the error was to copy the name from the decleration and paste it into all relevant locations, but then again I might be a moron.
Steven
2005.06.14, 08:49 PM
Typos are probably the #1 cause of code errors, that wasn't a personal attack ;)
Andrew
2005.06.15, 01:20 AM
Typos are probably the #1 cause of code errors, that wasn't a personal attack
exactly. Typos have absolutely nothing to do with coding skills. Some of the world's best computer scientists are also the world's worst typists. I was just trying to help you find the problem with code.
emileej
2005.06.15, 08:56 AM
hehe yea sorry for lashing out like that - was a bit stressed. However the first thing I do when getting errors like "unknown symbol" etc. is to copy/paste the name to the declarations, definitions and places where its used. I really don't get this error.
Malarkey
2005.06.15, 10:54 AM
exactly. Typos have absolutely nothing to do with coding skills. Some of the world's best computer scientists are also the world's worst typists. I was just trying to help you find the problem with code.
I have a co-worker who's slightly dyslexic and coded the basis of one of our UI engines. To this day, I still have to remember that kRecursiveDeleteAfterDisconect is not the correct spelling.
Steven
2005.06.15, 06:31 PM
Global find and replace ;)
Malarkey
2005.06.18, 12:51 PM
Global find and replace ;)
You know, we thought about doing that so many times but this was on a project that was under-budget and behind schedule and we were putting out so many "fires" that doing a global find and replace to fix one constant was really, really low on our list of things that needed doing. :)
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.