objective-c - 尝试将 NSDictionary 从 swift 类传递到目标 c 类时发送到实例的无法识别的选择器
问题描述
我有一个 swift 类,它使用键中的字符串和值中的对象来制作字典。例如let dict = ["key":value.key]
。该值始终是一个字符串。
当我在我的目标 c 类中获取数据时,对象类型是
_TtGCs26_SwiftDeferredNSDictionaryVs11AnyHashableP__$
并且代码期待nsdictionary。
现在当代码 -
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionaryFromSwiftClass];
被执行,程序抛出错误
********[1235:641121] -[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x280571380
//making dict in swift class
let dictForAddToCart:NSDictionary =
[
"assoc_prod":[
["assoc_prodID":"92",
"assoc_prodValue":currentSelectedColor,
"productInfo":[
"code":productInfoColor.value(forKey: "code"),
"id":productInfoColor.value(forKey: "id"),
"image":productInfoColor.value(forKey: "image"),
"label":productInfoColor.value(forKey: "label"),
"optionslabel":productInfoColor.value(forKey: "optionslabel"),
"price":productInfoColor.value(forKey: "price"),
"qty":productInfoColor.value(forKey: "qty")]],
["assoc_prodID":"92",
"assoc_prodValue":currentSelectedColor,
"productInfo":[
"code":productInfoColor.value(forKey: "code"),
"id":productInfoColor.value(forKey: "id"),
"image":productInfoColor.value(forKey: "image"),
"label":productInfoColor.value(forKey: "label"),
"optionslabel":productInfoColor.value(forKey: "optionslabel"),
"price":productInfoColor.value(forKey: "price"),
"qty":productInfoColor.value(forKey: "qty")]]
]
"productData":productData,
"qty":currentSelectedQuantity
] as NSDictionary
//saving object in objective c class
- (void)saveCartArray:(NSArray *)arrayToSave {
NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:arrayToSave.count];
for (NSMutableDictionary* productDic in arrayToSave) {
NSData *productDicData = [NSKeyedArchiver archivedDataWithRootObject:productDic];
[archiveArray addObject:productDicData];
}
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:archiveArray forKey:@"cart"];
}
//NSLOG of arrayToSave-
{
"assoc_prod" = (
{
"assoc_prodID" = 92;
"assoc_prodValue" = 396;
code = "********.Code.color";
id = 92;
image = "********.Image.empty";
label = "********.Label.color";
optionslabel = Nude;
price = "280.0000";
qty = "0.0000";
},
{
"assoc_prodID" = 180;
"assoc_prodValue" = 388;
code = "********.Code.size";
id = 180;
image = "********.Image.empty";
label = "********.Label.size";
optionslabel = 36;
price = "280.0000";
qty = "0.0000";
}
);
productData = {
additionalParameters = (
);
associatedProd = "someData";
brand = "";
categoryId = 378;
description = "";
image = "http://www.x.com/xyz.jpg";
link = "";
linkDownloads = "";
name = some;
position = 0;
price = 280;
productId = 1421;
qty = 0;
set = 0;
shortDescription = "";
sku = some;
type = some;
wsp = "";
};
qty = 1;
}
)
解决方案
我有预感这是我在这里回答的类似问题: https ://stackoverflow.com/a/53501401/5329717
由于以下原因,您的情况会略有不同:
- (void)saveCartArray:(NSArray *)arrayToSave {
NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:arrayToSave.count];
for (NSMutableDictionary* productDic in arrayToSave) {
NSData *productDicData = [NSKeyedArchiver archivedDataWithRootObject:productDic];
[archiveArray addObject:productDicData];
}
所以本质上,如果我理解正确,Swift 桥接SwiftDeferredNSDictionary
的元素是NSArray*
. 如果你能提取
NSData *productDicData = [NSKeyedArchiver archivedDataWithRootObject:productDic];
到一个单独的方法,例如:
-(NSData*)dictionaryToData:(NSDictionary*)dic {
return [NSKeyedArchiver archivedDataWithRootObject:productDic];
}
并使用我的链接答案解决方法:
func mySwiftFunc(dic: Dictionary) {
myObjcClassInstance.perform(#selector(NSSelectorFromString("dictionaryToData:"), with: dic as NSDictionary)
}
您可以确保处理“真实” NSDictionary
并避免隐式__SwiftValue
桥接。
推荐阅读
- python - 配置django app接收Apple App Store通知(refundNotification)并测试
- java - Thymeleaf 元素模型处理
- python - 如何将列表中的值分配给数据框中的一组值?
- react-redux - 如何使用 react hooks、redux 将图像上传到数据库?
- postgresql - 如何在 Postgresql 中存储列表?
- laravel - 如何为 Laravel 中的每种异常设置不同的日志级别?
- delphi - 使用 GDI+ 的线程 JPEG 解码随机触发“没有足够的内存资源可用于处理此命令”
- excel - 从三个工作表上的三个列表中提取唯一值
- wordpress - 拒绝在框架中显示“https://www.google.com/”,因为它在 Wordpress 中将“X-Frame-Options”设置为“sameorigin”
- swift - 从另一个类访问变量给 0 swift