newbie question: How to get input from a user for an object such as NSString

Discussion in 'Mac Programming' started by kingthong, Sep 23, 2010.

  1. macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #1
    Hi,

    it might be a silly question but i've just started learning Objective-C.
    My question is how do you get input from a user after initializing an object of NSString type.

    i.e say i've initialized

    Code:
    NSString *name;
    name = [[NSString alloc] init] ;
    i want the user to enter the value for Name. can i use a scanf?
    I've tried googling but to no avail.

    Thanks!
     
  2. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #2
    NSString has a method called stringWithCString.

    So you can use scanf to get a C string then use that method.

    Code:
    //Garbage collection or a pool must be present
    
    //Declare Variables
    char cString[100]; //cString is a string of 100 characters (technically an array of 100 characters)
    NSString *myString;
    	
    //Get the string form the user
    NSLog(@"Enter a string...");
    scanf("%s", &cString);
    
    //Convert to NSString
    myString = [NSString stringWithCString: cString encoding: NSASCIIStringEncoding];
    
    //Log Result
    NSLog(@"You entered: %@", myString);
    
     
  3. thread starter macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #3
    Thanks for the prompt reply!

    One more question as an addendum-

    1. Is this the only/most efficient way to read an input from a user? I will be coding on an IPhone eventually so memory and efficiency will be vital.

    Thanks again.
     
  4. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #4
    scanf will only be needed during the learning process and when coding command line based utilities.

    When you start making GUI based applications, getting a string from a user will be as simple as getting the data from a text box.

    Code:
    NSString *myString = nil;
    
    myString = [textBox stringValue];
    
     
  5. thread starter macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #5
    oh brilliant! i get it. right now i'm using GCC mainly to get the feel of Obj-C
    Thanks MorphingDragon!
     
  6. macrumors newbie

    Joined:
    Oct 20, 2005
    #6
    When I do this, I get the error "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

    Is there some #import statement I should be including?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    Post your actual code, exactly as you compiled it, and identify exactly where the error message occurs.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    I recommend using Xcode and the Clang compiler, not gcc. You will get much better error messages.
     
  9. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    I will back Clang, but not Xcode at this point. Try to avoid learning too many things at once. Obj-C is a language, Clang / GCC are both compilers, and Xcode is an IDE. All of them are interchangeable components that make up programming.
     
  10. macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #10
    I would vote for Xcode, the IDE, because it is similar to Delphi and C++Builder in the past.

    Instead of

    Code:
    myString = [textBox stringValue];
    I remember having used

    Code:
    myString = self.textBox.stringValue;
    I have to check it soon, whether I am right about this.
     
  11. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    Dot notation on the right hand side of an assignment is the same as calling methods without arguments.

    So these two lines are equivalent:
    Code:
    myString = [[self textBox] stringValue];
    myString = self.textBox.stringValue;
    Quick question... is it possible to write a setter function which returns the value set as use it with dot notation? It might allow something like:

    Code:
    myString = self.textBox1.stringValue = self.textBox2.stringValue;
    Or am I mistaken?
     
  12. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    I haven't tried it, but that should work anyway. The Objective-C compiler assumes you have setter and getter, and generates any other code you need. For example

    Code:
       self.count++
    
    will be compiled as

    Code:
      (tmp = self.count, self.count = tmp + 1, tmp)
     

Share This Page