gpt4 book ai didi

ios - 为什么我的 View Controller 没有检索到我的 CoreData 对象

转载 作者:行者123 更新时间:2023-11-30 13:47:52 25 4
gpt4 key购买 nike

所以我正在开发这个 xCode 项目,用 Swift 语言制作 iPhone 应用程序。我试图将两个字符串和一个二进制数据图像存储在核心数据的一个对象中,我可以在此 viewcontroller 中检索该对象。我用来转换到此 View 的 TableView 显示了正确的项目关联,但是当我加载此 View 时,属性未加载。为什么对象没有上传并显示到 viewcontroller

import UIKit
import CoreData

class IndividualMainViewController: UIViewController, NSFetchedResultsControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{

@IBOutlet weak var backCancelButton: UIBarButtonItem!
@IBOutlet weak var saveEditButton: UIBarButtonItem!

@IBOutlet weak var deviceImageButton: UIButton!
@IBOutlet weak var cameraImageButton: UIButton!


var item: Item? = nil
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let frc: NSFetchedResultsController = NSFetchedResultsController()


@IBOutlet weak var imageHolder: UIImageView!

@IBOutlet weak var horseName: UITextField!
@IBOutlet weak var officialName: UITextField!

override func viewDidLoad()
{
super.viewDidLoad()


if(item == nil)
{
print("item returned nil")
print(item)
}
else
{
print("Object did load")
self.horseName.text = item?.name
self.officialName.text = item?.offName
self.imageHolder.image = UIImage(data: (item?.image)!)

saveEditButton.title = "Edit"
backCancelButton.title = "To Barn"

self.deviceImageButton.hidden = true

self.cameraImageButton.hidden = true

}

let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissEditor")
view.addGestureRecognizer(tap)


}

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

func dismissEditor()
{
view.endEditing(true)
}



@IBAction func addImageFromDevice(sender: AnyObject)
{
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerController.allowsEditing = true

self.presentViewController(pickerController, animated: true, completion: nil)
}


@IBAction func addImageFromCamera(sender: AnyObject)
{
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.Camera
pickerController.allowsEditing = true

self.presentViewController(pickerController, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?)
{
self.dismissViewControllerAnimated(true, completion: nil)

self.imageHolder.image = image
}



@IBAction func saveEditTapped(sender: AnyObject)
{
if(saveEditButton.title == "Save")
{
if item != nil
{
editItem()

print("Item edit saved")

saveEditButton.title = "Edit"
backCancelButton.title = "To Barn"
}
else
{
createNewItem()

print("New item created")

performSegueWithIdentifier("back", sender: nil)
}
}
else
{
saveEditButton.title = "Save"
backCancelButton.title = "Cancel"
print("Not saved")
}
}



func createNewItem()
{
let entityDescription = NSEntityDescription.entityForName("Item", inManagedObjectContext: moc)

let item = Item(entity: entityDescription!, insertIntoManagedObjectContext: moc)

item.name = horseName.text
item.offName = officialName.text
item.image = UIImagePNGRepresentation(imageHolder.image!)

do
{
try self.moc.save()
}
catch
{
print("Failed ot create new object")
return
}
}



func editItem()
{
item?.name = horseName.text
item?.offName = officialName.text
item!.image = UIImagePNGRepresentation(imageHolder.image!)

do
{
try self.moc.save()
}
catch
{
print("Failed to save item")
return
}
}
}

最佳答案

仅使用通用初始化程序初始化 NSFetchedResultsController 是不够的。

您必须告诉 Controller 要做什么、要使用哪个托管对象上下文、设置委托(delegate)并执行初始获取。

Swift 中通常的方法是将 Controller 声明为惰性计算变量。

lazy var fetchedResultsController: NSFetchedResultsController = {

let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName(self.entity, inManagedObjectContext: self.managedObjectContext)
fetchRequest.entity = entity
fetchRequest.predicate = self.fetchPredicate

// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20

// Edit the sort key as appropriate.
fetchRequest.sortDescriptors = [self.sortDescriptor]

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self

do {
try aFetchedResultsController.performFetch()
} catch let error as NSError {
print(error)
}

return aFetchedResultsController
}()

将实例变量(以 self. 开头)表示的所有参数替换为您的值

关于ios - 为什么我的 View Controller 没有检索到我的 CoreData 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34698825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com