【问题标题】:NSArray throws exception stating that is (UIAnimator *)NSArray 抛出异常,说明是 (UIAnimator *)
【发布时间】:2009-10-09 00:13:43
【问题描述】:

作为概念证明,我尝试运行以下代码,但出现以下错误:

*** -[UIAnimator count]:无法识别的选择器发送到实例 0x3832610 2009-10-09 00:33:22.355 Concept1[680:207] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'*** -[UIAnimator count]:无法识别的选择器发送到 实例 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

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    sortedArrayUsingSelector: 的结果不是你所有,所以你不能发布它。该版本取消了您通过 self.keys = array 获得的所有权,这意味着该阵列可以消失。

    查看 Apple 的 memory management rules 了解更多信息。

    【讨论】:

    • 谢谢你。我简直不敢相信我是个白痴。没有分配 = 没有所有权。我知道这一点,但是在检查代码时我对这个问题完全视而不见。再次感谢您让我走上正确的道路:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多