SIGABRT访问NSMutableArray

时间:2011-11-15 16:49:38

标签: objective-c ios cocoa-touch nsmutablearray

我遇到一个特定的错误;我在这条线上得到了“SIGABRT”:

cell.textLabel.text = [collections objectAtIndex: [indexPath row]];

我的标题文件:

#import <UIKit/UIKit.h>

@interface CollectionsViewController : UITableViewController {
    NSMutableArray *collections;
    NSMutableData *responseData;
}
@property (nonatomic, retain) NSMutableArray *collections;

@end

和实施文件:

#import "CollectionsViewController.h"
#import "JSON.h"


@implementation CollectionsViewController
@synthesize collections;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.collections = nil;
}

- (void)viewWillAppear:(BOOL)animated
{

    collections = [[NSMutableArray alloc] init];
    responseData = [[NSMutableData data]retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.unpossible.com/misc/lucky_numbers.json"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];


    [super viewWillAppear:animated];

    //[self.tableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error connecting: \"%@\"", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSArray *luckyNumbers = [responseString JSONValue];


    for (int i = 0; i < [luckyNumbers count]; i++)
        [collections addObject:[luckyNumbers objectAtIndex:i]];

    for (int j = 0; j < [collections count]; j++)
        NSLog(@"This is \"%@\"", [collections objectAtIndex:j]);

    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [collections release];
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [collections count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    NSLog(@"Counter \"%u\"", [collections count]);
    cell.textLabel.text = [collections objectAtIndex: [indexPath row]];
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    [collections release];
    [super dealloc];
}

@end

减少大部分注释掉的内容。

最后我的日志:

2011-11-15 16:41:10.366 Clarke & Clarke[834:b303] This is "13"
2011-11-15 16:41:10.367 Clarke & Clarke[834:b303] This is "3"
2011-11-15 16:41:10.368 Clarke & Clarke[834:b303] This is "73"
2011-11-15 16:41:10.368 Clarke & Clarke[834:b303] This is "31"
2011-11-15 16:41:10.369 Clarke & Clarke[834:b303] Counter "4"
2011-11-15 16:41:10.370 Clarke & Clarke[834:b303] -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b699b0
2011-11-15 16:41:10.372 Clarke & Clarke[834:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b699b0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dde5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f32313 objc_exception_throw + 44
    2   CoreFoundation                      0x00de00bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d4f966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d4f522 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x00151afc -[UILabel setText:] + 72
    6   Clarke & Clarke                     0x0000316f -[CollectionsViewController tableView:cellForRowAtIndexPath:] + 399
    7   UIKit                               0x000a5b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
    8   UIKit                               0x0009b4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    9   UIKit                               0x000b08cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    10  UIKit                               0x000a890c -[UITableView layoutSubviews] + 242
    11  QuartzCore                          0x016c8a5a -[CALayer layoutSublayers] + 181
    12  QuartzCore                          0x016caddc CALayerLayoutIfNeeded + 220
    13  QuartzCore                          0x016700b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    14  QuartzCore                          0x01671294 _ZN2CA11Transaction6commitEv + 292
    15  QuartzCore                          0x0167146d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    16  CoreFoundation                      0x00dbf89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    17  CoreFoundation                      0x00d546e7 __CFRunLoopDoObservers + 295
    18  CoreFoundation                      0x00d1d1d7 __CFRunLoopRun + 1575
    19  CoreFoundation                      0x00d1c840 CFRunLoopRunSpecific + 208
    20  CoreFoundation                      0x00d1c761 CFRunLoopRunInMode + 97
    21  GraphicsServices                    0x010161c4 GSEventRunModal + 217
    22  GraphicsServices                    0x01016289 GSEventRun + 115
    23  UIKit                               0x0003ec93 UIApplicationMain + 1160
    24  Clarke & Clarke                     0x00001b29 main + 121
    25  Clarke & Clarke                     0x00001aa5 start + 53
    26  ???                                 0x00000001 0x0 + 1
)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language:  auto; currently objective-c

我已经尝试但看不到错误。有人可以教育我吗?

4 个答案:

答案 0 :(得分:5)

错误为2011-11-15 16:41:10.370 Clarke & Clarke[834:b303] -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b699b0

您尝试使用int

设置文本

体型:

cell.textLabel.text = [NSString stringWithFormat:@"%d",[collections objectAtIndex: [indexPath row]]];

答案 1 :(得分:0)

当您收到SIGABRT错误时,很多时候是因为您尝试更改的内容不存在。检查所有变量和指针是否已在需要时分配,并且您要分配实际值而不是零。

答案 2 :(得分:0)

仔细看看你给NSMutableArray *收藏品带来了什么,因为有些东西不是你想要的东西:

-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

您的阵列中有NSCFNumber。当你尝试将它添加到textLabel时,如果它是一个字符串(isEqualToString),则SKD会进行查询。所以,它没有。或者,如果没有,你的数组中的NSCFNumber,你需要首先将它修改为字符串:

cell.textLabel.text = [NSString stringWithFormat:"%@", [collections objectAtIndex: [indexPath row]]];

答案 3 :(得分:-2)

尝试更改:

cell.textLabel.text = [collections objectAtIndex: [indexPath row]];

cell.textLabel.text = [collections objectAtIndex:indexPath.row];