gpt4 book ai didi

ios - 当我输入此代码时应用程序意外退出(使用核心数据)

转载 作者:行者123 更新时间:2023-11-28 14:57:42 25 4
gpt4 key购买 nike

当我输入此代码(使用核心数据)时,应用程序意外退出。我在此屏幕中使用 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)
}

}

最佳答案

这不是您的答案,但您可以找到您的应用实际崩溃的位置。

通过单击加号按钮添加异常断点并再次运行您的应用。

enter image description here

关于ios - 当我输入此代码时应用程序意外退出(使用核心数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167300/

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