【问题标题】:How to get a post value on ASIFormDataRequest in DidFinishSelector method?如何在 DidFinishSelector 方法中获取 ASIFormDataRequest 的发布值?
【发布时间】:2012-02-23 21:16:05
【问题描述】:

Objective-C/iOS 新手在这里...

鉴于以下部分代码...

...
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myUrl];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(callComplete:)];
    [request setPostValue:@"Dilbert" forKey:@"name"];
...

...和...

- (void) callComplete:(ASIFormDataRequest*)request {
    // ? Determine what the value of the name parm was ?
}

...如何在 callComplete 处理程序中获取 name post 参数的值?

我原以为会有类似 [request postValueForKey:] 方法的东西,但没有。因此,除非有人知道这样做的方法,否则我想我只需要复制数据,可以是 ivars 的形式,也可以是请求中的 userInfo。

【问题讨论】:

    标签: objective-c ios asihttprequest


    【解决方案1】:

    你可以在类中编写你自己的方法来返回你想要的,但是像你一样 - 我没有看到一个内置的方法来做到这一点。

    请注意 - ASIHttpRequest(使用起来既快速又简单)不再被开发。这个框架不符合 ARC 标准。您可能想考虑改用其他框架(CF-Networking 或类似的东西)。

    【讨论】:

      【解决方案2】:

      我没有尝试过,但这可能有效:

      - (void) callComplete:(ASIFormDataRequest*)request {
      
          NSString *name;
          for (NSDictionary *parameter in [request postData]) {
              if ([[parameter objectForKey:@"key"] isEqualToString:@"name"]) {
                  name = [parameter objectForKey:@"value"];
                  break;
              }
          }
      }
      

      【讨论】:

      • ASIFormDataRequest中没有postData这个方法。
      【解决方案3】:

      试试这样:

      - (void) callComplete:(ASIFormDataRequest*)request {  
      
           NSMutableData *postBody=[request postBody]; 
      
           NSString *stringPostBody = [[NSString alloc] initWithData:postBody   
      
           encoding:NSUTF8StringEncoding];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 2014-11-05
        • 1970-01-01
        • 2020-07-24
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        相关资源
        最近更新 更多