如标题所述,如何将[AnyHashable: Any]转换为nsmutabledictionary?我需要做一些Obj-c到Swift的关系。
非常直接
var d = [AnyHashable: Any]()
d["name"] = "Jay"
d[0] = "Jay"
print(d)
//[AnyHashable(0): "Jay", AnyHashable("name"): "Jay"]
let nsD = NSDictionary(dictionary: d)
print(nsD)
//{
// 0 = Jay;
// name = Jay;
//}
let nsMD = NSMutableDictionary(dictionary: d)
print(nsMD)
//{
// 0 = Jay;
// name = Jay;
//}
桥接的例子:
你的swift类可能看起来像这样
@objc class Swft: NSObject {
@objc var d = [AnyHashable: Any]()
override init() {
super.init()
d["name"] = "jay"
}
@objc func method() ->[AnyHashable: Any] {
return d
}
}
你的Objective-C类可能看起来像这样,.m
#import "Objc.h"
#import "{Your_project_name}-Swift.h"
@interface Objc ()
@end
@implementation Objc
- (void)someMethod {
Swft *s = [[Swft alloc] init];
NSLog(@"1234567890 %@", s.d);
// 1234567890 {
// name = jay;
// }
NSLog(@"0987654321 %@", [s method]);
// 0987654321 {
// name = jay;
// }
}
@end