我正在尝试根据它包含的对象对NSMutableArray进行排序。我在下面的代码段中的这一行收到错误:
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
#import <UIKit/UIKit.h>
@class InboxItem;
@interface InboxTableViewController : UITableViewController<NSXMLParserDelegate> {
NSMutableArray *inboxFaxItems;
NSArray * sortedInboxFaxItems;
InboxItem *_inboxItem;
NSMutableData *xmlData;
NSURLConnection *connectionInprogress;
NSMutableString *inboxFaxesString;
UIActivityIndicatorView *activityIndicator;
}
@property(nonatomic,retain) InboxItem * inboxItem;
-(void) loadInbox;
@end
- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[parser release];
//lets sort by messageID
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"messageID" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors];
[[self tableView] reloadData];
activityIndicator.stopAnimating;
[connectionInprogress release];
connectionInprogress = nil;
[xmlData release];
xmlData = nil;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"InboxFaxItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//I AM GETTING ERROR HERE
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
[[cell textLabel]setText: ptrInboxItem.datetime];
cell.imageView.image = [UIImage imageNamed:@"document.png"];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
MyManager *sharedManager = [MyManager sharedManager];
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
sharedManager.pages = ptrInboxItem.pages;
sharedManager.from =ptrInboxItem.from;
FaxViewController *faxViewController = [[FaxViewController alloc] initWithNibName:@"FaxViewController" bundle:nil];
faxViewController.messageid=ptrInboxItem.messageID;
faxViewController.navigationItem.title=@"View Fax";
[self.navigationController pushViewController:faxViewController animated:YES];
[faxViewController release];
}
答案 0 :(得分:1)
这是因为这一行:
sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors];
您正在将一个您不拥有的对象分配给实例变量。稍后,当您尝试访问它时,该对象已被释放,因此您的实例变量现在指向垃圾。
您应该将该行更改为:
[sortedInboxFaxItems release]; // Release any previous value
sortedInboxFaxItems = [[inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors] retain];
更好。