本文介绍了核心数据 - 希望出现错误,获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Core Data开发一个iPhone应用程序,ahev注意到当我期望和错误时,我得到异常,这是一个例子:

I am developing an iPhone app using Core Data and ahev noticed that I get exceptions when I would expect and error, this is an example:

NSError *error;

// exception here if edited attribute name todoText in modeller and generated new database with that new name in it(ie clearing the iphone), ie tring to access a field not in the database

@try {

   NSMutableArray *mutableFetchResults = [[todoListManagedObjectContext   executeFetchRequest:request error:&error] mutableCopy];

   //seems like vars declaered inside a try is only known inside it, so process here

   if (mutableFetchResults == nil) { // nil = error

      // Handle the error.

如在注释中看到的,我没有得到一个错误,试图访问一个不存在的字段。 ...为什么不使用错误返回系统为此?

As seen in the comments I don't get an error for trying to access a non existing field....why not just use the error return system for this?

我想我的问题是,什么是错误和例外,我真的需要测试这样吗?

I guess my question is, what are errors and exceptions for, and do I really need to test for both like this?

Rgds
PM

RgdsPM

推荐答案

在Cocoa中,异常通常意味着程序员错误,所以它建议你传递一个无效的参数。试图访问不在数据库中的字段看起来像这样的情况;它不是一个错误的东西的错误,它是一个你做错了错误。

What exception are you getting? Throughout Cocoa, exceptions generally mean programmer error so it suggests you're passing a parameter which is not valid. Trying to access a field which isn't in the database seems like such a situation; it's not a "something went wrong" error, it's a "you did something wrong" error.

这篇关于核心数据 - 希望出现错误,获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:47