SamirP
SamirP

Reputation: 21

How to check variable value in swift 3?

I would like to check the value of the outgoing variable whether it is 0 or 1. I tried below code to do the same but I am not able to check it. In below code message is a dictionary which contains values of XMPPMessageArchiving_Message_CoreDataObject.

How to check the value of outgoing variable?

message dictionary contains:

bareJid = "(...not nil..)";
bareJidStr = "[email protected]";
body = "{\"Date\":\"14 Jun 2017\",\"Time\":\"4:21PM\",\"body\":\"hello\",\"filePath\":\"\",\"isImportant\":\"N\",\"isMine\":true,\"msgid\":\"167-36\",\"receiver\":\"tester1\",\"sender\":\"tester3\",\"senderName\":\"tester3\"}";
composing = 0;
message = "(...not nil..)";
messageStr = "<message xmlns=\"jabber:client\" to=\"[email protected]\" id=\"167-36\" type=\"chat\" from=\"[email protected]/Smack\"><body>{\"Date\":\"14 Jun 2017\",\"Time\":\"4:21PM\",\"body\":\"hello\",\"filePath\":\"\",\"isImportant\":\"N";
outgoing = 0;
streamBareJidStr = "[email protected]";
thread = "2066797c-4f79-48f3-bd04-30658ee35e9f";
timestamp = "2017-06-14 11:20:41 +0000";

When I debug the value of outgoing variable then Xcode shows the type of it is Any? and value of it is some.

let outgoing = messasge.value(forKey: "outgoing")
var isIncoming = true

if let og = outgoing as? Int {
    if og == 1 {
        isIncoming = false
    }
}

Upvotes: 0

Views: 1292

Answers (2)

Subramanian P
Subramanian P

Reputation: 4375

If your dictionary value data type is Anyobject then unwrap as NSNumber and convert to integer or bool

if let outgoing = message["outgoing"] {
       let convertedValue = Int(outgoing as! NSNumber)
        if convertedValue == 1 {
            isIncoming = false
            print("Incoming false")
        }
    }

Example :

 var messasge = [String: AnyObject]()
        messasge["outgoing"] = "1" as AnyObject;

        if let outgoing = messasge["outgoing"] {
           let convertedValue = Int(outgoing as! NSNumber)
            if convertedValue == 1 {
                print("Incoming false")
            }
        }

message["outgoing"] value data type is String :

 var messasge = [String: Any]()

  messasge["outgoing"] = "1" ;

 if let outgoing = messasge["outgoing"] as? String, outgoing == "1" {
     isIncoming = false
  }

message["outgoing"] value data type is Int :

 var messasge = [String: Any]()

 messasge["outgoing"] = 1 ;

 if let outgoing = messasge["outgoing"] as? Int, outgoing == 1 {
     isIncoming = false
  }

Upvotes: 2

Lalit kumar
Lalit kumar

Reputation: 2207

let outgoing : String = messasge.value(forKey: "outgoing")
let og = Int(outgoing)
if og == 1
{// isIncoming = false
}
else
{
  //isIncoming = true
}

Upvotes: 0

Related Questions