简体   繁体   English

如何在Swift 3中检查变量值?

[英]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. 我想检查传出变量的值是0还是1。我尝试下面的代码执行相同的操作,但无法对其进行检查。 In below code message is a dictionary which contains values of XMPPMessageArchiving_Message_CoreDataObject . 在下面的代码中, message是一个字典,其中包含XMPPMessageArchiving_Message_CoreDataObject值。

How to check the value of outgoing variable? 如何检查outgoing变量的值?

message dictionary contains: message字典包含:

bareJid = "(...not nil..)";
bareJidStr = "tester3@90.0.0.163";
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=\"tester1@90.0.0.163\" id=\"167-36\" type=\"chat\" from=\"tester3@90.0.0.163/Smack\"><body>{\"Date\":\"14 Jun 2017\",\"Time\":\"4:21PM\",\"body\":\"hello\",\"filePath\":\"\",\"isImportant\":\"N";
outgoing = 0;
streamBareJidStr = "tester1@90.0.0.163";
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? 当我调试outgoing变量的值时,Xcode显示它的类型为Any? and value of it is some . 它的价值是some

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

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

If your dictionary value data type is Anyobject then unwrap as NSNumber and convert to integer or bool 如果您的字典值数据类型为Anyobject,则将其解包装为NSNumber并转换为整数或布尔值

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 : message [“ outgoing”]值数据类型为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 : message [“ outgoing”]值数据类型为Int

 var messasge = [String: Any]()

 messasge["outgoing"] = 1 ;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM