2016-11-07 38 views
0

所以我有2个NSMutableArrays,一个叫做testArray,另一个叫做jsonArray。 jsonArray使用json和php从mysql服务器获取对象。然后将这些jsonArray中的相同对象插入到testArray中。我打印(jsonArray,testArray)和日志中显示的是这个。Swift将数组中的对象正确添加到另一个数组?

我也有名为Test一个NSObject类,如果这能帮助..

对于jsonArray

{ 
    testName = GreenCorn; 
    testStatus1 = 12; 
    testStatus2 = 13; 
    testURL = ""; 
    id = 1; 
} 

对于testArray

"<CustomCellSwift.Test: 0x17414df70>" 

现在我是新来的iOS斯威夫特但我不知道我是否将jsonArray正确插入到testArray中。这是我使用的代码。另外,我使用自定义tableview,它应该显示testArray.count,它的空单元格,但它显示了我在jsonArray中的几行。

var followedArray: NSMutableArray = [] 
var testArray: NSMutableArray = [] 

var jsonArray: NSMutableArray = [] 
var filteredArray: NSArray = [] 

var isFiltered: Bool = false 


// Number of Rows in Section 
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if !isFiltered { 
     if section == 0 { 
      return followedArray.count 
     } 
     else if section == 1 { 
      return testArray.count 
     } 
    } 
    return filteredArray.count 

} 


internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let CellIdentifier = "Cell" 
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell 

    if cell != cell { 
     cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) 
    } 

    // Coloring TableView 
    myTableView.backgroundColor = UIColor.white 

    // Configuring the cell 
    var testObject: Test 

    print("before ifFiltered") 

    if !isFiltered { 
     if indexPath.section == 0 { 
      print("isFiltered if") 
      testObject = followedArray[indexPath.row] as! Test 
      cell.populateCell(testObject, isFollowed: true, indexPath: indexPath, parentView: self) 
     } 
     else if indexPath.section == 1 { 
      print("isFiltered if 2") 
      testObject = testArray[indexPath.row] as! Test 
      cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self) 
     } 
    } 
    else { 
     print("isFiltered else") 
     testObject = filteredArray[indexPath.row] as! Test 
     cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self) 
    } 

    return cell 
} 





// Retrieving Data from Server 
func retrieveData() { 

    let getDataURL = "http://exampleip.org/json.php" 
    let url: NSURL = NSURL(string: getDataURL)! 

    do { 

     let data: Data = try Data(contentsOf: url as URL) 
     jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray 

     // Setting up testArray 
     let testArray: NSMutableArray = [] 

     // Looping through jsonArray 
     for i in 0..<jsonArray.count { 

      // Create Test Object 
      let tID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String 
      let tName: String = (jsonArray[i] as AnyObject).object(forKey: "testName") as! String 
      let tStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "testStatus1") as! String 
      let tStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "testStatus2") as! String 
      let tURL: String = (jsonArray[i] as AnyObject).object(forKey: "testURL") as! String 

      // Add Test Objects to Test Array 
      testArray.add(Test(testName: tName, andTestStatus1: tStatus1, andTestStatus2: tStatus2, andTestURL: tURL, andTestID: tID)) 

      print("retrieveData") 
      print(jsonArray, testArray) 
     } 

    } 
    catch { 
     print("Error: (Retrieving Data)") 
    } 

    myTableView.reloadData() 
} 

我这样做是否正确?为什么我的tableview有空单元格?

+0

您需要展示更多代码。为什么testArray是本地的? –

+0

从您的问题不确定,但什么不工作?如果唯一担心的是tableview中的空单元格,您是否也可以提供填充单元格的代码? (在表格的数据源中) –

+0

@johnelemans我更新了我的代码 – BroSimple

回答

0

删除这行代码工作。

let testArray: NSMutableArray = [] 
0

首先,你的网络/串行代码不应该在你的ViewController,但是这是一个更好的方式做的事情:

func retrieveData() { 

    let getDataURL = "http://exampleip.org/json.php" 
    let url: NSURL = NSURL(string: getDataURL)! 

    do { 

     let data: Data = try Data(contentsOf: url as URL) 
     guard let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String : AnyObject]] else { 
      print("Error Retrieving Data") 
      return 
     } 

     let testArray = jsonArray.flatMap(Test.init) 

     // make sure the add the result in your viewController 
     self.myTableView.models = testArray 
    } 
    catch { 
     print("Error: (Retrieving Data)") 
    } 

    myTableView.reloadData() 
} 

extension Test { 

    convenience init?(with jsonDictionary: [String : AnyObject]) { 
     guard let tID = jsonDictionary["id"] as? String, let tName = jsonDictionary["testName"] as? String, let tStatus1 = jsonDictionary["testStatus1"] as? String, 
       let tStatus2 = jsonDictionary["testStatus2"] as? String, let tURL = jsonDictionary["testURL"] as? String else { 
      return nil 
     } 
     self(testName: tName, andTestStatus1: tStatus1, andTestStatus2: tStatus2, andTestURL: tURL, andTestID: tID) 
    } 
} 

我真的无法测试它因此可能会有一些错误,但是这应该指向正确的方向。

+0

这是什么? – BroSimple

+0

该扩展使用JSON字典添加了一个易犯错的初始值设定项,您可以在这里了解地图:https://www.weheartswift.com/higher-order-functions-map-filter-reduce-and-more/ – PeejWeej

+0

I'll尽快试试吧 – BroSimple

相关问题