2015-10-10 71 views
0

我有一本字典,我需要將其轉換爲數組,但無法完成實現。在InterfaceController從值字典獲取數組

字典...在Event類值的

var receivedData = Array<Dictionary<String, String>>() 



func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { 

    if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String { 
     receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue]) 

     let eventsList = Event.eventsListFromValues(receivedData) 

     for eventM in eventsList { 
      NSLog("Event Match: %@", eventM.eventMatch) 
     } 

    } else { 
     print("tColorValue and matchValue are not same as dictionary value") 
    } 

} 

過程詞典:

class func eventsListFromValues(values: Array<Dictionary<String, String>>) -> Array<Event> { 
     var array = Array<Event>() 

     for eventValues in values { 
      let event = Event(dataDictionary: eventValues) 
      array.append(event) 
     } 

     return array 
    } 

} 

無法弄清楚這部分...使用陣列設置在setupTable表:

func doTable() { 

      // ...get array of `match`s for use in table set up 

      // ...Then set number of Rows 
      // ...Then iterate thru the array 
        for var i = 0; i < self.rowTable.numberOfRows; i++ { 
         var row = self.rowTable.rowControllerAtIndex(i) 
         // ...setup text label 
        } 
    } 

編輯:澄清

doTable將獲得收到的任何match s並將其顯示在表中。所以我認爲我應該做的是獲得一個match s的數組,然後使用它們來設置表中的文本標籤。

編輯2:這是我迄今爲止

class InterfaceController: WKInterfaceController, WCSessionDelegate { 

    class EventSO { 
     var teamColor:String! 
     var matchup:String! 

     init(dataDictionary:[String:String]) { 
      teamColor = dataDictionary["teamColor"] 
      matchup = dataDictionary["Matchup"] 
     } 
    } 

    var receivedDataSO = [Event]() 

    var tColorValueSO = "Red" 
    var matchValueSO = "someString" 

    var eventSO = EventSO(dataDictionary: ["teamColor": tColorValueSO, "Matchup": matchValueSO]) 

故事板:

enter image description here

+0

你想要什麼的'doTable'方法呢? – Adam

+0

我不是100%確定你在這裏做什麼,receivedData是一個字典數組了嗎?你能澄清一下嗎?你能告訴我們你想在doTable中做什麼以及事件是什麼嗎? – Scriptable

+0

@亞當嗨亞當,我添加了上面的編輯嘗試進一步澄清,但基本上'doTable'將設置我的WatchKit表。因此,在'doTable'裏面,我需要獲得一串'match',我可以遍歷它來構建表格並設置標籤的文本。我希望這更有意義!讓我知道如果不是 - – SRMR

回答

1

下面的代碼做了一些假設和習慣有直接關係,因爲我又不是對WatchKit和其他代碼非常熟悉。

我不認爲你真的需要eventListFromValues方法,我是在XCode的一個操場上完成的。

class Event { 
    var teamColor:String! 
    var matchup:String! 

    init(dataDictionary:[String:String]) { 
     teamColor = dataDictionary["teamColor"] 
     matchup = dataDictionary["Matchup"] 
    } 
} 

var receivedData = [Event]() 

var tColorValue = "Red" 
var matchValue = "someString" 

var event = Event(dataDictionary: ["teamColor": tColorValue, "Matchup": matchValue]) 

receivedData.append(event) 

func doTable(events: [Event]) { 
    myTable.setNumberOfRows(events.count, withRowType: "someRowController") 

    for (index, evt) in events { 
      let row = myTable.rowControllerAtIndex(index) as someRowController 
      row.interfaceLabel.setText(evt.teamColor) 
    } 
} 

因此,使用上面的代碼,你應該能夠去除eventListFromValues功能,當您收到用戶信息創建接收的值一個新的Event對象並把它添加到數組的結尾。

然後,您的doTable函數將對數組進行迭代,並根據行上的數據設置表的UI部分。

因此,要使用此代碼在您的特定情況下,你可能會做這樣的事情:

class InterfaceController: WKInterfaceController, WCSessionDelegate { 

    var evnts = [Event]() 

    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { 

     if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String { 
      var event = Event(dataDictionary: ["teamColor": tColorValue, "Matchup": matchValue]) 
      events.append(event) 
      doTable() 
     } else { 
      print("tColorValue and matchValue are not same as dictionary value") 
     } 

    } 

    func doTable() { 
     myTable.setNumberOfRows(events.count, withRowType: "someRowController") 

     for (index, evt) in events { 
      let row = myTable.rowControllerAtIndex(index) as someRowController 
      row.interfaceLabel.setText(evt.teamColor) 
     } 
    } 
} 
+0

這很酷,我現在正在嘗試整合它。 On'var event = Event(dataDictionary:[「teamColor」:tColorValue,「Matchup」:matchValue])'line我得到一個錯誤「實例成員tColorValue不能在類型InterfaceController上使用」。任何想法是什麼在那裏解決? (我把你列在我的'InterfaceController'類中的所有代碼放在裏面,讓我知道如果這是錯誤的) – SRMR

+0

你想在一個函數之外做這個嗎? – Scriptable

+0

我在'InterfaceController'類中做了所有的事情,這有意義嗎?我在EDIT 2中添加了代碼,所以你可以看到 – SRMR