0

I am migrating from swift 1.2 to 2.0 following code was working well in swift 1.2 but in 2.0 doesn't

    let data = dataContainer["d"] as NSArray!
    for item in data {
        var username: String = item["username"] as! String
    }

I am getting this compiler error:

Cannot convert value of type 'String' to specified type 'String'

How can I fix it?

Okan
  • 1,379
  • 3
  • 25
  • 38
  • Are you using the latest, released Xcode? The Swift team hopes they have eliminated all of the "Cannot convert 'x' to 'x'" errors. If you're getting one, you should open a radar. https://bugreport.apple.com – Rob Napier Sep 23 '15 at 21:27
  • 7.0 final release, not beta, right? If so, please do open a radar. You should never see this kind of error message. It's masking your actual syntax error. – Rob Napier Sep 23 '15 at 21:28
  • Also, you should indicate exactly where you get the error, and your actual Swift 2.0 code (the `as NSArray!` doesn't look like correct Swift 2). – Rob Napier Sep 23 '15 at 21:29
  • 1
    Just to be sure, you downloaded your Xcode from the Mac App Store right? :) – Luca Angeletti Sep 23 '15 at 21:30
  • 1
    @appzYourLife hahahha yes i am sure :) – Okan Sep 23 '15 at 21:30
  • @RobNapier My code looks like this: http://prntscr.com/8jqpux – Okan Sep 23 '15 at 21:31
  • @Okan: ok, I asked because of this: http://www.macrumors.com/2015/09/20/xcodeghost-chinese-malware-faq/ – Luca Angeletti Sep 23 '15 at 21:31
  • @appzYourLife Yeah, I know it – Okan Sep 23 '15 at 21:32
  • @Okan: how is `dataContainer` defined? – Luca Angeletti Sep 23 '15 at 21:33
  • @appzYourLife http://prntscr.com/8jqr8u – Okan Sep 23 '15 at 21:34
  • I can't reproduce this in Xcode 7 using your posted code. Can you produce a MCVE? http://stackoverflow.com/help/mcve – Rob Napier Sep 23 '15 at 21:38
  • I guess, i should never move to swift 2. Becase I am getting tons of errors and I can't handle it. Thanks for your help. – Okan Sep 23 '15 at 21:40
  • 1
    What is the type of data container – Luke De Feo Sep 23 '15 at 22:30
  • What happens if you just say `var username = item["username"] as! String` ? – Paulw11 Sep 25 '15 at 03:04
  • @Paulw11 http://prntscr.com/8k8gi9 – Okan Sep 25 '15 at 08:11
  • http://stackoverflow.com/questions/29874414/cannot-subscript-a-value-of-anyobject-with-an-index-of-type-int - what is `results`? That is a different variable to what you had before – Paulw11 Sep 25 '15 at 09:02
  • Also, you generally don't need to type variables with Swift if it can work it out from context - can you try `let dataContainer = notification.userInfo as! Dictionary let data = dataContainer["d"] as NSArray! for item in data { let username = item["username"] as! String print(username) } ` – Paulw11 Sep 25 '15 at 09:11
  • or even `func processComments(notification:NSNotification) { let dataContainer = notification.userInfo as! Dictionary if let data = dataContainer["d"] { for item in data { let username = item["username"] as! String print(username) } } }` - it depends are you putting optional arrays into the NSNotification dictionary? – Paulw11 Sep 25 '15 at 09:12
  • I tried your second code: http://prntscr.com/8kbal7 – Okan Sep 25 '15 at 13:38

0 Answers0