method Call

Discussion in 'iOS Programming' started by Azerix, Jul 16, 2013.

  1. Azerix, Jul 16, 2013
    Last edited: Jul 16, 2013

    Azerix macrumors newbie

    Azerix

    Joined:
    May 4, 2013
    Location:
    Amsterdam
    #1
    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
    , 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'
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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.
     
  3. Sonnestah macrumors regular

    Joined:
    Mar 2, 2013
    #3
    Something like

    Code:
    Socket *socket = [[Socket alloc] init];
    
    [socket createSocket];
    ?
     
  4. Azerix thread starter macrumors newbie

    Azerix

    Joined:
    May 4, 2013
    Location:
    Amsterdam
    #4
    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;
     
  5. Azerix thread starter macrumors newbie

    Azerix

    Joined:
    May 4, 2013
    Location:
    Amsterdam
    #5
    just one more question, doese anyone knows a better tutorial to understand how to call methods, objects or methods with(arguments) from diffrent classes?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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?
     
  7. Azerix, Jul 18, 2013
    Last edited: Jul 18, 2013

    Azerix thread starter macrumors newbie

    Azerix

    Joined:
    May 4, 2013
    Location:
    Amsterdam
    #7
    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)

    ??
     
  8. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #8
    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.
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    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.
     

Share This Page