【问题标题】:Issue with assigning the passed value from another ViewController to string将传递的值从另一个 ViewController 分配给字符串的问题
【发布时间】:2014-06-13 12:56:33
【问题描述】:

我从另一个 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。

【问题讨论】:

    标签: ios objective-c ios7 nsstring nsurlrequest


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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
      

      希望对你有帮助..

      【讨论】:

        【解决方案3】:

        您收到的 JSON 数据格式错误。您获取的 JSON 数据,包括 ()。检查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"];
        

        【讨论】:

          猜你喜欢
          • 2017-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-27
          相关资源
          最近更新 更多