【发布时间】:2014-06-12 19:39:47
【问题描述】:
在调用异步请求时,我尝试使用 Swift 的 闭包,就像 ObjC 中的完成块一样。
这似乎有效。我正在为我的模型类使用protocol,并与Array 结合使用我遇到了问题。相关代码:
//ModelProtocol.swift
protocol ModelProtocol {
// all my models should implement `all`
class func all(completion: ((models: Array<ModelProtocol>) -> Void) )
}
//Person.swift
// calls the HTTP request and should return all Person-Objects in `completion`
class func all(completion: ((models: Array<ModelProtocol>) -> Void) ) {
let request = HTTPRequest()
request.getAll() { (data:NSArray) in
var persons:Person[] = //... `data` is the result from the HTTP GET request and will be parsed here - this is ok
completion(models: persons)
}
}
//HTTPRequest.swift
func getAll(completion: ((data: NSArray) -> Void) ) {
//... some setup would be here
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
var jsonResponse: NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray
completion(data: jsonResponse)
}
}
//ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
// use this whole code here - receive all Persons and show in a tableView or something like this
Person.all( { (models: Array<ModelProtocol>) in
println(models) //CRASH here
})
}
当我将函数 all 的 protocol 定义(以及 Person.swift 中的函数 all)更改为 class func all(completion: ((models: Person[]) -> Void) ) 时,它正在工作。
但我想使用Array<ModelProtocol> 来使用多态性,并且只使用符合ModelProtocol 的类,可以是Person 或House 或其他。
我想我在这里遗漏了一些重要或基本的东西。我希望我的问题足够清楚。
编辑:
在ViewController.swift 中,应用程序的执行在println(models) 语句处停止,消息为EXC_BAD_ACCESS。
【问题讨论】:
-
能否请您发布崩溃的错误信息?
-
你试过
ModelProtocol[]而不是Array<ModelProtocol>吗? -
ModelProtocol[]给出同样的错误信息
标签: ios polymorphism swift