目录
目录README.md

Apic

Cocoapods Platform License codebeat badge

Apic communicates with RESTful services, parses the JSON HTTP response and delivers objects.

Installation

CocoaPods

Swift < 2.3:

pod ‘Apic’ ‘~> 2.2.4’

Swift 3.x :

pod ‘Apic’ ‘~> 3.9.6’

Swift 4

pod ‘Apic’ ‘> 4.0.0’ pod ‘Apic’ ‘> 5.0.0’

Repositories

The repository is a class that implements the logic to communicate with the services that provide the REST resources, basically a Repository has methods that correspond to endpoints in the backend, all repositories inherit from the generic class AbstractRepository:

class GistsRepository: AbstractRepository {
    func requestGistsOfUser(_ user: String, completion: ([Gist]) -> Void) -> Request<[Gist]> {
        return requestArray(route: .get("https://api.github.com/users/\(user)/gists"), completion: completion)
    }
}

The only requirement is that Gist implements Decodable

This repository can now be used by a ViewController to request a user’s gists.

class GistsController: UITableViewController {

    let gistsRepository = GistsRepository()
    var gists: [Gist]?
    var gistsRequest: Request<[Gist]>?

    override func viewDidLoad() {
        super.viewDidLoad()
        requestGists()
    }

    func requestGists() {
        gistsRequest?.cancel()
        gistsRequest = gistsRepository.requestGistsOfUser("UserName", completion: { [weak self] gists in
            self?.gists = gists
        }).fail { [weak self] error in
            self?.handleError(error)
        }.finished { [weak self] in
            self?.tableView.reloadData()
        }
    }
}

When the GistsRepository finishes calling the service and parsing the response it calls the completion closure that you provide sending you an array of [Gist] in this case.

关于
391.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号