2011-11-15 75 views
1

我遇到了一個特定錯誤的問題;我得到這條線 「SIGABRT」:關於訪問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 

我試過,但不能看到錯誤。任何人都可以教育我嗎?

回答

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]]]; 
+0

謝謝,這是做的伎倆。它不會將實際的數字顯示爲標題。不知道爲什麼。這就是說我的計劃只是爲了獲得JSON數組並將它們作爲表格行。實際上它應該只是取得字符串。謝謝。 – TMPilot

+0

你應該現在接受他的答案... – aopsfan

+0

對不起,這個網站的新用戶。完成(假設它是綠色的勾號)。乾杯。 – TMPilot

-2

嘗試改變:

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

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

爲什麼這應該有所作爲? (我沒有投票) – Till

0

當您遇到SIGABRT錯誤時,很多次是因爲您嘗試更改的內容不存在。檢查所有變量和指針是否在需要時分配,並且您正在分配實際值而不是零。

0

看carefuly什麼exacly你把你的NSMutableArray *集合,becouse有一些東西是不是你whant什麼是:

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

有NSCFNumber你的陣列英寸當你試圖將它添加到你的textLabel中時,如果它是一個字符串(isEqualToString),SKD就會進行搜索。所以,它不。或者,如果確定,陣列中的NSCFNumber,您需要修改它以先串起:

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