ios - Completion Block AFNetworking in Swift -


i'm working on ios app basis objective-c afnetworking communicate api. i'm writing new viewcontrollers in swift , have problems completionblock in swift files.

the original objective-c code:

- (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 thought should in swift:

func downloadjson() {     oddwebservice.sharedinstance().getparkingspaceswithcompletionblock { (result: anyobject, succes: bool) -> void in         if(succes) {             self.parkings.removeallobjects()              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 found in second line of code:

'(anyobject, bool) -> void' not convertible '(([anyobject]!, bool) -> void)!' 

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 first line in following code code.

if let dicts = result as? nsdictionary<string, anyobject> { dict: nsdictionary in dicts {             var parking: parking = parking             parking.name = dict["name"] 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -