问题描述
我正在使用alamofire将图像发送到服务器。如果参数;
I am sending images to the server using alamofire. If parameters ;
let parameters: Parameters = [
"userID": "1",
"acToken": "acToken"
]
正在工作但有参数;
let parameters: Parameters = [
"userID": userID,
"acToken": acToken ?? ""
]
不起作用。错误= libc ++ abi.dylib:以类型为NSException的未捕获异常终止
not working. error = libc++abi.dylib: terminating with uncaught exception of type NSException
我正在使用swift4,alamofire 4,Xcode 9.2
I'm using swift4, alamofire 4, Xcode 9.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 CoreFoundation 0x000000010520f12b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001048a3f41 objc_exception_throw + 48
2 CoreFoundation 0x0000000105290024-[NSObject(NSO bject)doesNotRecognizeSelector:] + 132
3分配的CoreFoundation 0x0000000105191f78 ___转发_ 强> + 1432
4分配的CoreFoundation 0x0000000105191958 _CF_forwarding_prep_0 + 120
5分配VoiceStage 0x0000000103d09a7a T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU + 1354
6分配VoiceStage 0x0000000103d0f9a4 _T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU_TA + 84
7分配Alamofire 0x000000010415a994 _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_ + 212
8分配Alamofire 0x000000010415e2bc _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09mult ipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_TA + 156
9分配Alamofire 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_ $ dyb + 13) :以类型为NSException的未捕获异常终止
(lldb)
VoiceStage[33099:1632858] -[NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance 0xb0000000000000132018-03-26 16:20:16.686009+0300 VoiceStage[33099:1632858] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance 0xb000000000000013'*** First throw call stack:( 0 CoreFoundation 0x000000010520f12b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001048a3f41 objc_exception_throw + 48 2 CoreFoundation 0x0000000105290024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x0000000105191f78 ___forwarding_ + 1432 4 CoreFoundation 0x0000000105191958 _CF_forwarding_prep_0 + 120 5 VoiceStage 0x0000000103d09a7a T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU + 1354 6 VoiceStage 0x0000000103d0f9a4 _T010VoiceStage27ProfileDetailViewControllerC18profilePhotoUploadyyFy9Alamofire17MultipartFormDataCcfU_TA + 84 7 Alamofire 0x000000010415a994 _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_ + 212 8 Alamofire 0x000000010415e2bc _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_TA + 156 9 Alamofire 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: terminating with uncaught exception of type NSException(lldb)
推荐答案
-[NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance
错误消息是:我试图调用方法 dataUsingEncoding:
(Objective-C版本1),即在 NSNumber data(using:)
中/ code>对象。我不能,我坠毁了。
因此,这意味着您有时会认为您正在使用 String
对象,而实际上它是 Number
一个(或从 Int
转换为 Number
)。
The error message is saying: I tried to call a method dataUsingEncoding:
(Objective-C version one), that is in Swift data(using:)
on a NSNumber
object. I can't, I crashed.So it means that at some point you think that you are using a String
object when in fact it's a Number
one (or a conversion to Number
from an Int
).
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
您假定值
响应数据(使用: )
。
实际上,应该更多(值作为String)
。
处理更多情况将是测试值
的类。如果是String对象,请使用 data(using:)
,如果已经是 Data
对象,只需将值
。
You assume that value
responds to data(using:)
.Well in fact, it should be more (value as String)
.Managing more case would be to test the class of value
. If it's a String object, use data(using:)
, if it's already a Data
object simply put 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
。
So after all this explanation about the error:userID
is not a String or acToken
is not a String but an Int
. According to the names, I tend to say that userID
is an Int
.
这篇关于libc ++ abi.dylib:以NSException Alamofire类型的未捕获异常终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!