[英]NSArray throws exception stating that is (UIAnimator *)
我尝试运行以下代码作为概念验证,但出现以下错误:
*** -[UIAnimator count]: unrecognized selector sent to instance 0x3832610 2009-10-09 00:33:22.355 Concept1[680:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIAnimator count]: unrecognized selector sent to instance 0x3832610'
这个异常似乎是指我之前定义为NSArray的变量(即变量“ keys”),因此对于现在为什么将其更改为UIAnimator感到困惑。 同样令人沮丧的是,我无法在UIAnimator上找到任何文档。
注释掉在viewDidLoad方法中用于分配给“键”的临时数组的释放会使代码运行,但是我无法理解为什么...由于该对象拥有所有权而未能释放“数组”不会导致泄漏呢?
我将非常感激任何人在凌晨1点可以提供的任何帮助,并且对于可能导致这种行为的原因我已经没有足够的想法了。
无论如何,这是代码:
#import "MyViewController.h"
@implementation MyViewController
@synthesize names;
@synthesize keys;
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if (self = [super initWithStyle:style]) {
}
return self;
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My Details";
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"David", @"First Name", @"Johnson", @"Last Name", nil];
self.names = dic;
[dic release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.keys = array;
[array release]; //if this is commented out then the code works. why? won't this leak?
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.names = nil;
self.keys = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [keys count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.text = [names objectForKey:[keys objectAtIndex:row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
}
- (void)dealloc {
[names release];
[keys release];
[super dealloc];
}
@end
您不拥有sortedArrayUsingSelector:
的结果,因此您不能释放它。 该发行版取消了您通过self.keys = array
获得的所有权,这意味着该阵列可以消失。
查看Apple的内存管理规则以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.