每次编译器在从Json对象获取一些数据后尝试创建webview时,我的程序都会崩溃。它崩溃了这个错误:
EXC_BREAKPOINT(CODE = EXC_I386_BPT,SUBCODE = 0X0)
注意:任何其他对象(UIlabel
,UIButton
,UItextfield
等)都是成功创建的,程序不会崩溃,只有UIWEBView
导致程序崩溃。
以下是代码:
getdata(){ (boolValue) -> () in
self.loadimg()
self.reloadInputViews()
SwiftLoading().hideLoading()
}
}
func loadimg()
{
for L in 0..<Basketpic.count{
var webv = UIWebView()
let link = NSURL(string:Basketpic[L])
let requestObj = NSURLRequest(url: link! as URL) as URLRequest
webv.loadRequest(requestObj)
webv.contentMode = .scaleToFill
webv.frame = CGRect(x: 280 , y:100 + ( 120 * L ), width: 90, height: 100)
/*let webv = UIWebView()
let link = NSURL(string:Basketpic[L])
let requestObj = NSURLRequest(url: link! as URL) as URLRequest
webv.loadRequest(requestObj)
webv.contentMode = .scaleToFill
webv.frame = CGRect(x: 280 , y:100 + ( 120 * L ), width: 90, height: 100)*/
/*let name = UILabel()
name.text = (BasketNames[L])
name.contentMode = .scaleToFill
name.frame = CGRect(x: 140 , y: 100 + (120 * L), width: 150, height: 100)
let price = UILabel()
price.text = (String(BasketPrices[L]))
price.contentMode = .scaleToFill
price.frame = CGRect(x: 55 , y: 100 + (120 * L), width: 100, height: 100)*/
// view.addSubview(name)
// view.addSubview(price)
// view.addSubview(webv)
}
/* let sum = BasketPrices.reduce(0, +)
let total = UILabel()
total.text = (String(sum))
total.contentMode = .scaleToFill
let L = BasketPrices.count
total.frame = CGRect(x: 55 , y: 100 + (120 * L ), width: 100, height: 100)
let totall = UILabel()
totall.text = ("المجموع الكلي")
totall.contentMode = .scaleToFill
totall.frame = CGRect(x: 155 , y: 100 + (120 * L ), width: 100, height: 100)
view.addSubview(totall)
view.addSubview(total) */
}
func getdata(completion: @escaping (_ result: Bool)->())
{
let listUrlString = "http://burjuman.net/kkkls/basketreview.php"
let myUrl = URL(string: listUrlString);
var request = URLRequest(url:myUrl!)
request.httpMethod = "GET";
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
if let result = json["result"] as? [[String: AnyObject]] {
for blog in result {
if let basket = blog["basket"] as? String {
if let id = blog["id"] as? String {
if let userid = blog["User_Id"] as? String {
if(userid == "75")
{
Basketpic.append(basket)
print(userid)
print(id)
print(basket)
}
}
}
}
}
}
} catch {
print("error serializing JSON: \(error)")
}
print(Basketpic)
completion (true)
}
task.resume()
}
答案 0 :(得分:0)
要在请求后显示UI元素,您需要使用:
(在Swift3中)
DispatchQueue.main.async {
/* What you want to display, update label, show webview */
}