我想提取节标记中id的值。
节标记为:
[<stanza-id xmlns="urn:xmpp:sid:0" by="f.talebi@wepod.ir" id="1531235744929009"></stanza-id>]
这是从Xmpp服务器收到的消息的一部分。
我需要提取“ 1531235744929009”值。因此,我这样写:
var stanza = message.elements(forName: "stanza-id")
print(stanza)
var id = stanza.first?.attributes
if let attributes = stanza.first?.attributes {
let lastItem = attributes.last
if let stanzaID = lastItem?.stringValue {
print("stanzaID = \(stanzaID)")
}
}
此代码正常工作,但不是干净的代码。特别是在我写此行lastItem = attributes.last
的地方,因为如果顺序更改了,将无法使用。
答案 0 :(得分:0)
只需使用stanza.attributed(forName: String)