-1

I have a project in which this code does not give me problems, but in Xcode 7.0 beta 6 it skips the warning and I can't find a way to fix it

 func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    print("Mensaje recibido:\(message)")

    if let msg = message as? String{ //Error here
        // do something with the uname
    }

    replyHandler(["reply" : "OK"])
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Alesao
  • 3
  • 4
  • 2
    What warning is it skipping? – Fogmeister Sep 04 '15 at 15:27
  • 2
    Why are you trying to cast the message dictionary [String : AnyObject] into a String? Are you trying to get a value out of the dictionary and cast the AnyObject to a String? – Mr Beardsley Sep 04 '15 at 15:29
  • The warning message says inmmutable value if let msg = message["Status"] as? String{ //Error msg replace for _ } – Alesao Sep 04 '15 at 15:37

1 Answers1

3

As Mr Beardsley said, the instruction if let msg = message as? String won't work because you're trying to cast message, which is a dictionary, to a String optional. This should do the job:

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print("Mensaje recibido:\(message)")

if let msg = message["/* Whatever key you want to select */"] as? String {
    // do something with the uname
}

replyHandler(["reply" : "OK"])

}

Replace the 'Whatever key you want to select' part with the key paired to the value you want to assign to msg.

MustangXY
  • 358
  • 2
  • 16
  • Sorry, the code of the question I forgot to put the key, but in my code if I really start yet still fails if let msg = message["Status"] as? String{ //Error msg replace for _ } – Alesao Sep 07 '15 at 08:06
  • If the error you're receiving tells you to replace `msg` with `_` that's almost certainly because you're not using the `msg` constant inside the if-let statement braces. Try writing `println(msg)` inside those braces as a test, and see if the error disappears. – MustangXY Sep 07 '15 at 08:26