从核心数据中获取记录时应用程序崩溃

时间:2014-10-07 09:17:17

标签: ios uitableview core-data ios7 xcode5

嗨朋友我的应用程序在核心数据中执行获取请求时崩溃。下面是崩溃时显示的消息。

&#34; 2014-10-07 14:38:07.203 Social [1540:60b] - [AppDelegate fetchAllAds]:无法识别的选择器发送到实例0x9725da0 2014-10-07 14:38:07.209社交[1540:60b] *由于未捕获的异常而终止应用&#39; NSInvalidArgumentException&#39;,**原因:&#39; - [AppDelegate fetchAllAds]:< / strong>无法识别的选择器发送到实例0x9725da0&#39; ***首先抛出调用堆栈:

(
0   CoreFoundation                      0x025201e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x01e1e8e5 objc_exception_throw + 44
2   CoreFoundation                      0x025bd243 -[NSObject(NSObject)  
doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0251050b ___forwarding___ + 1019
4   CoreFoundation                      0x025100ee _CF_forwarding_prep_0 + 14
5   Social                              0x00002e5b -[ShoppingCartViewController viewDidLoad] +  
139
6   UIKit                               0x00bfd33d -[UIViewController loadViewIfRequired] + 696
7   UIKit                               0x00bfd5d9 -[UIViewController view] + 35
8   UIKit                               0x00c0cf89 -[UIViewController shouldAutorotate] + 36
9   UIKit                               0x00c0d2d1 -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:] + 297
10  UIKit                               0x00eab3d5 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 2330
11  UIKit                               0x00c095d5 -[UIViewController presentViewController:withTransition:completion:] + 6538
12  UIKit                               0x00c09aef -[UIViewController presentViewController:animated:completion:] + 130
13  UIKit                               0x00c09b2f -[UIViewController presentModalViewController:animated:] + 56
14  UIKit                               0x01053e00 -[UIStoryboardModalSegue perform] + 271
15  UIKit                               0x01042f0c -[UIStoryboardSegueTemplate _perform:] + 174
16  UIKit                               0x01042f87 -[UIStoryboardSegueTemplate perform:] + 115
17  libobjc.A.dylib                     0x01e30880 -[NSObject performSelector:withObject:withObject:] + 77
18  UIKit                               0x00ae03b9 -[UIApplication sendAction:to:from:forEvent:] + 108
19  UIKit                               0x00ae0345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
20  UIKit                               0x00be1bd1 -[UIControl sendAction:to:forEvent:] + 66
21  UIKit                               0x00be1fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
22  UIKit                               0x00be1243 -[UIControl touchesEnded:withEvent:] + 641
23  UIKit                               0x00b1fddd -[UIWindow _sendTouchesForEvent:] + 852
24  UIKit                               0x00b209d1 -[UIWindow sendEvent:] + 1117
25  UIKit                               0x00af25f2 -[UIApplication sendEvent:] + 242
26  UIKit                               0x00adc353 _UIApplicationHandleEventQueue + 11455
27  CoreFoundation                      0x024a977f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
28  CoreFoundation                      0x024a910b __CFRunLoopDoSources0 + 235
29  CoreFoundation                      0x024c61ae __CFRunLoopRun + 910
30  CoreFoundation                      0x024c59d3 CFRunLoopRunSpecific + 467
31  CoreFoundation                      0x024c57eb CFRunLoopRunInMode + 123
32  GraphicsServices                    0x03ca25ee GSEventRunModal + 192
33  GraphicsServices                    0x03ca242b GSEventRun + 104
34  UIKit                               0x00adef9b UIApplicationMain + 1225
35  Social                              0x00017a2d main + 141
36  libdyld.dylib                       0x0393b701 start + 1
37  ???                                 0x00000001 0x0 + 1

) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

以下是我的代码。

- (void)viewDidLoad
{
    DBManager* manager = [UIApplication sharedApplication].delegate;
    self.fetchedRecordsArray = [manager fetchAllAds];
    [self.shoppingtbl reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fetchedRecordsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
    *)indexPath
{    
    static NSString *CellIdentifier = @"ShoppingCart";
    ShoppingCart *cell = (ShoppingCart*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
    forIndexPath:indexPath];
    Ad * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
    cell.addesc.text = [NSString stringWithFormat:@"%@",record.data];
    return cell;
}

注意我已经在名为dbmanager的单独文件中退出了所有核心数据函数。

2 个答案:

答案 0 :(得分:0)

它告诉您AppDelegate没有fetchAllAds函数。

就像快速说明一样。无论如何不应该具有该功能。

应用代理中缺少(或错误输入)的内容。如果您无法解决此问题,则需要在应用代理中显示代码。

修改

哦!我刚刚意识到你有一个名为DBManager的班级,但你加载它的方式......

DBManager* manager = [UIApplication sharedApplication].delegate;

实际上是将UIAppDelegate放入属性。

你需要像...这样的东西。

DBManager *manager = [[DBManager alloc] init];

这将停止错误。

这是做什么......

当您运行[manager fetchAllAds];时,它会获取属性manager

这是对App Delegate的引用,因为您正在加载它。

编辑1

好的,听起来好像你的DBManager是你的app委托的属性。

然后你可以像这样加载......

DBManager *manager = [[UIApplication sharedApplication] delegate].dbManager; //or whatever the property is.

答案 1 :(得分:0)

朋友这不是coreData的问题,但是你在viewDidLoad上访问了错误的对象

- (void)viewDidLoad
{
DBManager* manager = [UIApplication sharedApplication].delegate;
self.fetchedRecordsArray = [manager fetchAllAds];
[self.shoppingtbl reloadData];
}

此处您的代码[UIApplication sharedApplication].delegate;将返回appDelegate而不是DBManager的对象,并且您将其存储在DBManager中,这是错误的。因此,如果你的appDelegate中有一个属性dbManager,那么以这种方式访问​​[[[UIApplication sharedApplication] delegate] dbManager]

因此,如果您的应用中有DBManager属性,请以这种方式访问​​它

- (void)viewDidLoad
    {
    DBManager* manager = [[[UIApplication sharedApplication] delegate] dbManager];
    self.fetchedRecordsArray = [manager fetchAllAds];
    [self.shoppingtbl reloadData];
    }

或在此处创建新对象

- (void)viewDidLoad
    {
    DBManager* manager = [[DBManager alloc] init];
    self.fetchedRecordsArray = [manager fetchAllAds];
    [self.shoppingtbl reloadData];
    }