Resolved how to properly exit a method?

Discussion in 'iOS Programming' started by DennisBlah, Jul 20, 2014.

  1. DennisBlah, Jul 20, 2014
    Last edited: Jul 21, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Just as my title already says.
    My method looks a bit like this.
    Sorry the code is really not related.

    Code:
    -(void)updateData {
      //Loading current data in plist
      //Doing some preperations and styling
    
      //If we don't have network connection or host is offline
        //Exit this function and init error viewcontroller
    
      //checking current data with online data and update
      //whilst we stay on this viewcontroller
    }
    
    How to exit the method after I'm initializing my next viewcontroller?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    You can return... Or I'm pretty sure there's an implicit return at the end of a method that's declared as returning void.
     
  4. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    Hmmm, ok I will try that :)

    I thought only methods that are defined as a var can return.

    Thanks guys!
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    Your Pseudocode suggested you wanted to return early. For those cases you can do a simple return when void is involved or return a value of the return type of the method or function.

    Personally I try to avoid early returns as it can get messy to keep track of them in a large method. Knowing there is one return location at the end of the block of code means I only have to look there for the result.
     

Share This Page