Skip to content

Instantly share code, notes, and snippets.

@ArchieR7
Last active March 30, 2020 20:05
Show Gist options
  • Save ArchieR7/fc45e5647eb758ac90ca77349d4026e2 to your computer and use it in GitHub Desktop.
Save ArchieR7/fc45e5647eb758ac90ca77349d4026e2 to your computer and use it in GitHub Desktop.
Alamofire singleton example
//
// APIRequest.swift
//
// Created by 家齊 on 2016/12/27.
// Copyright © 2016年 Archie Chang. All rights reserved.
//
import Alamofire
class APIRequest {
static let shared = APIRequest()
private let manager: Alamofire.SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
return Alamofire.SessionManager(configuration: configuration)
}()
func getYoutubeList(completHandler: @escaping ([YoutubeItem]) -> ()) {
manager.request("").responseJSON(completionHandler: {
response in
guard let responseDictionary = response.result.value as? [String: AnyObject] else {
return
}
var returnItems = [YoutubeItem]()
if let items = responseDictionary["items"] as? [[String: AnyObject]] {
for itemFromServer in items {
if let title = itemFromServer["title"] as? String,
let url = itemFromServer["url"] as? String {
returnItems.append(YoutubeItem(title: title, url: url))
}
}
}
completHandler(returnItems)
})
}
}
struct YoutubeItem {
var title: String
var url: String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment