有人可以帮助我吗?我刚刚将我的小项目从 Objective-C 重新编程为 Swift,并收到以下错误:
Cannot subscript a value of type 'Dictionary' with an index of type 'String'
eins
、zwei
、drei
和 vier
是从另一个 View Controller 获取数据的字符串。
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 {
}
}
我的 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")
}
这两行“产生”错误
if (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false {
bluetoothSwitch.isOn = (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false
最佳答案
请不要尝试将 Objective-C 代码逐字翻译为 Swift。从 Swift 的角度来看,有很多很多不好的做法。
基本上不要使用NS...
Swift 中的类(如果有 native 对应项)。
发生错误的原因是从 NSDictionary
进行桥接的结果至 Dictionary
(不指定 Swift 类型)是 Dictionary<NSObject, AnyObject>
与值类型 String
不兼容
这是一个真正的 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) }
fileExists(atPath
检查是多余的,所以我把它省略了。
关于ios - 错误 无法使用 'Dictionary<NSObject, AnyObject>' 类型的索引为 'String' 类型的值添加下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57977696/