 |
 |
Calling class functions...
|
 |
|
 |
|
Forum Regular
Join Date: Jul 2002
Location: Los Angeles, CA
Status:
Offline
|
|
I have a bit of a dilemma...
Currently I have a document-based application that uses sockets to relay data to and from servers. The sockets are threaded so that they await data while the application continues to do its business. Right now, I use NSNotificationCenter whenever the thread receives data from the server, and I have the document subscribe to the center looking for messages from the thread.
I don't really like this method, and it has issues when multiple documents are open. I would like the thread to simply be able to call a function from the document it belongs to whenever it receives text rather than having to go through a NotificationCenter. I've tried using [super functionname] to access functions from the document class from within the socket class, but this does not work. Anyone have an idea on how I could implement this?
|
|
I'm a bad...motherf%#!ing DJ
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
You can do it using the same kind of target/action paradigm that NSControl uses. Give your socket class some methods like
Code:
@interface MySocketClass {
id _target;
SEL _selector;
}
- (id)target;
- (SEL)action;
- (void)setTarget: (id)target;
- (void)setAction: (SEL)action;
@end
and then whenever you receive text, do [[self target] performSelector: [self action]];
You might want to retain _target, or you might not.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jul 2002
Location: Los Angeles, CA
Status:
Offline
|
|
Originally posted by smeger:
You can do it using the same kind of target/action paradigm that NSControl uses. Give your socket class some methods like
Code:
@interface MySocketClass {
id _target;
SEL _selector;
}
- (id)target;
- (SEL)action;
- (void)setTarget: (id)target;
- (void)setAction: (SEL)action;
@end
and then whenever you receive text, do [[self target] performSelector: [self action]];
You might want to retain _target, or you might not.
Hmmm, so when a new MySocketClass is created, the class that created it would have a pointer back to itself in "target?" What would setTarget look like, and what is the purpose of setAction?
I'm sorry if I don't follow, I'm still not entirely clear on class intercommunication in objective C... 
|
|
I'm a bad...motherf%#!ing DJ
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
Originally posted by Seamus:
Hmmm, so when a new MySocketClass is created, the class that created it would have a pointer back to itself in "target?" What would setTarget look like, and what is the purpose of setAction?
I'm sorry if I don't follow, I'm still not entirely clear on class intercommunication in objective C... :hmm:
When you're creating MySocketClass from another class, do this:
Code:
[mySocket setTarget:self];
[mySocket setAction:@selector(dataReceived:)];
Then the calling class gets set a dataReceived: message every time new data is received.
When the socket receives data, do this:
Code:
[[self target] performSelector:[self action] withObject:newData];
Of course, you can omit the "newData" part if you have no need forit.
The methods should look like:
Code:
- (SEL)action
{
return _action;
}
- (void)setAction:(SEL)anAction
{
_action = anAction;
}
- (id)target
{
return _target;
}
- (void)setTarget:(id)anObject
{
_target = anObject;
}
I wouldn't retain _target, that's not usually how it's done.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jul 2002
Location: Los Angeles, CA
Status:
Offline
|
|
Perfectamundo, Ibson! Thanks a ton!
Just out of curiousity, why are target and action preceded by a _ when they are declared?
|
|
I'm a bad...motherf%#!ing DJ
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
Originally posted by Seamus:
Perfectamundo, Ibson! Thanks a ton!
Just out of curiousity, why are target and action preceded by a _ when they are declared?
It's a Obj-C convention followed by many programmers (including Apple in almost every class) that tells any developers that the instance variable is private, and should not be accessed directly. Another way is to use the @private directive:
Code:
@interface MyClass : NSObject
{
@private
id _ivar1;
void *_reserved;
}
However, most developers usually don't bother, and in Apple's headers you'll usually see (instead of @private):
/*All instance variables are private*/
Apple also uses the underscore (_) to prefix private methods; however they do not encourage other developers to do that as you could then accidently override a private method. (I have to admit, however, when I subclass NSObject, I prefix my private methods with an underscore).
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
I usually double-underscore my instance variables for the same reason, and to avoid possible collisions with local variables and method variables.
But, I was typing quickly, so I didn't bother. Sorry for any confusion 
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
Originally posted by smeger:
I usually double-underscore my instance variables for the same reason, and to avoid possible collisions with local variables and method variables.
But, I was typing quickly, so I didn't bother. Sorry for any confusion
Why would you do that? I'd never want to underscore my local or method variables.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jul 2002
Location: Los Angeles, CA
Status:
Offline
|
|
Thanks for the help guys...project is coming along nicely... 
|
|
I'm a bad...motherf%#!ing DJ
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Originally posted by Ibson:
Why would you do that? I'd never want to underscore my local or method variables.
I wouldn't either  I double-underscore my instance variables, so that they don't conflict with method parameters or local variables, and so that they don't conflict with apple's superclass instance variables.
Need more caffeine, can't communicate today 
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
Originally posted by smeger:
I wouldn't either :) I double-underscore my instance variables, so that they don't conflict with method parameters or local variables, and so that they don't conflict with apple's superclass instance variables.
Need more caffeine, can't communicate today :)
I understood your original post, but you say you want to avoid collisions with method params or local vars. When was the last time you saw a method prototype like this:
Code:
- (void)doSomethingWith:(id)_firstObject and:(id)_secondObject;
I'd have to say never. I've also never seen local variables prefixed with an underscore. So my point is: why bother double underscoring your ivars? Apple actually encourages it; it's extremely unlikely that an ivar could conflict with Apple's superclass vars. But, maybe I don't quite see your point (maybe I need more caffeine :)).
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Originally posted by Ibson:
I understood your original post, but you say you want to avoid collisions with method params or local vars. When was the last time you saw a method prototype like this:
Code:
- (void)doSomethingWith:(id)_firstObject and:(id)_secondObject;
I'd have to say never. I've also never seen local variables prefixed with an underscore. So my point is: why bother double underscoring your ivars? Apple actually encourages it; it's extremely unlikely that an ivar could conflict with Apple's superclass vars. But, maybe I don't quite see your point (maybe I need more caffeine ).
Good point! I'm converted to a single underscorer!
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Mar 2001
Location: Iowa City, IA
Status:
Offline
|
|
Unfortunately, the ISO C standard reserves all names beginning with leading underscores (any number of them) to the implementation (compiler, libraries, etc.). So there's no guarantee that a _name won't collide with something internal.
I've taken to postfixing private names with underscores.
That said, I've never actually seen a collision of the sort the C Standard warns against.
|
|
James
"I grew up. Then I got better." - Sea Wasp
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|