基本上问题正是标题所说的。
我的应用程序在模拟器上运行顺畅,没有任何崩溃。事实上,以前的版本在应用商店。我在这里和那里做了一些小改动,突然它开始在一个非常奇怪的地方崩溃。
我[NSBundle mainBundle] resourcepath]
在代码中的各个地方使用来访问 plist 文件、图像等。前几次调用NSBundle mainBundle]
是完全正常的 - 正如预期的那样 - 但是,在某些时候它会返回......
-[NSBundle < null selector>]: unrecognized selector sent to instance 0x10a0e0
...并在设备上崩溃。这是确切的代码片段:
-(void) setImageName:(NSString *)s
{
[imageName release];
imageName = [s copy];
NSLog(@"last line before crash");
NSString *imagePath =[[NSBundle mainBundle] resourcePath];
NSLog(@"Why would it crash before here???");
imagePath = [imagePath stringByAppendingString:imageName];
imageUI = [[UIImage alloc] initWithContentsOfFile:imagePath];
[self setNeedsDisplay];
}
为了检查那个调用是否真的是问题,我在项目resourcePath
的第一次调用中保存到一个字符串中[NSBundle mainbundle]
(前几次调用完全正常,如上所述)并resourcePath
在我需要的任何地方使用该字符串 [NSbundle mainbundle] 瞧!没有崩溃/泄漏,没有...
我完全糊涂了..为什么该调用会使我的应用程序在设备上而不是模拟器上崩溃?
编辑:使用...
NSArray *array = [NSBundle allBundles];
NSBundle *bundle = [array objectAtIndex:0];
NSString *imagePath = [bundle bundlePath];
...而不是[[NSBundle mainBundle] resourcePath]
工作。我想我正在做一些只影响这个特定调用的事情。
编辑 2:这是我在 -[NSObject doesNotRecognizeSelector:] 中设置断点时的回溯:
#0 0x30e27b98 in -[NSObject doesNotRecognizeSelector:]
#1 0x30dacb18 in ___forwarding___
#2 0x30da3840 in __forwarding_prep_0___
#3 0x0000bcfe in -[CustomTableViewCell setImageName:] at CustomTableViewCell.m:93
#4 0x0000499e in -[RootTableViewController tableView:willDisplayCell:forRowAtIndexPath:] at RootTableViewController.m:469
#5 0x3364d5d0 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]
#6 0x3364cde0 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:]
#7 0x335f832c in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow]
#8 0x335f6514 in -[UITableView layoutSubviews]
#9 0x335f22d8 in -[UIView(CALayerDelegate) _layoutSublayersOfLayer:]
#10 0x32bac1c0 in -[CALayer layoutSublayers]
#11 0x32babedc in CALayerLayoutIfNeeded
#12 0x32bab844 in CA::Context::commit_transaction
#13 0x32bab474 in CA::Transaction::commit
#14 0x32bb35dc in CA::Transaction::observer_callback
#15 0x30da1830 in __CFRunLoopDoObservers
#16 0x30de9346 in CFRunLoopRunSpecific
#17 0x30de8c1e in CFRunLoopRunInMode
#18 0x332e7374 in GSEventRunModal
#19 0x335adc30 in -[UIApplication _run]
#20 0x335ac230 in UIApplicationMain
... 其中 #3 - CustomTableViewCell.m:93 是 NSString *imagePath =[[NSBundle mainBundle] resourcePath]; 在我上面发布的代码部分。