Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jun 27, 2007, 07:28 PM   #1
macrumors 6502a
bbarnhart's Avatar
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
It can't be that hard: NSData & NSString

I've resurrected an old Cocoa project and after making some additions I'm still stuck on something that caused me to stop in the first place. I'm still learning Cocoa so I may not quite "get it" yet, but this can't be that hard.

I've got a xml file I want to upload to my web server. I want to add some data before the xml data. Basically, my url looks like this:

http://../myphp.php?type=Put&file=xxx.xml&data=<xmldata here>

So, I've tried building an NSString to put "type=Put&file=xxx.xml&data=" and then copy that into an NSMutableData object. Then I add the xml to the NSMutableData. I had it "working" once, but then it failed with a different xml file.

Am I making a mistake with Unicode or am I not understanding fully how NSString and NSMutableData works? Any help would be appreciated!

- (void) makeXML
  NSXMLDocument* xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
  NSData *xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
  [xmlData writeToFile:@"/Users/b...barnhart/Temp/hello.xml" atomically:YES];
  [self uploadTest: xmlData];
  return TRUE;

- (void) uploadTest: (NSData*) data
  NSURL* webServiceURL = [NSURL URLWithString:@"http://.../~b...barnhart/.../xxxxxx.php"];
  NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:webServiceURL];

  NSString* str = @"type=Put&filename=xxx.xml&data=";
  const char* utf_str = [str UTF8String];
  NSMutableData* mute_data = [NSMutableData dataWithLength:strlen(utf_str) + [data length]];
  [mute_data appendBytes:utf_str length:strlen(utf_str)];
  [mute_data appendData:data];
  ... stops working down here, but I think the problem is above

bbarnhart is offline   0 Reply With Quote
Old Jun 27, 2007, 07:47 PM   #2
Moderator emeritus
kainjow's Avatar
Join Date: Jun 2000
The best way to go from NSString to NSData is via NSString's dataUsingEncoding:allowLossyConversion: method, and you usually want NSUTF8StringEncoding as the encoding.
kainjow is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NSData byte Access xArtx iPhone/iPad Programming 27 Sep 28, 2013 02:25 AM
Bare 3.5 hard drives, & enclosures vs buying all in one external hard drive Sossity Buying Tips and Advice 16 Feb 12, 2013 11:10 AM
NSString format & xtra characters Dreamspinner Mac Programming 11 Dec 24, 2012 12:41 PM
how to remove &amp; from NSString Narendar Singh iPhone/iPad Programming 7 Dec 12, 2012 08:34 AM
Converting NSData to NSString SIGABRTs me Maury Mac Programming 4 Jun 18, 2012 09:12 PM

Forum Jump

All times are GMT -5. The time now is 09:17 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC