难以理解客观的c代码

时间:2015-08-11 11:34:14

标签: ios objective-c iphone

我一直在尝试实施follow code,但我很难理解以下代码:

- (void)getRoutesWithStopName:(NSString *) stopName
            success:(void (^)(NSArray *routes))success
            error:(void (^)(NSString *errorMsg)) error
{

    [[self AFManagerObject] POST:GET_ROUTES
       parameters:@{@"params" : @{ @"stopName":    [NSString stringWithFormat:@"%%%@%%",[stopName lowercaseString]]} }
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSArray *routesRows = responseObject[@"rows"];
              NSMutableArray *routes = [[NSMutableArray alloc] initWithCapacity:routesRows.count];

              for(NSDictionary *dicRoute in routesRows)
              {
                  FLBRoute *route = [[FLBRoute alloc] initWithAttrs:dicRoute];
                  [routes addObject:route];
              }

              success(routes);

          }
          failure:^(AFHTTPRequestOperation *operation, NSError *err) {
              error(err.description);
          }
     ];

}

我尝试了解积木,但我仍然无法理解这里发生了什么。你能为我提供一步一步的代码解释吗?

2 个答案:

答案 0 :(得分:2)

实际上这里用于webserviceCall

<强>步骤-1

- (void)getRoutesWithStopName:(NSString *) stopName
        success:(void (^)(NSArray *routes))success
        error:(void (^)(NSString *errorMsg)) error 

//此处传递一个NSString并使用NSArray获取响应,并使用NSString

失败

<强>步骤-2

//此处使用AFNEtworking来拨打电话web service

//request block
{p> [self AFManagerObject] - NSObject方法位置的AFNetworking类。

POST:GET_ROUTES - &gt; post是请求类型的默认函数,GET_ROUTES - &gt; your Macro class for Request URL

parameters --> send the parameter to server

[[self AFManagerObject] POST:GET_ROUTES
   parameters:@{@"params" : @{ @"stopName":    [NSString stringWithFormat:@"%%%@%%",[stopName lowercaseString]]} }
      success:^(AFHTTPRequestOperation *operation, id responseObject) 
{
 /***********  success response serlize and store into Array**********/

  NSArray *routesRows = responseObject[@"rows"];
          NSMutableArray *routes = [[NSMutableArray alloc] initWithCapacity:routesRows.count];

          for(NSDictionary *dicRoute in routesRows)
          {
              FLBRoute *route = [[FLBRoute alloc] initWithAttrs:dicRoute];
              [routes addObject:route];
              // this is your NSObject class for save the details , 
          }

          success(routes);

      /**************  success stop **********/

   }
  /***********  error if request is fail ************/
  failure:^(AFHTTPRequestOperation *operation, NSError *err) {
          error(err.description);
      }
 ];
   /***********  error if request is stop ************/

答案 1 :(得分:1)

我认为你需要阅读更多关于回调https://en.m.wikipedia.org/wiki/Callback_(computer_programming)和阻止https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.htmlhttps://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html的内容 基本上该方法发送POST请求,并且您知道将请求发送到服务器和服务器响应需要一些时间。您不希望此时您的应用程序被冻结,因此使用2个回调,1个用于成功案例,1个用于失败案例。块回调只是一个代码块,您希望稍后执行,当服务器响应成功或失败时。