Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jholder85638

macrumors newbie
Original poster
Apr 29, 2010
1
0
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:
Code:
@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;

@end

and the method:
Code:
- (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=\"http://www.w3.org/2003/05/soap-envelope\">\n"
	 "<soap:Header>\n"
	 "<context xmlns=\"urn:myapp\">\n"
	 "<userAgent name=\"myuseragent\"/>\n"
	 "<session xmlns=\"\"/>\n"
	 "</context>\n"
	 "</soap:Header>\n"
	 "<soap:Body>\n"
	 "<MyRequest xmlns=\"urn:myauth\">\n"
	 "<text xmlns=\"\">%@</text>\n"
	 "</MyReqest>\n"
	 "</soap:Body>\n"
	 "</soap:Envelope>\n", textValue,
	 ];

But the data sent looks like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:myapp">
<userAgent name="myuseragent"/>
<session xmlns=""/>
</context>
</soap:Header>
<soap:Body>
<MyRequest xmlns="urn:myauth">
<text xmlns=""><NSTextField: 0x1001417d0></text>
</MyRequest>
</soap:Body>
</soap:Envelope>

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. :)
 
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.
 
[NSString stringWithFormat: @"%@", someObject] puts the returned value for (NSString *)[someObject description] into that string.
 
[NSString stringWithFormat: @"%@", someObject] puts the returned value for (NSString *)[someObject description] into that string.

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

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.
 
EDIT: also in the above codefragments (chown33's), after asignment both str's will be nil if someObject = nil.

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

Code:
	NSString * nilly1 = [NSString stringWithFormat: @"%@", nil];
	NSLog( @" nilly1: %@, %d", nilly1, (nilly1 == nil) );

	nilly1 = nil;
	NSString * nilly2 = [nilly1 description];
	NSLog( @" nilly2: %@, %d", nilly2, (nilly2 == nil) );
Output:
Code:
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
 
Not so. The stringWithFormat: one will format and return a NSString* containing "(null)". The other one will return nil.

Code:
	NSString * nilly1 = [NSString stringWithFormat: @"%@", nil];
	NSLog( @" nilly1: %@, %d", nilly1, (nilly1 == nil) );

	nilly1 = nil;
	NSString * nilly2 = [nilly1 description];
	NSLog( @" nilly2: %@, %d", nilly2, (nilly2 == nil) );
Output:
Code:
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
Lol yeah, why didn't I think of that. True, the stringWithFormat one will actually make a string just containing (null).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.