libc++abi.dylib:以 NSException Alamofire 类型的未捕获异常终止

libc++abi.dylib: terminating with uncaught exception of type NSException Alamofire

我正在使用 alamofire 将图像发送到服务器。如果参数;

    let parameters: Parameters = [
        "userID": "1",
        "acToken": "acToken"
    ]

工作但有参数;

    let parameters: Parameters = [
        "userID": userID,
        "acToken": acToken ?? ""
    ]

不工作。 error = libc++abi.dylib: 以 NSException

类型的未捕获异常终止

我正在使用 swift4、alamofire 4、Xcode9.2

我的上传方式:

func profilePhotoUpload(){
        let defaults = UserDefaults.standard
        let userID = defaults.integer(forKey: "userID")
        let acToken = defaults.string(forKey: "acToken")
        print("\(userID) - \(acToken ?? "")")

            // add UI related changes here
            let image = self.profilePhoto.image

            let parameters: Parameters = [
                "userID": userID,
                "acToken": acToken ?? ""
            ]

            Alamofire.upload(multipartFormData: { (multipartFormData) in
                multipartFormData.append(UIImageJPEGRepresentation(image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
                for (key, value) in parameters {
                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                }
            }, to:"http......")
            { (result) in
                switch result {
                case .success(let upload, _, _):

                    upload.uploadProgress(closure: { (progress) in
                        //Print progress
                    })

                    upload.responseJSON { response in
                        print(response.result)
                        //print response.result
                    }

                case .failure(let encodingError):
                    print(encodingError.localizedDescription)
                    break
                    //print encodingError.description
                }
            }
    }

错误日志:

VoiceStage[33099:1632858] -[NSCFNumber dataUsingEncoding:]: 无法识别的选择器发送到实例 0xb000000000000013 2018-03-26 16:20:16.686009+0300 VoiceStage[33099:1632858] *** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[__NSCFNumber dataUsingEncoding:]:无法识别选择器发送到实例 0xb000000000000013' *** 首先抛出调用栈: ( 0 核心基础 0x000000010520f12b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001048a3f41 objc_exception_throw + 48 2 CoreFoundation 0x0000000105290024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 核心基础 0x0000000105191f78 ___forwarding_ + 1432 4 核心基础 0x0000000105191958 _CF_forwarding_prep_0 + 120 5 语音舞台 0x0000000103d09a7a T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU + 1354 6 语音舞台 0x0000000103d0f9a4 _T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU_TA + 84 7 阿拉莫火 0x000000010415a994 _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_ + 212 8 阿拉莫火 0x000000010415e2bc _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_TA + 156 9 阿拉莫火 0x00000001040e0599 _T0Ix_IyB_TR + 41 10 libdispatch.dylib 0x000000010a10d2f7 _dispatch_call_block_and_release + 12 11 libdispatch.dylib 0x000000010a10e33d _dispatch_client_callout + 8 12 libdispatch.dylib 0x000000010a11a3a2 _dispatch_root_queue_drain + 1444 13 libdispatch.dylib 0x000000010a119da0 _dispatch_worker_thread3 + 132 14 libsystem_pthread.dylib 0x000000010a5d41ca _pthread_wqthread + 1387 15 libsystem_pthread.dylib 0x000000010a5d3c4d start_wqthread + 13 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

-[NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance

错误消息说:我试图调用方法 dataUsingEncoding:(Objective-C 版本一),即 Swift data(using:) 上的 [=16] =] 对象。不行,我崩溃了。 所以这意味着在某些时候你认为你正在使用一个 String 对象,而实际上它是一个 Number 对象(或者从 IntNumber 的转换)。

multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

您假设 value 响应 data(using:)。 好吧其实应该更多(value as String)。 管理更多案例将是测试 value 的 class。如果它是一个 String 对象,使用 data(using:),如果它已经是一个 Data 对象,只需输入 value.

在伪代码中:

let valueToSet;
if value is a String Object {
   valueToSet = (value as String).data(using:.utf8)
} else if value is a Data Object {
   valueToSet = value
} else if value is a Number Object {
   valueToSet = String(initWithNumber:value).data(using:.utf8)
} else ... ?

    multipartFormData.append(valueToSet, withName: key)

你还写:

let parameters: Parameters = [
    "userID": userID,
    "acToken": acToken ?? ""
]

所以在所有关于错误的解释之后: userID 不是字符串或 acToken 不是字符串而是 Int。根据名称,我倾向于说 userID 是一个 Int.