[英]Error Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String'
Can someone help me? 有人能帮我吗? I am just reprogramming my small project from Objective-C to Swift and get the following error:
我只是将我的小项目从Objective-C重新编程为Swift,并得到以下错误:
Cannot subscript a value of type 'Dictionary' with an index of type 'String'
无法用索引类型“字符串”下标“字典”类型的值
eins
, zwei
, drei
,and vier
are strings that get their data from another view controller. eins
, zwei
, drei
和vier
是从另一个视图控制器获取其数据的字符串。
Objective-C Code: Objective-C代码:
@property (strong, nonatomic) IBOutlet UISwitch *bluetoothSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *SwitchState;
NSArray *myStrings = [[NSArray alloc] initWithObjects:eins,zwei,drei, vier, nil];
SwitchState = [myStrings componentsJoinedByString:mad:" | | "];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:mad:"store.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:mad:"store.plist"] ];
}
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if ([[savedValue objectForKey:SwitchState] boolValue]) {
self.bluetoothSwitch.on = [[savedValue objectForKey:SwitchState] boolValue];
} else {
}
}
My Swift Code: 我的Swift代码:
var eins = "Test"
var zwei = "Text"
var drei = "Test"
var vier = "Text"
var SwitchState = ""
let myStrings = [eins, zwei, drei, vier]
SwitchState = myStrings.joined(separator: " | | ")
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
var path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("store.plist").absoluteString
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: path) {
path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("store.plist").absoluteString
}
var savedValue = NSDictionary(contentsOfFile: path) as Dictionary?
print("Das bekommt viewdid: \(SwitchState)")
if (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false {
bluetoothSwitch.isOn = (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false
} else {
print("nein")
}
This two Lines "produce" the Errors 这两行“产生”错误
if (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false {
bluetoothSwitch.isOn = (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false
Please don't try to translate Objective-C code literally to Swift. 请不要尝试将Objective-C代码从字面上转换为Swift。 There are many, many bad practices from the Swift perspective.
从Swift的角度来看,有许多不良做法。
Basically don't use NS...
classes in Swift if there is a native counterpart. 如果有本机版本,基本上不要在Swift中使用
NS...
类。
The error occurs because the result of a bridge cast from NSDictionary
to Dictionary
(without specifying the Swift types) is Dictionary<NSObject, AnyObject>
which is not compatible with value type String
发生错误的原因是,从
NSDictionary
为Dictionary
(未指定Swift类型)的桥的结果是Dictionary<NSObject, AnyObject>
,它与值类型String
不兼容
This is a real Swift version: 这是一个真正的Swift版本:
let eins = "Test"
let zwei = "Text"
let drei = "Test"
let vier = "Text"
let myStrings = [eins, zwei, drei, vier]
let switchState = myStrings.joined(separator: " | | ")
let fileManager = FileManager.default
do {
let applicationSupportDirectory = try fileManager.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let storeURL = applicationSupportDirectory.appendingPathComponent("store.plist")
let data = try Data(contentsOf: storeURL)
print("Das bekommt viewdid: \(switchState)")
if let savedDictionary = try PropertyListSerialization.propertyList(from: data, format: nil) as? [String:Any],
let savedState = savedDictionary[switchState] as? Bool {
bluetoothSwitch.isOn = savedState
} else {
bluetoothSwitch.isOn = false
print("nein")
}
} catch { print(error) }
The fileExists(atPath
check is redundant so I left it out. fileExists(atPath
检查是多余的,所以我省略了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.