【发布时间】: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