简体   繁体   English

Swift中的完成区块AFNetworking

[英]Completion Block AFNetworking in Swift

I'm working on an iOS app whose the basis is Objective-C with AFNetworking for communicate with the API. 我正在使用基于AFNetworking的Objective-C与iOS进行通信的iOS应用。 Now I'm writing my new ViewControllers in Swift and have some problems with the CompletionBlock in the Swift files. 现在,我正在用Swift编写新的ViewController,并且在Swift文件中的CompletionBlock遇到了一些问题。

The original Objective-C code: 原始的Objective-C代码:

- (void)downloadJson {
    [[ODDWebService sharedInstance] getParkingSpacesWithCompletionBlock:^(NSArray *result, BOOL succes) {
        if(succes) {
            [self.parkings removeAllObjects];

            for(NSDictionary *dict in result) {
                Parking *parking = [[Parking alloc] init];
                parking.name = [dict objectForKey:@"name"];
                parking.freespace = [[dict objectForKey:@"freespace"] integerValue];
                [self.parkings addObject:parking];
            }
            [self updateParkings];
        } else {
            NSLog(@"ERROR RETRIEVING PARKING SPACES");
        }
    }];
}

This is what I thought it should be in Swift: 这就是我认为应该在Swift中执行的操作:

func downloadJson() {
    ODDWebService.sharedInstance().getParkingSpacesWithCompletionBlock { (result: AnyObject, succes: Bool) -> Void in
        if(succes) {
            self.parkings.removeAllObjects()

            for dict: NSDictionary in result {
                var parking: Parking = Parking
                parking.name = dict["name"]
                parking.freespace = dict["freespace"].integerValue()
                parkings.addObject(parking)
            } else {
                NSLog("ERROR RETRIEVING PARKING SPACES")
            }
        }
    }
}

The main error is found in the second line of code: 主要错误在第二行代码中找到:

'(AnyObject, Bool) -> Void' is not convertible to '(([AnyObject]!, Bool) -> Void)!'

You are trying to cast NSArray to Anyobject. 您正在尝试将NSArray强制转换为Anyobject。 Can you try this code: 您可以尝试以下代码:

func downloadJson() {
    ODDWebService.sharedInstance().getParkingSpacesWithCompletionBlock { (result: [AnyObject]!, success: Bool) -> Void in
        if(succes) {
            self.parkings.removeAllObjects()

            for dict in result as NSArray {
                var parking: Parking = Parking
                parking.name = dict["name"]
                parking.freespace = dict["freespace"].integerValue()
                parkings.addObject(parking)
            } else {
                NSLog("ERROR RETRIEVING PARKING SPACES")
            }
        }
    }
}

Replace 更换

func downloadJson() {
ODDWebService.sharedInstance().getParkingSpacesWithCompletionBlock { (result: AnyObject, succes: Bool) -> Void in
    if(succes) {

With

func downloadJson() {
ODDWebService.sharedInstance().getParkingSpacesWithCompletionBlock {
result, success  in
    if(succes) {

Also add the first line in the following code to your code. 还将以下代码中的第一行添加到您的代码中。

if let dicts = result as? NSDictionary<String, AnyObject> {
for dict: NSDictionary in dicts {
            var parking: Parking = Parking
            parking.name = dict["name"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM