Posting Data from NSTextField

Discussion in 'Mac Programming' started by jholder85638, Apr 29, 2010.

  1. jholder85638 macrumors newbie

    Apr 29, 2010
    Hi everyone,
    I'm sure this is a really simple question. I have a user entered item in a NSTextField form. When the user enters the information, they hit post, and it sends the data to he server.

    Unfortunately, it's not sending the data, but some sort of encoded value. How do I get xcode to preserve the string i'm sending?

    Example from header file:
    @interface LoginForm : NSObject {
    	IBOutlet NSTextField *textValue;
    	//---web service access---
        NSMutableData *webData;
        NSMutableString *soapResults;
        NSURLConnection *conn;
    	//---xml parsing---
        NSXMLParser *xmlParser;
        BOOL *elementFound;  
    @property (nonatomic, retain) NSTextField *textValue;
    - (IBAction)doLogin:(id)sender;
    and the method:
    - (IBAction)doLogin:(id)sender{	
    	[theRequest setHTTPMethod:@"POST"];
    	// Set useful headers
    	[theRequest setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
    	[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
    	// The body
    	NSString *theRequest = 
    	[NSString stringWithFormat:
    	 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    	 "<soap:Envelope xmlns:soap=\"\">\n"
    	 "<context xmlns=\"urn:myapp\">\n"
    	 "<userAgent name=\"myuseragent\"/>\n"
    	 "<session xmlns=\"\"/>\n"
    	 "<MyRequest xmlns=\"urn:myauth\">\n"
    	 "<text xmlns=\"\">%@</text>\n"
    	 "</soap:Envelope>\n", textValue,
    But the data sent looks like this:
    Notice that textValue has become <NSTextField: 0x1001417d0>, which ofcourse is driving the server nuts. The funny thing is this works great on the iphone. Just not the Mac. Thank you so much!

    **note: There maybe syntax errors in here from my editing to take out a few things. But the problem still exists. :)
  2. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    You're telling it to convert an NSTextField as an NSString, not the contents of the text field. You need to use the -stringValue method.
  3. jared_kipe macrumors 68030


    Dec 8, 2003
    [NSString stringWithFormat: @"%@", someObject] puts the returned value for (NSString *)[someObject description] into that string.
  4. chown33 macrumors 604

    Aug 9, 2009
    This code:
    NSString * str = [NSString stringWithFormat: @"%@", someObject];
    can be expressed more concisely as:
    NSString * str = [someObject description];
    The only difference one might expect is if someObject is nil.
  5. jared_kipe macrumors 68030


    Dec 8, 2003
    True in this case, I was meaning more broadly though in that if have a more general formatting string...

    [NSString stringWithFormat: @"some int = %d some object = %@", someInt, someObject];

    If someObject is an NSString, or probably a subclass of NSString, you get the string in the %@'s position. Otherwise [someObject description] is placed in the %@'s position. You can override -(NSString *) description; to return any string you'd like (for instance a print out of the ivar values) but the default NSObject's implementation is to return "ClassName memory address".

    In fact, NSString has overridden the description method to just return the string itself. Hence NSLog(@" str value %@ and str description %@", str, [str desctiption]); logs the string itself in both locations.

    EDIT: also in the above codefragments (chown33's), after asignment both str's will be nil if someObject = nil. And if logged or formatted into another string you will get (null) in the position.
  6. chown33 macrumors 604

    Aug 9, 2009
    Not so. The stringWithFormat: one will format and return a NSString* containing "(null)". The other one will return nil.

    	NSString * nilly1 = [NSString stringWithFormat: @"%@", nil];
    	NSLog( @" nilly1: %@, %d", nilly1, (nilly1 == nil) );
    	nilly1 = nil;
    	NSString * nilly2 = [nilly1 description];
    	NSLog( @" nilly2: %@, %d", nilly2, (nilly2 == nil) );
    2010-04-30 14:01:04.143 a.out[6998]  nilly1: (null), 0
    2010-04-30 14:01:04.143 a.out[6998]  nilly2: (null), 1
  7. jared_kipe macrumors 68030


    Dec 8, 2003
    Lol yeah, why didn't I think of that. True, the stringWithFormat one will actually make a string just containing (null).

Share This Page