Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Azerix

macrumors newbie
Original poster
May 4, 2013
7
0
Amsterdam
hi,

i have two different classes. One is Socket.m and h of type NSObject inside this class i have a method(creatSocket) which creates a socket. I want to call this method(creatSocket) inside SomeViewController.m class.

How do i do that?

i tryd with selector
[[Socket class] performSelector:mad:selector(createSocket)];
, but that did not workt for me. I get:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Socket createSocket]: unrecognized selector sent to class 0xa250'
 
Last edited:
You will have to post the Socket code for us to have a clue. At the very least, the header file.

We know it's not this:

Code:
s = [Socket createSocket];

But without at least a header file there's no way to know. So we'll need more information.
 
SOLVED


My header file:

Code:
@interface Socket : NSObject
-(void)openSocket;
-(void)createSocket;
-(void)closeSocket;
@end

Yes this is correct way, indeed:

Code:
Socket *socket = [[Socket alloc] init];
[socket createSocket];

but the reason why it did not workt for me, douse i used + sign
Code:
+(void)createSocket;
 
just one more question, doese anyone knows a better tutorial to understand how to call methods, objects or methods with(arguments) from diffrent classes?
 
Take a step back. Do you understand the difference between a class method and an instance method. Or for that matter a class and an instance?
 
i understand those differences, i just dont know how to call the method that i have in Socket.m class in class SomeViewController.m:
Code:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode

is this possible? or NSStreamEvent invoking it self only? Is it possible to make a separate method that can invoke the handleEvent:(NSStreamEvent)

??
 
Last edited:
You have a couple options. It all depends on your application and what you are doing.

  1. Create the Socket instance in the controller. Probably as a member variable of the controller. This is likely the best choice if only the controller needs to access the socket.
  2. Create the socket instance in Interface Builder and use Outlets to get access to it. Probably not the best choice, but possible. It's almost the same as the last option...
  3. Use a singleton design pattern for the Socket if it makes sense.

But either way, keep in mind that you will likely need to do your network operations in a separate thread or you will need to use some other network operation class that will perform non-blocking network operations.

You will not be able to block on a read from the socket in the main application thread or it will lock up your UI while the network read is blocked. That's why separate threads are necessary.
 
i understand those differences, i just dont know how to call the method that i have in Socket.m class in class SomeViewController.m:
Code:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode

is this possible? or NSStreamEvent invoking it self only? Is it possible to make a separate method that can invoke the handleEvent:(NSStreamEvent)

??

1) I assume that an instance of SomeViewController has access to an existing instance of Socket: creating a new instance to just call that sort of method is very unlikely to have the results you want. Which is where my questions come from...

2) If so then yes: just call the method on the instance you have passing valid values for the parameters.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.