5
我有一個單身APIService像下面斯威夫特功能參數
import Foundation
import Alamofire
import SwiftyJSON
typealias Success = JSON -> Void
typealias Failure = NSError? -> Void
class APIService {
private let BaseURL = "http://api.openweathermap.org/data/2.5"
class var instance: APIService {
struct Singleton {
static let instance: APIService = APIService()
}
return Singleton.instance
}
func currentWeather(cityName: String, success:Success?, failure:Failure?) {
let url = BaseURL + "/weather"
Alamofire.request(.GET, url, parameters:["q" : cityName])
.responseJSON { (_, _, jsonObject, error) in
if error != nil && failure != nil {
failure!(error)
} else {
let json = JSON(jsonObject!)
if success != nil {
success!(json)
} else {
println(json)
}
}
}
}
}
當我打電話給在其他地方,比如這個的UIViewController功能,這樣,它的確定
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
APIService.instance.currentWeather("San Gabriel", success: nil, failure: {error in println(error)})
}
}
然而它是編譯錯誤,當我打電話這樣的功能
APIService.instance.currentWeather("San Gabriel", success: {json in println(json)}, failure: {error in println(error)})
0 swift 0x0000000101f2da68 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x0000000101f2df54 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff95838f1a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fff5e9903d8 _sigtramp + 3373626584
4 swift 0x00000001013d1f8c swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 108
5 swift 0x00000001013f1829 swift::Lowering::SILGenFunction::visitBraceStmt(swift::BraceStmt*) + 297
6 swift 0x00000001013f48e8 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Stmt*) + 152
7 swift 0x00000001013c0da1 swift::Lowering::SILGenFunction::emitValueConstructor(swift::ConstructorDecl*) + 5617
8 swift 0x000000010139c04a swift::Lowering::SILGenModule::emitConstructor(swift::ConstructorDecl*) + 666
9 swift 0x000000010139d8bb swift::SILModule::constructSIL(swift::Module*, swift::SourceFile*, swift::Optional<unsigned int>) + 475
10 swift 0x000000010139d968 swift::performSILGeneration(swift::SourceFile&, swift::Optional<unsigned int>) + 72
11 swift 0x0000000101273e18 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 3432
12 swift 0x000000010127196d main + 1677
13 libdyld.dylib 0x00007fff93e515c9 start + 1
Stack dump:
0. Program arguments:
1. While silgen emitConstructor SIL function @_TFVSC20NSJSONReadingOptionsCfMS_FT_S_ for 'init' at <invalid loc>
如果我把函數只是在UIViewController ,一切都很好,太有線了,需要幫助
好像相對於SwiftyJSON的JSON結構的東西,但我還是搞不清爲什麼? – xmkevinchen 2014-10-29 23:48:10
編譯器崩潰。提交bug報告的時間 – 2014-10-30 02:34:42
我遇到了與SwiftJSON的JSON結構相同的編譯器錯誤。我做了一個像你一樣的API單例,它只能在測試用例上工作,並在任何控制器上失敗。 – yuhua 2014-11-27 08:30:50