Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Error Too Many Arguments to function StringWithString Help Needed --

proindigoproindigo Posts: 34Registered Users
edited July 2012 in iOS SDK Development
Hello and greetings. I constructed the following block of code to upload an image file to some location on the server but when built, it gives me error 'too many arguments to function StringWithString'. I am totally new to Xcode and Objective C. So I haven't got much clue as to what I am doing wrong. Please help me by pinpointing the seource of error and rectify the situation.

Here is my block of code.

- (IBAction)uploadImage {
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@yyyyMMddHHmmss];
NSDate *now=[[NSDate alloc] init];
NSString *imageName=[NSString stringWithFormat:@Image_%@", [format stringFromDate:now]];
[now release];
[format release];
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
NSString *urlString = @"";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@POST];

NSString *boundary = [NSString stringWithString:@---------------------------14737809831466499882746641449];
NSString *contentType = [NSString stringWithFormat:@multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @Content-Type];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Disposition: form-data; name=\userfile\"; filename=\"%@.jpg\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]]; <----------- this line gives the error
[body appendData:[[NSString stringWithString:@Content-Type: application/octet-stream\r\n\r\n] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@%d, [body length]] forHTTPHeaderField:@Content-Length];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


Look forward to some active help on this one. Any kind of assistance will be immensely appreciated.

Many Thanks


  • kapps11kapps11 Posts: 732Registered Users @ @ @
    edited July 2012
    the error is pretty simple actually. in that line you call the NSString method StringWithString:, which takes one argument. You are trying to pass it two when at the end of the first string you have , imageName. That is where you are passing two parameters to a method that only takes one. You need to combine that string and imageName in a previous line. Don't try to do everything on the same line, it makes code very difficult to read/maintain and you get weird errors like this.

    EDIT: appdesignify's method is smarter, use StringWithFormat. But still don't try to do everything on one line.
  • appdesignifyappdesignify Posts: 39New Users
    edited July 2012
    You want to use stringWithFormat here instead of stringWithString if you want to pass in the argument imageName. The following should fix you up.
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]];
    <head><title>iPhone App Templates</title></head>
    <body style="font-family:arial; font-size:14px;">
    <a href="">app templates</a>
    <a href=""><img src=""; align="left" border="0"></a>
  • proindigoproindigo Posts: 34Registered Users
    Yes, Now I got the error fixed. Actually I know NOTHING of Objective C. Just about writing my first few lines of code. So you can guess. Another thing I'd like to to know, since I am attempting to create a camera app and simulator doesn't have camera support, is there any way possible for me to test the code and the application as a whole I am writing?

    Many Thanks.
  • appdesignifyappdesignify Posts: 39New Users
    If you are creating an app that utilizes the camera feature then you will definitely need to develop on a device rather than the simulator. You really should not rely on the simulator anyway since your app is not guaranteed to work on a device just because it works on the simulator.

    The simulator is running on your Mac so it has a lot more memory, is much faster, and there can be plenty of issues that you will encounter on an actual device that you will never find using the simulator to test.
    <head><title>iPhone App Templates</title></head>
    <body style="font-family:arial; font-size:14px;">
    <a href="">app templates</a>
    <a href=""><img src=""; align="left" border="0"></a>
  • proindigoproindigo Posts: 34Registered Users
    Does that mean I will have to enroll to the Apple Developers Program with a subscription of 99$/Year?
  • proindigoproindigo Posts: 34Registered Users

    it works on the simulator

    How?? Any internet resource that I refer to says that Simulator doesn't have support for Camera testing. Is there a workaround?
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @

    it works on the simulator

    The camera works on the simulator? I've never seen it do so. I'm developing an app right now that depends heavily on the camera, so if you have a way to use a Mac iSight camera in the sim, please share!

    Duncan C

    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    The simulator does not interface to the isight camera to the best of my knowledge. Since, I cannot pull up the camera for a picture, The way I test photo capture through the simulator is with the following code.

    imagePickerController = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; } else { imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } imagePickerController.allowsEditing = NO; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:Nil];
Sign In or Register to comment.