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
Post a Comment