與協議操場周圍面向網絡代碼,並在最後一行,我說斯威夫特協議導向網絡代碼崩潰的Xcode
LLDB RPC服務器已經崩潰帶來的一切融合在一起時,Xcode崩潰...等在
在我的代碼中肯定有一些很大的錯誤,我確定它在最後幾行,但我無法弄清楚。
import UIKit
import PlaygroundSupport
struct Repo {
let id: Int
let name: String
}
extension Repo {
init?(dict: JSONDict) {
guard let id = dict["id"] as? Int, let name = dict["name"] as? String else { return nil }
self.id = id
self.name = name
}
}
typealias JSONDict = [String:Any]
protocol Resource {
associatedtype Content
var url: URL { get }
func parse(data: Data) -> Content?
}
extension Resource {
func parseJSON(data: Data) -> Any? {
let json = try? JSONSerialization.jsonObject(with: data)
return json
}
}
struct RepoService: Resource {
let url = URL(string: "https://api.github.com/users/mkchoi212/repos")!
func parse(data: Data) -> [Repo]? {
guard let dictArray = self.parseJSON(data: data) as? [JSONDict] else { return nil }
return dictArray.flatMap(Repo.init)
}
}
final class WebService<Content> {
func load<R: Resource>(resource: R, completion: @escaping (R.Content?) ->()) {
URLSession.shared.dataTask(with: resource.url) { data, _, _ in
let res = data.flatMap(resource.parse)
completion(res)
}.resume()
}
}
WebService().load(resource: RepoService) { res in
for elem in res {
print(elem)
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
我的第一個建議離子將是使它成爲一個編譯的項目。然後,你可以設置斷點和調試你的代碼... –
@MartinR我做了,它只是在它成功建設之前seg故障 –