我创建了一个如下课程:
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
都显示为空