一个非常简单的问题。我无法从Swift的for循环中删除特定项目。我正在用来自Firebase的URL在scrollview(每个webview是一张不同的幻灯片)中显示不同的webview。这段代码很好用,但是如果网址不可用,我希望能够删除该网站(幻灯片),因此它不仅是空白框架。如果有更简单,更简单的方法,请告诉我。
func loadWebsites() -> [Website] {
let url1 = URL(string: "\(nikeWebsite)")
let request1 = URLRequest(url: url1!)
let url2 = URL(string: "\(finishlineWebsite)")
let request2 = URLRequest(url: url2!)
let url3 = URL(string: "\(eastbayWebsite)")
let request3 = URLRequest(url: url3!)
let url4 = URL(string: "\(footlockerWebsite)")
let request4 = URLRequest(url: url4!)
let url5 = URL(string: "\(footactionWebsite)")
let request5 = URLRequest(url: url5!)
let url6 = URL(string: "\(champsWebsite)")
let request6 = URLRequest(url: url6!)
let website1:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website1.webView.load(request1)
website1.webView.navigationDelegate = self
let website2:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website2.webView.load(request2)
website2.webView.navigationDelegate = self
let website3:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website3.webView.load(request3)
website3.webView.navigationDelegate = self
let website4:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website4.webView.load(request4)
website4.webView.navigationDelegate = self
let website5:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website5.webView.load(request5)
website5.webView.navigationDelegate = self
let website6:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website6.webView.load(request6)
website6.webView.navigationDelegate = self
return [website1, website2, website3, website4, website5, website6]
}
func setupWebsiteScrollView(websites : [Website]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(websites.count), height: view.frame.height)
scrollView.isPagingEnabled = false
scrollView.isScrollEnabled = false
scrollView.contentInsetAdjustmentBehavior = .never
for i in 0 ..< websites.count {
websites[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(websites[i])
//It would be an if statement that initially trigger the code I'm looking for
if nikeWebsite == nil {
websites.remove(at: 0)
}
}
获取此错误消息。
不能对不可变值使用变异成员:“网站”是一个“ let”常量
答案 0 :(得分:0)
在几个级别上,您的代码没有任何意义。
第一个问题:
此代码:
func foo() {
var array = ["a", "b", "c", "d", "e", "f"]
for index in 0..<array.count {
let item = array[index]
print("Item \(item)")
array.remove(at: index)
}
}
会崩溃。
for循环的范围是在开始执行for循环之前生成的。由于数组中有6个项目,因此for循环将迭代从0到5的范围。
在对数组进行索引时,无法从数组中删除项目。您必须向后遍历。
下一个问题:您将数组作为函数参数传入。数组按值传递,因此参数为let常数。您无法修改数组。
您可以通过向后遍历数组并将数组设为inout
参数来解决这两个问题,但是为什么呢?
func foo(_ array: inout [String]) {
for index in (0..<array.count).reversed() {
let item = array[index]
print("Item \(item)")
array.remove(at: index)
}
}
为什么需要在处理数组时从数组中删除每个项目?