我在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];
一个单独的方法,如:
-(void)dictionaryToData:(NSDictionary*)dic {
return [NSKeyedArchiver archivedDataWithRootObject:productDic];
}
并使用我的链接答案解决方法:
func mySwiftFunc(dic: Dictionary) {
myObjcClassInstance.perform(#selector(NSSelectorFromString("dictionaryToData:"), with: dic as NSDictionary)
}
你可以确保你处理“真正的”NSDictionary
并避免隐含的__SwiftValue
桥接。