【问题标题】:prepareForSegue empties my NSManagedObject ArrayprepareForSegue 清空我的 NSManagedObject 数组
【发布时间】:2016-05-26 02:08:37
【问题描述】:

我需要在我正在使用适用于 iOS 8 的最新版本 Swift 开发的当前应用程序中将数据从一个视图传递到另一个视图。我使用一个函数填充 FacultyArray,该函数使用 MagicalRecord 从 CoreData 获取信息。我遇到的问题是,当我尝试在 prepareForSegue 函数中使用填充数组时,结果它是空的。

import UIKit
import SwiftyJSON


class EspecialidadTVC: UITableViewController {

let defaults = NSUserDefaults.standardUserDefaults()
let endpoint = Connection()

var facultyList:Array<Faculty>? = TR_Faculty().get()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources thavaran be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    //return self.facultyArray!.count
    return (self.facultyList?.count)!
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("facultyCell", forIndexPath: indexPath)

    cell.textLabel?.text = self.facultyList![indexPath.row].nombre
    return cell
}

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "presentMainMenuSegue" {
        let splitViewController:MasterSVC = segue.destinationViewController as! MasterSVC

        let indexpath = self.tableView.indexPathForSelectedRow


        splitViewController.faculty = self.facultyList![indexpath!.row]

        //splitViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
    }

    if segue.identifier == "logoutSegue" {

    }
}

}

有没有更好的方法在视图之间发送 NSManagedObject?

更新

这是 MasterSVC 的目标代码。没有控制台错误,只是我的数组被清空了,所以当我发送对象时,这是一个 nil 对象。

import UIKit
import Foundation

class MasterSVC: UISplitViewController {

var faculty:Faculty!

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
    navigationItem.leftItemsSupplementBackButton = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

}

}

更新 2

填充数组的方法

class TR_Faculty {

let dateFormatter = NSDateFormatter()

func store(json: JSON) {

    //Clear previous data
    Faculty.MR_truncateAll()

    for (index,subJson):(String, JSON) in json {
        let fac = Faculty.MR_createEntity()
        fac?.id = Int(subJson["IdEspecialidad"].stringValue)
        fac?.codigo = subJson["Codigo"].stringValue
        fac?.nombre = subJson["Nombre"].stringValue
        fac?.descripcion = subJson["Descripcion"].stringValue
        fac?.updated_at = self.dateFormatter.dateFromString(subJson["updated_at"].stringValue)
    }

    NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
}

func get()-> Array<Faculty>?{
    return Faculty.MR_findAll() as! Array<Faculty>
}

}

【问题讨论】:

  • 您能否展示您的MasterSVC 课程以及您尝试访问faculty 的位置?
  • 您的教师数组是否包含 NSManagedObjects?还是它自己的 ManagedObject?
  • 你得到什么错误?
  • 刚刚添加了 MasterSVC 类,可以在其中读取对象教师。教师数组包含 NSManagedObjects
  • 如果facultyList 真的是空的,我希望你在splitViewController.faculty = self.facultyList![indexpath!.row] 崩溃。在该行之前打印facultyList 的内容,让我们知道它显示的内容。

标签: ios arrays swift core-data nsmanagedobject


【解决方案1】:

我遇到的问题是对服务的调用是异步的,因此在您执行对另一个视图控制器的 segue 之前,无法保证调用会完成。对我有用的只是将performSegueWithIdentifier 放在通话的成功部分中。

作为对刚开始在移动应用程序中使用 API 的其他人的提示,当您想从您的应用程序调用一个 RESTful API 时,您应该进行调用,等待它成功完成,然后填充 SQLite 数据库。之后,您可以调用数据库来填充视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2011-05-10
    • 2018-08-31
    相关资源
    最近更新 更多