我想检查传出变量的值是否为0或1.我尝试下面的代码做同样的但我无法检查它。在下面的代码中,import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Hero } from '../shared/hero.model';
import { HeroService } from '../shared/hero.service';
@Component({
selector: 'app-hero',
templateUrl: './hero.component.html',
styleUrls: ['./hero.component.scss'],
providers: [HeroService]
})
export class HeroComponent implements OnInit {
hero: Hero;
constructor(private route: ActivatedRoute, private heroService: HeroService) { }
ngOnInit() {
let id = this.route.snapshot.params['id'];
this.heroService.getHero(id).subscribe(hero => {
this.hero = hero;
});
}
}
是一个包含message
的值的字典。
如何检查XMPPMessageArchiving_Message_CoreDataObject
变量的值?
outgoing
字典包含:
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";
变量的值时,Xcode会显示其类型为outgoing
,其值为Any?
。
some
答案 0 :(得分:2)
如果您的字典值数据类型是Anyobject,则打开为NSNumber并转换为整数或bool
if let outgoing = message["outgoing"] {
let convertedValue = Int(outgoing as! NSNumber)
if convertedValue == 1 {
isIncoming = false
print("Incoming false")
}
}
示例:
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")
}
}
消息[“传出”]值数据类型为String
:
var messasge = [String: Any]()
messasge["outgoing"] = "1" ;
if let outgoing = messasge["outgoing"] as? String, outgoing == "1" {
isIncoming = false
}
消息[“传出”]值数据类型为Int
:
var messasge = [String: Any]()
messasge["outgoing"] = 1 ;
if let outgoing = messasge["outgoing"] as? Int, outgoing == 1 {
isIncoming = false
}
答案 1 :(得分:0)
let outgoing : String = messasge.value(forKey: "outgoing")
let og = Int(outgoing)
if og == 1
{// isIncoming = false
}
else
{
//isIncoming = true
}