Ios swift,从核心数据中删除对象,仍然附加空对象?

Ios swift,从核心数据中删除对象,仍然附加空对象?,ios,uitableview,swift,core-data,Ios,Uitableview,Swift,Core Data,我正在尝试设置一个UITableView,其中所有对象都位于一个实体中 但是,我正在从api加载数据。所以每次加载时,我都会删除实体中的所有对象。并添加新的 然而,当我这样做的时候。它显示了5个单元格和api数据,每次我加载它。它添加了5个空单元格 它添加空单元格的原因是因为我定义了numberOfRowsInSection,对象计数如下: func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&g

我正在尝试设置一个UITableView,其中所有对象都位于一个实体中

但是,我正在从api加载数据。所以每次加载时,我都会删除实体中的所有对象。并添加新的

然而,当我这样做的时候。它显示了5个单元格和api数据,每次我加载它。它添加了5个空单元格

它添加空单元格的原因是因为我定义了
numberOfRowsInSection
,对象计数如下:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return stocks.count
}
其中股票为:
var股票=[NSManagedObject]()

因此,我假设这是因为我的实体中有一些空的对象

以下是我试图设置数据的方式:

    func loadSuggestions(Formula: Int) {
    println("----- Loading Data -----")
    // Check for an internet connection
    if Reachability.isConnectedToNetwork() == false {
        println("ERROR: -> No Internet Connection <-")
    } else {

        // Delete all the current objects in the entity
        let fetchRequest = NSFetchRequest(entityName: formulaEntity)
        let a = managedContext.executeFetchRequest(fetchRequest, error: nil) as! [NSManagedObject]
        for mo in a {
            managedContext.deleteObject(mo)
            //println("removed \(mo)")
        }
        // save the context after removing objects.
        managedContext.save(nil)

        // Setting up a fetch request to get the api data.
        let entity =  NSEntityDescription.entityForName("\(formulaEntity)", inManagedObjectContext:managedContext)

        var request = NSURLRequest(URL: formulaAPI!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        var formula = JSON(data: data!)

        for (index: String, actionable: JSON) in formula["actionable"] {
            stockName = actionable["name"].stringValue
            ticker = actionable["ticker"].stringValue
            action = actionable["action"].stringValue
            suggestedPrice = actionable["suggested_price"].floatValue
            weight = actionable["percentage_weight"].floatValue

            // Set the values in CoreData
            let stock = NSManagedObject(entity: entity!,insertIntoManagedObjectContext:managedContext)

            stock.setValue(stockName, forKey: "name")
            stock.setValue(ticker, forKey: "ticker")
            stock.setValue(action, forKey: "action")
            stock.setValue(suggestedPrice, forKey: "suggestedPrice")
            stock.setValue(weight, forKey: "weight")

            // Set up second api fetch
            var quoteAPI = NSURL(string: "http://dev.markitondemand.com/Api/v2/Quote/json?symbol=\(ticker)")

            var quoteRequest = NSURLRequest(URL: quoteAPI!)
            var quoteData = NSURLConnection.sendSynchronousRequest(quoteRequest, returningResponse: nil, error: nil)
            if quoteData != nil {
                var quote = JSON(data: quoteData!)
                betterStockName = quote["Name"].stringValue
                lastPrice = quote["LastPrice"].floatValue

                // Setting the last values in CoreData
                if betterStockName != "" {
                    stock.setValue(betterStockName, forKey: "name")
                }
                stock.setValue(lastPrice, forKey: "lastPrice")

            } else {
                println("ERROR - NO DATA")
            }

            var error: NSError?
            if !managedContext.save(&error) {
                println("Could not save \(error), \(error?.userInfo)")
            }
            // finally add the stockdata to the CoreData entity.
            stocks.append(stock)


        }



        // Reload the tableview with the new data.
        self.tableView.reloadData()
    }
}
func加载建议(公式:Int){
println(“----加载数据------”)
//检查互联网连接
如果可达性.isconnectedtonework()=false{

println(“错误:->没有Internet连接原因很简单,因为您只在
股票
中添加新条目,您永远不会删除任何内容。如果您永远不会从数组中删除任何内容,它将永远不会变小。”


您确实从核心数据中删除了对象,但这不会自动将其从阵列中删除。它们不会因为核心数据不再包含而从内存和阵列中消失。从核心数据中删除对象后,您需要单独调用阵列上的
removeAll

您是否已注销J返回的子数据?可能您得到的是空对象。是的,它打印出来很好。Tom Harrington在下面的回答中发现了问题。