将传递的值从另一个ViewController分配给字符串时出现问题

时间:2014-06-13 12:56:33

标签: ios objective-c ios7 nsstring nsurlrequest

我从另一个ViewController获得了一个值。我可以在日志中打印这个,但问题是我得到了这种格式的值

 (
     201  )

我从JSON数据中获取此值。我无法在UITextField中获取值。同时它不使用urlstring,即我在下面的代码中提到了请求的字符串。

NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@&AgeRange=%@",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,gender,imagename,ageTextfield.text];

NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/InsertFacialImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.


NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];

NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"Ret: %@",returnString);

我需要运行NSUrlRequest以及该字符串。

3 个答案:

答案 0 :(得分:1)

如果您的日志是NSLog的结果,那么您的号码似乎被封装到一个数组中。尝试将您正在记录的变量强制转换为NSArray,检索第一个对象并将其转换为整数。

答案 1 :(得分:0)

当您从JSON数据中获得类似Array格式的值时。您需要在NSArray中获取它们并通过检索数组的第一个对象获取值,如下面的代码。因为我不知道你是如何获得这个价值的。从字典或其他来源..

NSArray *tempArray=[[NSArray alloc]initWithObjects:@"201", nil]; //Replace this the code you are getting that value(or)
NSArray *tempArray=[dict objectForKey:@"Value"];//If you getting from dict
NSLog(@"@@@@@@@@@Value from Array is:%@",[tempArray objectAtIndex:0]);
NSString *userIdString=[tempArray objectAtIndex:0];//If you want value in string format
int userId=[userIdString intValue];// If you want in Integer format

希望它可以帮助你..

答案 2 :(得分:0)

您的JSON数据格式错误。获取的JSON数据包括with()。检查http://jsonviewer.stack.hu/中的JSON。连接中的更改确实完成加载

NSError *err;
NSString *JSON = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
  NSLog(@"jsonImage:%@" , JSON);

jsonDi = [NSJSONSerialization JSONObjectWithData:receivedData options: NSJSONReadingMutableContainers error: &err];

compareId = [jsonDi valueForKey:@"ImageId"];