2016-03-21 39 views
1

我正在製作一個iOS應用程序,我試圖用Osmosis從網站獲取一些數據並將其顯示在我的應用程序中。爲此,我按照github上的自述文件中描述的usage使用陣列與滲透

我的問題是,我試圖獲取解析的信息時出現錯誤。下面是我的代碼片段:

class ViewController: UIViewController { 

    var array: [String] = [] 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     Osmosis(errorHandler: { (error) -> Void in 
      print(error) 
     }) 
      .get(NSURL(string: "www.mysite.com")!) 
      .find(OsmosisSelector(selector: ".quote"), type: .CSS) 
      .populate([ 
        OsmosisPopulateKey.Single("quotes") : OsmosisSelector(selector: "tt_quotes b") 
       ], type: .CSS) 
      .list { (dict) -> Void in 
       self.array.append(dict) 
      .start() 
     print(dict) 

當我嘗試建立我的項目,我得到以下錯誤:

ViewController.swift:34:35: Cannot convert value of type '[String : AnyObject]' to expected argument type 'String'

爲什麼我的項目崩盤和我應該如何聲明我的數組,以使其正常工作?

回答

2

你宣佈你的數組來保存String對象在這裏:如果你想讓它保持詞典,改變申報

var array: [String] = [] 

var array: [[String : AnyObject]] = [] 

或使用[AnyObject]持有任何類型的對象(字符串,字典,不管)。

關於第二個問題,你不要關閉您的關閉:

.list { (dict) -> Void in 
    self.array.append(dict) 
}.start() 
+0

謝謝您的回答,但是當我這樣做,我得到以下錯誤: 'ViewController.swift:34:28 :元組類型'()'的值沒有成員'start'' – Mehdi

+0

@Mehdi編輯我的答案以解決第二個問題。 – JAL

+0

愚蠢的錯誤..謝謝! – Mehdi