我正在尝试在 iPhone 应用程序中使用网站上的文本。为此,我使用网络scraper,Osmosis 。我设法检索了我想要的数据,但我无法将数据影响到数组,以便在函数之外使用它。这是我所做的一个片段:
<语言:lang-swift>
class ViewController: UIViewController {
@IBOutlet weak var quote: UILabel!
private var array: [[String: AnyObject]] = []
var initArray: [[String: AnyObject]] {
get {
return array
}
set {
for e in self.initArray {
array.append(e)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getQuotes()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getQuotes()
Osmosis(errorHandler: { (error) -> Void in
print(error)
})
.get(NSURL(string: "http://www.example.com")!)
.find(OsmosisSelector(selector: ".parentDiv"), type: .CSS)
.populate([
OsmosisPopulateKey.Single("quotes") : OsmosisSelector(selector: ".childDiv")
], type: .CSS)
.list { (dict) -> Void in
self.array.append(dict)
print(self.initArray) // this prints data
}
.start()
print(self.initArray) // This prints an empty array
}
}
我认为我无法使用该数组,因为它填充在 Osmosis()
的 init 内部,但我想知道如何在代码中的其他地方使用它。您能否解释一下我的代码中发生了什么(为什么我只能在 .list()
中使用数据)以及如何解决该问题。
编辑: 我尝试使用 getter 和 setter 来检索范围之外的数据,但结果与没有 getter/setter 相同。
最佳答案
您想将一个数组的所有元素添加到另一个数组吗?
for e in quotesArray {
self.array.append(e)
}
或
self.array.appendContentsOf(quotesArray)
关于ios - 使用范围外的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151535/