有人可以帮助我吗?我只是将我的小项目从Objective-C重新编程为Swift,并得到以下错误:
不能用索引类型为“字符串”的下标“字典”类型的值
eins
,zwei
,drei
和vier
是从其他视图控制器获取数据的字符串。
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
答案 0 :(得分:0)
请不要尝试将Objective-C代码从字面上转换为Swift。从Swift的角度来看,有很多不良做法。
如果有本机副本,则基本上不使用Swift中的NS...
类。
发生错误是因为从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
支票是多余的,因此我忽略了。