简体   繁体   English

错误无法下标“字典”类型的值 <NSObject, AnyObject> &#39;的索引类型为&#39;String&#39;

[英]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. einszweidreivier是从另一个视图控制器获取其数据的字符串。

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 发生错误的原因是,从NSDictionaryDictionary (未指定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.

相关问题 “无法下标‘字典’类型的值<nsobject, anyobject> ' 索引类型为 'String' 错误</nsobject,> - "Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String" error 不能下标'[NSObject:AnyObject]类型的值吗?索引类型为'String' - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' 无法用索引类型“字符串”下标“ [NSObject:AnyObject]”类型的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String' 无法下标&#39;[NSObject:AnyObject]类型的值?” 索引类型为&#39;String&#39;的Swift错误 - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' Swift Error 无法用索引为“ NSKeyValueChangeKey”的下标“ [NSObject:AnyObject]”的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'NSKeyValueChangeKey' 无法下标[AnyObject]的值? 索引类型为String - Cannot subscript a value of [AnyObject]? with an index of type String 不能下标类型为[String:AnyObject]的值?索引类型为String - Cannot subscript a value of type [String: AnyObject]? with an index of type String 迅速:无法下标[字典式]的值 <String, AnyObject> ],其索引类型为字符串 - swift: cannot subscript a value of type [Dictionary<String, AnyObject>] with an index of type string 无法使用索引类型为“String”的类型&#39;[String:AnyObject]&#39;下标值 - Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String' 无法用索引类型“ String”下标“ [String:AnyObject]”类型的值 - Cannot subscript a value of type '[String: AnyObject]' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM