2017-06-15 40 views
2

與協議操場周圍面向網絡代碼,並在最後一行,我說斯威夫特協議導向網絡代碼崩潰的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 
+0

我的第一個建議離子將是使它成爲一個編譯的項目。然後,你可以設置斷點和調試你的代碼... –

+0

@MartinR我做了,它只是在它成功建設之前seg故障 –

回答

4

崩潰的編譯器是一個錯誤,應該報告。 Xcode的9(試用版)不 崩潰與您的代碼,但給有用的錯誤信息導致的解決方案:

 
error: generic parameter 'Content' could not be inferred 
WebService().load(resource: RepoService) { res in 
      ^

通用佔位Contentclass WebService<Content>是 都沒有用,刪除,使該類非通用的。然後:

 
error: generic parameter 'R' could not be inferred 
WebService().load(resource: RepoService) { res in 
      ^

你要的RepoService傳遞一個實例到load方法, 和res內封閉是可選的。

隨着這些變化

final class WebService { 
    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 
    guard let res = res else { return } 
    for elem in res { 
     print(elem) 
    } 
} 

你的代碼編譯和運行符合預期,在Xcode 8.3.3和9

+0

欣賞它。我將確保繼續並將其報告爲Xcode 8.3.3的缺陷 –

0

工作示例

import Foundation 

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 { 
    func load<R: Resource>(resource: R, completion: @escaping (R.Content) ->()) { 
     URLSession.shared.dataTask(with: resource.url) { data, _, _ in 
      if 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 

給了我這個結果

Repo(id: 93473150, name: "bitbuf") 
Repo(id: 28517340, name: "DIJKASTRAS") 
Repo(id: 71310619, name: "git-strbuf") 
Repo(id: 30558002, name: "Habitats-master") 
Repo(id: 28517464, name: "Home-Automation-Project") 
Repo(id: 33394787, name: "hw.") 
Repo(id: 39911528, name: "JPSThumbnailAnnotation") 
Repo(id: 37162894, name: "M3") 
Repo(id: 39709018, name: "Maroon") 
Repo(id: 41267183, name: "mkchoi212.github.io") 
Repo(id: 28519262, name: "Money-Saving-Meals") 
Repo(id: 39857111, name: "MRCircularProgressView") 
Repo(id: 81773538, name: "papers-we-love") 
Repo(id: 37074317, name: "socket.io-client-swift") 
Repo(id: 28519322, name: "Wordsneak")