Swift对象值被打印到表格视图中,但是在打印到控制台时显示为空

时间:2019-04-27 02:52:31

标签: ios swift xcode uitableview realm

我创建了一个如下课程:

import Foundation
import RealmSwift

class StockCellContent: Object{

    @objc dynamic var StockName: String = ""
    @objc dynamic var image: String = ""
    @objc dynamic var requiredStockPrice: Double = 0.0000
    @objc dynamic var requiredPriceUpOrDown: String = ""
    @objc dynamic var StockPrice: Double = 0.0000
    //var parentCategory = LinkingObjects(fromType: Category.self, property: "StockCellContent")

}

然后我创建了该类的对象,并调用了一个函数,该函数通过api获取数据并将其保存在该对象中。然后,我将该对象添加到要打印在UITableView上的数组中。 UITableView可以很好地打印这些值,但是当我尝试将同一对象传递到其他任何地方或什至将其打印到控制台时,数据将变为nil或已初始化的默认值。我不明白我在做什么错。

功能如下:

func dataReceived(data: String) {
    self.stockNames.append(data)
    self.stockKeyword = data
    //var stockData = StockCellContent()
    //var stockDataTest = StockObjectTest()
    let params = ["function" : "GLOBAL_QUOTE", "symbol" : stockKeyword, "apikey" : stockApiKey]
    let stockData = getStockPriceFromJSON(url: stockApiURL, parameters: params)
    //print(stockData.StockPrice)

    print(stockData.StockPrice)
    self.stockArray.append(stockData)

    //print(data)
    //print(stockData.StockName)
    //print(stockData.StockPrice)
    //print(self.stockArray[0].StockPrice)
    //self.stockCellContents = stockData
    //self.stockList.append(newCategory)
    self.save(stockCellContent: stockData)

    self.stockUpdateFlag = true
    //updateTable()
    //tableView.reloadData()
}

stockData.StockPrice的打印显示0.0 甚至stockData.StockName都显示为空

0 个答案:

没有答案