I have created a class as below:

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")

}

then i have created a object of that class and called a function which gets the data through an api and saves it in the object. Then i add the object to an array to be printed on the UITableView. The UITableView prints the values perfectly fine but when i try to pass the same object anywhere else or even print it to the console the data becomes nil or the default value with which it has been initialised. I don't understand what i am doing wrong here.

the function is as below:

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()
}

The print of stockData.StockPrice shows 0.0 even stockData.StockName shows empty

0 Answers