当我输入此代码(使用核心数据)时,应用程序意外退出。我在此屏幕中使用CoreData
对象来添加和删除数据。我不知道为什么每次进入此屏幕时此代码都会崩溃应用程序。
出现此问题:当我在核心数据对象中添加类别以及我获取核心数据对象时。
CoreData.swift
class categoryCoreData {
static func saveData(tfCat: String)
{
let cat = Category(context: context)
cat.category_name = tfCat
// Save the data to coredata
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
static func fetchData() {
do {
addCategory = try context.fetch(Category.fetchRequest())
}
catch {
print("Fetching Failed")
}
}
}
ViewController.swift
class CategoriesVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate, UIGestureRecognizerDelegate
{
@IBOutlet weak var collCategory: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.collCategory.dataSource = self
self.collCategory.delegate = self
//categoryCoreData.fetchData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
self.collCategory.reloadData()
}
//Collection View Data-Source Methods
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return addCategory.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cellID = indexPath.row < addCategory.count ? "CategoryCell" : "ExtraCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
setupCell(cell: cell, indexPath: indexPath, type: cellID)
return cell
}
func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String)
{
switch(type)
{
case "CategoryCell":
setupCategoryCell(cell: cell as! CategoryCollectionCell, indexPath: indexPath)
case "ExtraCell":
setupAddButtonCell(cell: cell as! CategoryExtraCell, indexPath: indexPath)
default:
break
}
}
func setupCategoryCell(cell: CategoryCollectionCell, indexPath: IndexPath)
{
if indexPath.row == 0
{
var countedValue: Int = 0
for i in 0..<addCategory.count
{
let cat = addCategory[i]
let strCatName = cat.category_name
filterAllTask = CategoryTaskFilteredData.filterAllCategory(filteredObject: strCatName!)
//All the tasks counted value and add in variable countedValue
countedValue = filterAllTask.count + countedValue
}
//Counted value of all tasks
cell.lblSubHeader.text = String(format: "%d Task Added", countedValue)
cell.lblHeader.text = "All"
}
else
{
let cat = addCategory[indexPath.row]
cell.lblHeader.text = cat.category_name
filterCategoryTask = CategoryTaskFilteredData.filterCategory(filteredObject: cat.category_name!)
cell.lblSubHeader.text = String(format: "%d Task Added", filterCategoryTask.count)
}
}
func setupAddButtonCell(cell: CategoryExtraCell, indexPath: IndexPath)
{
//Extra Button "Add Button" in a cell
}
//Set the CollectionViewCell size same for all iOS Devices
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let width = collectionView.bounds.width
let height = collectionView.bounds.height
return CGSize(width: (width / 2 - 1),height: height / 3)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
self.collCategory.allowsMultipleSelection = false
if indexPath.row < addCategory.count
{
print("Main Cell")
let categoryTaskVC = self.storyboard?.instantiateViewController(withIdentifier: "CategoryTaskVC") as! CategoryTaskVC
let cat = addCategory[indexPath.row]
categoryTaskVC.strHeader = cat.category_name!
if indexPath.row == 0
{
print("entered 0")
categoryTaskVC.selectedIndexPath = indexPath.row
}
else
{
for i in 1..<addCategory.count
{
if indexPath.row == i
{
print("entered \(i)")
categoryTaskVC.selectedIndexPath = indexPath.row
}
else {
print("error")
}
}
}
self.navigationController?.pushViewController(categoryTaskVC, animated: true)
}
else
{
print("Add New Cell")
self.blurEffects()
view.addSubview(blurEffectView)
//Alert View Controller when Adding Categories...
let inputBox = BMInputBox.boxWithStyle(.plainTextInput)
inputBox.blurEffectStyle = .extraLight
inputBox.title = NSLocalizedString("Add Category", comment: "")
inputBox.message = NSLocalizedString("Please enter unique category name.", comment: "")
inputBox.customiseInputElement = {(element: UITextField) in
element.placeholder = "Enter a category"
return element
}
inputBox.submitButtonText = NSLocalizedString("OK", comment: "")
inputBox.onSubmit = {(value: String...) in
//Store value in text field in "text" object.
for text in value
{
let strCategory = text
print("STR CATE : \(strCategory)")
DispatchQueue.main.async(execute: {
//Store category in CoreData
categoryCoreData.saveData(tfCat: strCategory)
//Fetch Category Data
categoryCoreData.fetchData()
self.collCategory.reloadData()
})
}
self.blurEffectView.removeFromSuperview()
}
inputBox.cancelButtonText = NSLocalizedString("Cancel", comment: "")
inputBox.onCancel = {
//Remove blur effects from Superview
self.blurEffectView.removeFromSuperview()
}
inputBox.show()
}
}
@IBAction func btnAddTask(_ sender: Any)
{
let addTaskVC = self.storyboard?.instantiateViewController(withIdentifier: "AddTaskVC") as! AddTaskVC
self.navigationController?.pushViewController(addTaskVC, animated: true)
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
情况可能并非如此,但有时自动生成的核心数据文件会搞砸。确保您已清理项目产品 - &gt;清洁。然后清理构建文件夹产品 - &gt;&#34;清理构建文件夹&#34; (你需要按住&#34; alt&#34;按下&#34;产品&#34;)。然后删除派生数据Xcode-&gt; preferences-&gt; Locations,然后按下面的右箭头&#34;派生数据&#34;,输入&#34;派生数据&#34;文件夹并删除名称与您的应用程序类似的文件夹(该文件夹将立即弹出,但不要担心)。现在尝试重建。
还要确保没有警告。当你忘记在核心数据模型中分配一些数据类型时,这个问题似乎就会产生。
完成这两项操作并且问题仍然存在后,我建议您使用此信息更新您的问题。