Rohit Goyal
Rohit Goyal

Reputation: 1539

SIGABRT ABORT crash on iOS swift

I am getting a SIGABRT ABORT crash at the line mentioned below the comment(let data = fbResult.objectForKey("data") as! NSMutableArray). I am just trying to get user's score saved over Facebook. Please note that I am not able to reproduce this issue and this is happening on a very few devices. I just got the issue through crash reports.

var fbScore = 0
func getFBScore(){
    if(FBSDKAccessToken.currentAccessToken() != nil){
        var request = FBSDKGraphRequest(graphPath: "me/scores?fields=score", parameters: nil)
        request.startWithCompletionHandler({(connection, result, error) -> Void in
            if(error != nil){
                println("facebook score reading failed with error \(error)")

            } else {
                println("facebook score reading result \(result)")
                let fbResult = result as! NSDictionary
                //crash on below line
                let data = fbResult.objectForKey("data") as! NSMutableArray
                if(data.count == 0){
                    self.postFBScore(0)
                } else {
                    if let mScore = data[0].objectForKey("score") as? Int{
                        self.fbScore = mScore
                        println("facebook score is \(self.fbScore)")
                    } else{
                        self.postFBScore(0)
                    }

                }
            }

        })
    }
}

This is the crash report I have got.


    Thread : Crashed: com.apple.main-thread
    0  libsystem_kernel.dylib         0x00000001954db270 __pthread_kill + 8
    1  libsystem_pthread.dylib        0x0000000195579160 pthread_kill + 112
    2  libsystem_c.dylib              0x0000000195452aa0 abort + 112
    3  libswiftCore.dylib             0x0000000100a6dd80 swift::fatalError(char const*, ...) + 182
    4  libswiftCore.dylib             0x0000000100a4aefc swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 70
    5  libswiftCore.dylib             0x0000000100a4af80 swift::swift_dynamicCastFailure(swift::Metadata const*, swift::Metadata const*, char const*) + 130
    6  libswiftCore.dylib             0x0000000100a6f6dc swift_dynamicCastObjCClassUnconditional + 72
    7  MY_APP                     0x00000001000e5c70 MY_APP.GameScene.(getFBScore (MY_APP.GameScene) -> () -> ()).(closure #1) (GameScene.swift:1600)
    8  MY_APP                     0x00000001003d6ea4 -[FBSDKGraphRequestMetadata invokeCompletionHandlerForConnection:withResults:error:] (FBSDKGraphRequestMetadata.m:48)
    9  MY_APP                     0x00000001003ddef0 __82-[FBSDKGraphRequestConnection processResultBody:error:metadata:canNotifyDelegate:]_block_invoke394 (FBSDKGraphRequestConnection.m:719)
    10 MY_APP                     0x00000001003ddca0 -[FBSDKGraphRequestConnection processResultBody:error:metadata:canNotifyDelegate:] (FBSDKGraphRequestConnection.m:768)
    11 MY_APP                     0x00000001003dd768 __64-[FBSDKGraphRequestConnection completeWithResults:networkError:]_block_invoke (FBSDKGraphRequestConnection.m:697)
    12 CoreFoundation                 0x00000001832b0d48 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 132
    13 CoreFoundation                 0x00000001832b0c2c -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 312
    14 MY_APP                     0x00000001003dd500 -[FBSDKGraphRequestConnection completeWithResults:networkError:] (FBSDKGraphRequestConnection.m:679)
    15 MY_APP                     0x00000001003dc950 -[FBSDKGraphRequestConnection completeFBSDKURLConnectionWithResponse:data:networkError:] (FBSDKGraphRequestConnection.m:544)
    16 MY_APP                     0x00000001003da910 __36-[FBSDKGraphRequestConnection start]_block_invoke_2 (FBSDKGraphRequestConnection.m:188)
    17 MY_APP                     0x00000001003d76ec -[FBSDKURLConnection invokeHandler:error:response:responseData:] (FBSDKURLConnection.m:104)
    18 MY_APP                     0x00000001003d7644 -[FBSDKURLConnection logAndInvokeHandler:response:responseData:] (FBSDKURLConnection.m:96)
    19 MY_APP                     0x00000001003d7a28 -[FBSDKURLConnection connectionDidFinishLoading:] (FBSDKURLConnection.m:143)
    20 CFNetwork                      0x0000000182e391e8 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 80
    21 CFNetwork                      0x0000000182e39178 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 208
    22 CFNetwork                      0x0000000182e392f4 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 60
    23 CFNetwork                      0x0000000182d0bdfc ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 104
    24 CFNetwork                      0x0000000182dd6120 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 104
    25 CFNetwork                      0x0000000182cf7efc RunloopBlockContext::_invoke_block(void const*, void*) + 76
    26 CoreFoundation                 0x000000018329c97c CFArrayApplyFunction + 68
    27 CFNetwork                      0x0000000182cf7da8 RunloopBlockContext::perform() + 136
    28 CFNetwork                      0x0000000182cf7c5c MultiplexerSource::perform() + 312
    29 CFNetwork                      0x0000000182cf7a88 MultiplexerSource::_perform(void*) + 68
    30 CoreFoundation                 0x0000000183373f8c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    31 CoreFoundation                 0x0000000183373230 __CFRunLoopDoSources0 + 264
    32 CoreFoundation                 0x00000001833712e0 __CFRunLoopRun + 712
    33 CoreFoundation                 0x000000018329cf74 CFRunLoopRunSpecific + 396
    34 GraphicsServices               0x000000018cba76fc GSEventRunModal + 168
    35 UIKit                          0x0000000187e9ed94 UIApplicationMain + 1488
    36 MY_APP                     0x00000001000f6c58 main (AppDelegate.swift:16)
    37 libdyld.dylib                  0x00000001953c2a08 start + 4 

When I try to print fbResult on my console this is what I get.

{ data = ( { score = 41; user = { id = 10206371034343540; name = "Rohit Goyal"; }; } ); }

Upvotes: 1

Views: 4113

Answers (2)

Seema Nair
Seema Nair

Reputation: 165

Try replacing it like this:

Current:

let data = fbResult.objectForKey("data") as! NSMutableArray

New:

let data = fbResult.objectForKey("data").mutablecopy() as! NSMutableArray

Upvotes: 0

Renzo
Renzo

Reputation: 27434

Since there is an exception at this line:

let data = fbResult.objectForKey("data") as! NSMutableArray

given the fact that the Swift is a strongly and static typed language, the only possible cause of error is the use of the ! operator to cast the object to the type NSMutableArray. So, if you know that the value bound to the key data is an array, you should try to cast it to a non mutable array:

let data = fbResult.objectForKey("data") as! NSArray

Upvotes: 1

Related Questions