2014-10-29 3 views
5

내가스위프트 기능은

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을했습니다 매개 변수 이 함수를 호출 할 때 컴파일 오류가 발생합니다.

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 , 다 다시 확인, 너무 유선, 도움이 필요합니다

+0

그것은 SwiftyJSON의 JSON 구조체를 기준으로 뭔가 것 같아,하지만 난 아직도 왜 혼동하고있어? – xmkevinchen

+0

컴파일러가 충돌했습니다. 버그 보고서를 작성하는 시간 –

+1

SwiftJSON의 JSON 구조체와 동일한 컴파일러 오류가 발생했습니다. 테스트 케이스에서만 작동하고 컨트롤러에서는 작동하지 않는 API 싱글 톤을 만들었습니다. – yuhua

답변

0

나는 그것이 대답이라고 생각하지 않습니다.

if error != nil && failure != nil { 
    failure!(error) 
} else { 
... 

일해야

if error != nil { 
    failure ?? failure(error) // or: failure?(error) 
} else { 
... 
+1

'실패? (오류)'괜찮습니다,'실패 ?? 실패 (오류)'가 아닙니다. –