gpt4 book ai didi

ios - *** 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:07 25 4
gpt4 key购买 nike

为什么我总是报错

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' *** First throw call stack: (0x183702db0 0x182d67f80 0x1836799ac 0x18d48f6d4 0x10003999c 0x100039b04 0x188860c40 0x1888790d0 0x188a13e5c 0x18891fe40 0x18891fb1c 0x18891fa84 0x194f41fd0 0x18885c1e4 0x1861ee994 0x1861e95d0 0x1861e9490 0x1861e8ac0 0x1861e8820 0x18885eff4 0x1836b909c 0x1836b8b30 0x1836b6830 0x1835e0c50 0x184ec8088 0x1888ca088 0x10003fd7c 0x18317e8b8) libc++abi.dylib: terminating with uncaught exception of type NSException

下面给出了我的初始 View Controller 的代码

import UIKit
import CoreData
class NotesListTableViewController: UITableViewController {
var managedObjectContext : NSManagedObjectContext!
var entries: [NSManagedObject]!

override func viewDidLoad()
{
super.viewDidLoad()


let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
managedObjectContext = appDelegate.managedObjectContext
//self.fetchEntries()

// makes the searchbar stay in the current screen and not spill into the next screen
//definesPresentationContext = true

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source



override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.entries.count


}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}

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

// Configure the cell...
let entry1 = entries[indexPath.row]
cell.textLabel?.text = entry1.valueForKey("entry_body") as? String


let imageData2 = entry1.valueForKey("entry_image") as? NSData

if let imageData2 = imageData2
{
let myimage = UIImage(data:imageData2,scale:1.0)
cell.imageView?.image = myimage
}





return cell

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier! == "showNote"
{
let detailDisplay = segue.destinationViewController as! DetailDisplayViewController
let selectedIndexPath = tableView.indexPathForSelectedRow!
let entry = entries[selectedIndexPath.row]
detailDisplay.entry = entry
}
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
let entry1 = self.entries[indexPath.row]
self.managedObjectContext.deleteObject(entry1)
self.entries.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
do
{
try managedObjectContext.save()
} catch {
print ("could not save the new entry ")
}


}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = false

self.fetchEntries()

}

func fetchEntries()
{
let fetchRequest = NSFetchRequest(entityName:"Entry")
do {
let entryObjects = try managedObjectContext.executeFetchRequest(fetchRequest)
self.entries = entryObjects as! [NSManagedObject]
}catch let error as NSError
{
print ("could not save the new entry \(error.description) ")
}
tableView.reloadData()



}
}

最佳答案

你试过吗:

  1. 点击您的表格 View
  2. 转到您的属性检查器
  3. 将内容从 Static Cells 更改为 Dynamic Prototypes

enter image description here

关于ios - *** 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304437/

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