Swift型铸造问题

时间:2015-10-20 12:34:58

标签: ios swift types casting crash

我有一个奇怪的崩溃(像1万次),这是一个代码:

typealias APIData = Dictionary <String, AnyObject>

static func performAction(action: APIAction, params: APIParams, needAuthorization: Bool = true, completion: APICompletion) -> AFHTTPRequestOperation? {
    var requestParameters: APIParams = ["request": params]

    if needAuthorization && sessionIsValid() {
        requestParameters["session_id"] = sessionID!
    }

    APILogger.logAction(action, params: requestParameters)

    return API.requestManager.POST(action.rawValue, parameters: requestParameters, success: { (operation, data) -> Void in

        guard let _data = data as? APIData, errorCode = _data["errorCode"] as? Int, error = APIError(rawValue: errorCode) else {
            completion(state: .Failed, data: nil, error: .Undefined)
            return
        }

        if error == .NoError {
            if let responceData = _data["response"] as? APIData {
                APILogger.logData(_data)
                completion(state: .Success, data: responceData, error: error)
            } else {
                completion(state: .Success, data: nil, error: error)
            }
        } else {
            if error == .ExpiredSession {
                sessionID = nil
            }
            completion(state: .Failed, data: nil, error: error)
        }

        }) { (operation, error) -> Void in
            if operation.cancelled {
                completion(state: .Cancelled, data: nil, error: .Undefined)
            } else {
                completion(state: .Failed, data: nil, error: .Undefined)
            }
    }
}

堆栈追踪:

                Thread : Crashed: com.apple.main-thread
            0  libswiftCore.dylib             0x0000000100645cbc swift::_swift_getClass(void const*) + 24
            1  libswiftCore.dylib             0x000000010063d57c swift_getObjectType + 12
            2  libswiftCore.dylib             0x000000010062209c _dynamicCastFromExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::ExistentialTypeMetadata const*, swift::Metadata const*, swift::DynamicCastFlags) + 156
            3  libswiftCore.dylib             0x0000000100620e2c swift_dynamicCast + 496
            4  BenefitCalculator_iOS          0x000000010012a2ec static API.(performAction(API.Type) -> (APIAction, params : [String : AnyObject], needAuthorization : Bool, completion : (state : APIOperationState, data : [String : AnyObject]?, error : APIError) -> ()) -> AFHTTPRequestOperation?).(closure #1) (API.swift:43)
            5  BenefitCalculator_iOS          0x000000010012aa28 thunk (API.swift)
            6  BenefitCalculator_iOS          0x000000010012aa54 thunk (API.swift)
            7  BenefitCalculator_iOS          0x000000010012aab4 thunk (API.swift)
            8  libdispatch.dylib              0x0000000196fb1994 _dispatch_call_block_and_release + 24
            9  libdispatch.dylib              0x0000000196fb1954 _dispatch_client_callout + 16
            10 libdispatch.dylib              0x0000000196fb620c _dispatch_main_queue_callback_4CF + 1608
            11 CoreFoundation                 0x00000001850f37f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
            12 CoreFoundation                 0x00000001850f18a0 __CFRunLoopRun + 1492
            13 CoreFoundation                 0x000000018501d2d4 CFRunLoopRunSpecific + 396
            14 GraphicsServices               0x000000018e8336fc GSEventRunModal + 168
            15 UIKit                          0x0000000189be2fac UIApplicationMain + 1488
            16 BenefitCalculator_iOS          0x0000000100122f4c main (AppDelegate.swift:15)
            17 libdyld.dylib                  0x0000000196fdea08 start + 4

根据堆栈跟踪,它在这里崩溃:

guard let _data = data as? APIData, errorCode = _data["errorCode"] as? Int, error = APIError(rawValue: errorCode) else

重建这次崩溃真是太难了......

看起来Swift类型的渲染可能会崩溃,即使我做得正确(让_data =数据为?APIData),这是非常罕见的。如果有人能帮助我解决这个问题,我将非常感激。

0 个答案:

没有答案