views:

156

answers:

2

How would I send a Selector to another class? I know to send it to a selector in the same file you do

[self performSelector:@selector(doSomething)];

and for sending it to another class I've tried

[otherClass performSelector:@selector(doSomethingElse)];

But I just get an error in the Debugger saying

+[otherClass doSomethingElse]: unrecognized selector sent to class 0xe5c4

Why is this??


EDIT In Response To Daves Answer

' Chances are it's not a class method but rather an instance method … '

How do I create an Instance of My Class then?

+3  A: 

From the debug message +[otherClass doSomethingElse] it says that you're sending it to the class itself, which means you're trying to invoke a class (static) method.

Chances are it's not a class method but rather an instance method, which means you should be doing:

[anInstanceOfOtherClass performSelector:@selector(doSomethingElse)];
Dave DeLong
Ah! I see. So how do I make an instance of the class?
Joshua
@Joshua - `[[OtherClass alloc] init]`...
Dave DeLong
Thanks, I've added that, but Still get the same error plus some more due to the added code (by the looks of things). http://cld.ly/fd3o8.
Joshua
JGManagedObject being what replaced `otherClass`.
Joshua
@Dave alloc/init not recommended with Core Data managed object classes.
Abizern
@Abizern I see, that's why I got the error. @Dave How else would i make an instance of my class then?
Joshua
Have a look at the designated initializer for NSManagedObject http://developer.apple.com/mac/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/initWithEntity:insertIntoManagedObjectContext:
Abizern
`initWithEntity:insertIntoManagedObjectContext:`?
Joshua
@Joshua - that's the designated initializer for an NSManagedObject, so sure. @Abizern - there was no indication of using Core Data until @Joshua's comment linking to some code.
Dave DeLong
Ok. I know what to put after `insertIntoManagedObjectContext:` but i don't know what I should put after `initWithEntity:`. I think it would be something like http://cld.ly/4b3q9, but I wasn't sure what to put after `NSEntityDescription *entity =`.
Joshua
@Dave; My apologies; if you haven't been answering Joshua's questions you wouldn't automatically know that he's talking about a CD object.
Abizern
@Abizern - no probs. @Joshua - you need an `NSEntityDescription`. What does the documentation say on how you acquire `NSEntityDescription` objects? Reading the documentation is not rocket science.
Dave DeLong
I think we can officially find that Joshua does not want to read the documentation or understand basic Cocoa, Objective-C, and C concepts. He just wants to copy and paste code we provide for him. (Look over his previous questions, their answers, and his comments on them for more evidence of this.)
Peter Hosey
@Peter I do and will read the Documentation but it's quite hard to find what you want because it is so large. I mainly use the Documentation for looking at classes and their methods, not the programming guides. Ok, Yes coping and pasting is easy, but i am also capable of figuring some things out my self and on a couple of occasions answered my own questions. However in my earlier questions I was less experienced and all I wanted was code, and I want to say sorry for that because I can't expect the users of SO to do everything for me. I'll try to figure out more things by my self in the future.
Joshua
I'm sorry for my past attitude if it in anyway offended you.
Joshua
Ok. This is what I've got http://cld.ly/3e429. I thought it would work but no, http://cld.ly/ce427. Still same problem, Whats gone wrong?
Joshua
Joshua: You don't even know how to create an instance. **The reference documentation is not helping you at this level.** You need to read the guides first. I suggest the Cocoa Fundamentals Guide, along with everything it links to. Alternatively, buy the Hillegass book and read that, cover to cover. Or read the tutorials on Cocoa Dev Central. Any of those paths will make a Cocoa programmer out of you *much* faster than what you're doing now.
Peter Hosey
You've created an instance of a class and added it to the moc; but you're still sending a message to the class. In any case, I think you have something wrong with your design, because you're trying to create an object just to call removeObserver: on it.
Abizern
@Peter Ok, I'll buy Hillegass' book, will it have everything I need to know about the basics of Cocoa? Do you recommend it? @Abizern In the class JGManagedObject there is a method called removeObserver, I want to run that method from another class. Because to run a method in the same class you just do `[self performSelector:@selector(doSomething)];`.
Joshua
No. Look in the Xcode documentation viewer. `self` returns an `id`. An `id` is a pointer to an _instance_ of a class. So, you can't call a class method by sending the message to self; because you're sending the message to an object of that class.
Abizern
To send a message to a class you use `[Class aMessage];`. To send a message to an object use use `[object aMessage];` When you don't know what message to send until runtime use use `performSelector` http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/doc/uid/20000052-BBCBGHFG
Abizern
But that's what I'm using. `performSelector:@selector(doSomething)`. Isn't it?
Joshua
@Abizern not quite. `self` can refer to a Class if used within a class method. `self` always refers to the object on which the method is operating. For class methods, it's the Class object. For instance methods, it's the particular instance of the class. So if you're in a class method, you *can* do `[self performSelector:@selector(anotherClassMethod)]`. For a real example, check this out: http://github.com/davedelong/CHMath/blob/master/Source/CHNumber.m
Dave DeLong
@Dave - I stand corrected! Thanks for the info.
Abizern
So what do I need to do? `id JGManagedObject = […………]`?
Joshua
@Joshua - I would say that you need to rethink your design. If your `removeObserver` method doesn't care what instance it operates from (as indicated by you wanting to create an arbitrary `JGManagedObject` *just* so you can call `removeObserver`), then it probably shouldn't be an instance method. However, it seems like your confusion runs even deeper than that, so I'd suggest finding some tutorials on basic Cocoa programming and going through those.
Dave DeLong
@Dave Ok, I'll check out a few tutorials but how else could I run the method in another class?
Joshua
How have you declared `removeObserver` in the `JGManagedObject` header file?
Abizern
`-(void) removerObserver { … }`
Joshua
+2  A: 

-(void) removeObserver {…}

There's your problem. The - sign identifies this as an instance method; that is only run on objects of the class. What you need to do is declare and define it as:

+(void)removeObserver …

and you can call this as:

[JGManagedObject removeObserver];

That way, you wont need to use performSelector: to avoid the error message you get when sending an instance message to a class.

To help you along, here's the relevant documentation.

Abizern