5

I've a singleton APIService like below

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

While I'm calling this function in other place, such as an UIViewController, like this, it's ok

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


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


    }

}

However it's compile error when I'm calling the func like this

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>

If I put the function just in the UIViewController, everything is ok again, too wired, need help

xmkevinchen
  • 1,506
  • 1
  • 14
  • 17

1 Answers1

0

I don't think it's the answer.

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

Should be

if error != nil {
   failure ?? failure(error) // or: failure?(error)
} else {
...
tangplin
  • 147
  • 4