Communicate string value to main of Cocoa app

Discussion in 'Mac Programming' started by Draque, Mar 17, 2015.

  1. Draque macrumors newbie

    Jan 6, 2015
    I'm collecting a string value in a method of my app, and as it closes, I need to pass the value back to the top level main. The reason for this is because I need to spawn a thread in a manner that can only be done from main.

    Is there a way to do this? Since I am calling NSAplicationMain immediately, there doesn't seem to be a way to access the class representing my program from there.
  2. briloronmacrumo, Mar 19, 2015
    Last edited: Mar 19, 2015

    briloronmacrumo macrumors 6502


    Jan 25, 2008
    Seems like you need to learn the basics of Cocoa

    A method can return an NSString, so a method called from main ( or applicationDidFinishLaunching: or whereever ) could use the returned string. Some consideration will be required for retention of the string ( and the use ( or not ) of ARC is a consideration too )

    If you post code it will be much easier to respond instead of guessing how ( and from where ) the method is called. Programming requires effort and that includes providing information in posts. Otherwise it's GIGO.

    -(NSString *)methodThatCollectsStringValue {
        NSString *someString = blah_blah; // do "collection" of string here. Need to consider string retention.
        return someString;
  3. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    Put a global variable in the main file. Declare an external global variable in your file where you want to write the variable. This is basic C.

    I assume this would work, but I haven't tried it.
  4. Draque thread starter macrumors newbie

    Jan 6, 2015
    Thanks for the replies, guys!

    I did find a solution to this, but unfortunately neither of those ones would work for me. The string is passed to a delegate class, and the NSAplicationMain() method abstracts away my ability to directly call the program's active class directly.

    What I ended up doing was making a two pass system, where the first pass runs a cocoa program whose only purpose is to collect the value of the double clicked file, then starts the second executable, passing the collected values and killing itself.

Share This Page