-1

When I run my app on a simulator or my phone the code compiles fine but when I go to archive it (to upload to the App Store) I get the "ambiguous use of subscript" error.

Int((results?.valueForKey("schoolYear")[0])! as! Int)

How can I fix this?

Schuey999
  • 4,706
  • 7
  • 21
  • 36
  • 2
    Please [search on the error message](http://stackoverflow.com/search?q=%5Bswift%5D+ambiguous+use+of+subscript) before posting a question. – rmaddy Apr 16 '16 at 21:49
  • none of the solutions have helped me – Schuey999 Apr 16 '16 at 21:50
  • Why do you initialize an `Int` from a type which is forced casted to `Int? You have to tell the compiler about the type the dictionary returns (seems to be array). – vadian Apr 16 '16 at 21:50
  • What solutions have you tried? Update your question with your attempts and their results. – rmaddy Apr 16 '16 at 21:50
  • ask the swift compiler...I confused about it too but if I take the as! Int away, the compiler cries about it – Schuey999 Apr 16 '16 at 21:50
  • 1
    You should never force unwrap just to shut the compiler up. Split the statement up into multiple lines, inspect what each one is returning, and deal with the optionals properly when you encounter them. You'll feel better for it, and the compiler will thank you too. – Hamish Apr 16 '16 at 22:02

1 Answers1

1

try

if let myExpectedArray = results?["schoolYear"] as? [Int] where !myExpectedArray.isEmpty {
   let myInt = myExpectedArray[0]
}

The compiler doesn't know which type the dictionary returns, that means ambiguous use of subscript

vadian
  • 274,689
  • 30
  • 353
  • 361