2014-10-29 11 views
5

Ho un APIService Singleton come qui di seguitofunzione Swift parametri

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) 
        } 
       }         
     } 
    } 
} 

Mentre sto chiamando questa funzione in altro luogo, come ad esempio un UIViewController, come questo, è ok

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     APIService.instance.currentWeather("San Gabriel", success: nil, failure: {error in println(error)}) 


    } 

} 

Tuttavia è la compilazione di errore quando chiamo la func come questo

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> 

Se metto la funzione proprio nel UIViewController , tutto è ok di nuovo, troppo cablato, ho bisogno di aiuto

+0

sembra che qualcosa di relativo a JSON struct di SwiftyJSON, ma sto ancora confondono perché? Il compilatore – xmkevinchen

+0

si è arrestato. tempo di presentare un bug report –

+1

Mi sono imbattuto nello stesso errore del compilatore con la struttura JSON di SwiftJSON. Ho creato un'API singleton come te che funziona solo su test case e non funziona su alcun controller. – yuhua

risposta

0

Non credo sia la risposta.

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

Dovrebbe essere

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

'errore? (Errore)' sta bene, 'fallimento ?? fallimento (errore) 'non lo è –