2017-04-22 24 views
-5
if let name = property["Name"] as? String, 
     let latitude = property["Latitude"] as? NSNumber, 
     let longitude = property["Longitude"] as? NSNumber, 
     let image = property["Image"] as? String { 

     // To learn how to read data from plist file, check out our Saving Data 
     // in iOS Video Tutorial series. 

     // Code goes here 


    } 

私のコードを最新のswiftバージョンに変換したとき、「タイプには添え字エラーがありません」と直面しています。私はこの時点で迅速に立ち往生することはかなり新しいです。他の関連記事を見ましたが、ここでは解決策を見つけることができませんでした。助けてください。タイプanyはswiftに添え字エラーがありません3

+0

[編集] 'property'が宣言され、初期化されているかを示すあなたのコード。 – rmaddy

+0

[タイプ 'Any'の可能な重複はSwift 3にアップデートした後に添え字メンバーを持たない](http://stackoverflow.com/questions/39480150/type-any-has-no-subscript-members-after-updating-to- swift-3) –

+1

確かにあなたの答えは[これらの検索結果](http://stackoverflow.com/search?q=%5Bswift%5D+Type+any+has+no+subscript)で見つけることができます。 – rmaddy

答えて

1

多くの場合、questions with duplicate titlesを流しましたが、あなたのケースをはっきりとカバーしているものが見つかりませんでした。何が起こっているのかを説明します。

変数propertyは、タイプを保持できる変数であるタイプAnyとして明確に宣言されています。エラーメッセージは、サブスクリプト[]を使用できないことを示しています。 Stringの値がであるのキーがDictionaryと考えられることは明らかです。 Swift 3では、何かが入力されました。Anyです。したがって、property[String : Any]となる予定です。 property[String : Any]にキャストすると、[]を使用できるようになります。

あなたが最初にすべきことは、[String : Any]propertyをキャストして、それが動作するかどう進めるための試みである:

if let property = property as? [String : Any], 
    let name = property["Name"] as? String, 
    let latitude = property["Latitude"] as? NSNumber, 
    let longitude = property["Longitude"] as? NSNumber, 
    let image = property["Image"] as? String { 

    // To learn how to read data from plist file, check out our Saving Data 
    // in iOS Video Tutorial series. 

    // Code goes here 
} 
関連する問題