??Swift??д??????????
???????????? ???????[ 2016/6/16 15:08:21 ] ??????????????? ???????
???????????????????????????????????????????????????ù???????????????????д?????? Swift-Style ????????????????β??????μ?????????????????????????????????????????Щ???????????????????????????????????????????????????μ????????????????????? C# ???д????????? iOS ???????????????????????????????????????????????????????? Swift ???????????????????????????? Swift ?????????????????????????????????????????? OOP ????д??????????Щ?鷳???????????β????д?????????ù??????“α?????”???в???????????????????????????????????????????????????????
?????????????????????????о??????????????????????
????· ??????????????????????
????· ????????????漰???????????????????????
??????????????????????????????XCTest????expectationWithDescription+waitForExpectationsWithTimeout??????????
????func testFetchDataWithAPI_invalidAPI_failureResult() {
????let expectation = expectationWithDescription("")
????let timeout = 15 as NSTimeInterval
????NetworkManager
????.defaultManager
????.fetchDataWithAPI(.Invalid?? responseKey: "") {
????expectation.fulfill()
????XCTAssertTrue($0.isFailure)
????}
????waitForExpectationsWithTimeout(timeout?? handler: nil)
????}
????????????? test??????_???????_??????? ????????????????????????????expectationWithDescription??????????expectation??????????????????????????????????β???????????????????????????????????????????????????е???expectation.fulfill()?????????????????????????????ж??????????????????????waitForExpectationsWithTimeout(timeout?? handler: nil)??????????timeout??????????У??????????hander????????????????д?Щ?????????????????????????????????????task?.cancel()?????????????????????????????????????????????????
????????????????????????????????????????????е?????????????????????????????????????????????????
???????????????????????????е???????????????????????????????????????????????????????????????????????????硢???????????????????????
???????????????????????????????????????????????????????????粻?????????????????????????????????????????????????п???????????????????????????????????????????????????п?????????衣
?????????????????????????????????дд??????????? Alamofire ?в??????????????????в?????????????????????????????? Alamofire ?????????????????????????д????????????????????? Alamofire ??????Щ?????????????????????????? Alamofire ???ɡ?
??????????????????????
/**
Fetch raw object
- parameter api: API address
- parameter method: HTTP method?? default = POST
- parameter parameters: Request parameters?? default = nil
- parameter responseKey: Key of target value?? use '.' to get nested objects?? e.g. "data.vehicle_list"
- parameter jsonArrayHandler: Handle result with raw object
- returns: Optional request object which is cancellable.
*/
func fetchDataWithAPI(api: API??
method: Alamofire.Method = .POST??
parameters: [String: String]? = nil??
responseKey: String??
networkCompletionHandler: NetworkCompletionHandler) -> Cancellable? {
guard let url = api.url else {
printLog("URL Invalid: \(api.rawValue)")
return nil
}
return Alamofire.request(method?? url?? parameters: parameters).responseJSON {
networkCompletionHandler(self.parseResult($0.result?? responseKey: responseKey))
}
}
?????????????????????????????????
func testFetchDataWithAPI_invalidURL_returnNil {
let task = NetworkManager
.defaultManager
.fetchDataWithAPI(.InvalidURL?? responseKey: "") {}
XCTAssertNil(task)
}
func testFetchDataWithAPI_validAPI_returnNotNil {
let task = NetworkManager
.defaultManager
.fetchDataWithAPI(.ValidURL?? responseKey: "") {}
XCTAssertNotNil(task)
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11