EXC_BAD_INSTRUCTION 检查 xml 元素在 swift 中是否为空时出错
EXC_BAD_INSTRUCTION error for checking if xml element is empty in swift
我在检查 xml 元素是否有 desire 标签时遇到问题:
我写了如下代码:
//checking if link is empty
if xml["rss"]["channel"]["item"][index]["source"].element!.attributes["url"] != "" {
Link = (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"])!
} else {
Link = ""
}
当我 运行 这段代码时,我的代码第一行出现错误:Thread3 EXC_BAD_INSTRUCTION
,但是当我 运行 时,代码如下:
如果不理解源元素根本不存在,它就会通过!
代码:
//checking if link is empty
if (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String?) != nil {
Link = (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String?)!
} else {
Link = ""
}
如何检查 swift 2 中是否存在源元素?
完整代码:
let xml = SWXMLHash.parse(data)
//one root element
let count = xml["rss"]["channel"]["item"].all.count
print(count)
var Name = ""
var Image = ""
var Link = ""
var Time = ""
for var index = 0; index < count; index++ {
Name = xml["rss"]["channel"]["item"][index]["title"].element!.text!
Image = (xml["rss"]["channel"]["item"][index]["description"]["img"].element?.attributes["src"])!
//checking if time is empty
if let check_time = xml["rss"]["channel"]["item"][index]["pubDate"].element?.text! as String? {
Time = check_time
}else{
Time = ""
}
//checking if link is empty
if let element = xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String? where element != "" {
print("salalalalam")
print(Link)
Link = element
} else {
Link = ""
}
在您的第一个片段中,元素似乎为 nil,因此您的代码崩溃了。
在你的第二个片段中(因为元素是零)它只是通过了低谷。
如何在 Swift 中使用条件可选绑定?
let i:[Int?] = [1,2,10, nil]
i.forEach { v in
if let x = v where x != 10 {
print("value passed the test:", x)
} else {
print("didn't pass the test:", v)
}
}
/*
value passed the test: 1
value passed the test: 2
didn't pass the test: Optional(10)
didn't pass the test: nil
*/
根据您的笔记进行更新
if let url = xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] {
Link = url
} else {
Link = ""
}
我在检查 xml 元素是否有 desire 标签时遇到问题:
我写了如下代码:
//checking if link is empty
if xml["rss"]["channel"]["item"][index]["source"].element!.attributes["url"] != "" {
Link = (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"])!
} else {
Link = ""
}
当我 运行 这段代码时,我的代码第一行出现错误:Thread3 EXC_BAD_INSTRUCTION
,但是当我 运行 时,代码如下:
如果不理解源元素根本不存在,它就会通过!
代码:
//checking if link is empty
if (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String?) != nil {
Link = (xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String?)!
} else {
Link = ""
}
如何检查 swift 2 中是否存在源元素?
完整代码:
let xml = SWXMLHash.parse(data)
//one root element
let count = xml["rss"]["channel"]["item"].all.count
print(count)
var Name = ""
var Image = ""
var Link = ""
var Time = ""
for var index = 0; index < count; index++ {
Name = xml["rss"]["channel"]["item"][index]["title"].element!.text!
Image = (xml["rss"]["channel"]["item"][index]["description"]["img"].element?.attributes["src"])!
//checking if time is empty
if let check_time = xml["rss"]["channel"]["item"][index]["pubDate"].element?.text! as String? {
Time = check_time
}else{
Time = ""
}
//checking if link is empty
if let element = xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] as String? where element != "" {
print("salalalalam")
print(Link)
Link = element
} else {
Link = ""
}
在您的第一个片段中,元素似乎为 nil,因此您的代码崩溃了。 在你的第二个片段中(因为元素是零)它只是通过了低谷。
如何在 Swift 中使用条件可选绑定?
let i:[Int?] = [1,2,10, nil]
i.forEach { v in
if let x = v where x != 10 {
print("value passed the test:", x)
} else {
print("didn't pass the test:", v)
}
}
/*
value passed the test: 1
value passed the test: 2
didn't pass the test: Optional(10)
didn't pass the test: nil
*/
根据您的笔记进行更新
if let url = xml["rss"]["channel"]["item"][index]["source"].element?.attributes["url"] {
Link = url
} else {
Link = ""
}