0

尝试访问“MySkinsView”时出现以下错误

2013-07-06 14:57:28.523 Skin Creator[778:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'
*** First throw call stack:
(0x335662a3 0x3b1d997f 0x335661c5 0x334dec01 0x77741 0x353b954d 0x3539e313 0x353b57cf 0x35371803 0x3511bd8b 0x3511b929 0x3511c85d 0x3511c243 0x3511c051 0x3511beb1 0x3353b6cd 0x335399c1 0x33539d17 0x334acebd 0x334acd49 0x3705d2eb 0x353c2301 0x6b171 0x6b0f8)
libc++abi.dylib: terminate called throwing an exception

我的 .m 如下:

#import "MySkinsView.h"
#import <QuartzCore/QuartzCore.h>
#import "CreateASkinView.h"
#import "SkinManipulator.h"


@interface MySkinsView ()

@end

@implementation MySkinsView{
    int deletedIndexPath;
}
@synthesize customCell, cellImage, cellLabel, array, alertView, TableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *arrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];
    NSString *errString;
    NSData *serialized = [NSData dataWithContentsOfFile:arrayFileName];

    array =
    [NSPropertyListSerialization propertyListFromData:serialized
                                     mutabilityOption:NSPropertyListMutableContainers
                                               format:NULL
                                     errorDescription:&errString];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *arrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];
    NSString *errString;
    NSData *serialized = [NSData dataWithContentsOfFile:arrayFileName];

    array =
    [NSPropertyListSerialization propertyListFromData:serialized
                                     mutabilityOption:NSPropertyListMutableContainers
                                               format:NULL
                                     errorDescription:&errString];
    if (errString)
    {
        NSLog(@"%@", errString);
    }

    [super viewDidLoad];
    [TableView reloadData];
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [array count];
}

-(UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"cell identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = customCell;
        self.customCell = nil;
    }
    NSArray *skinInfo=[array objectAtIndex:indexPath.row];

    cellLabel.text=[skinInfo objectAtIndex:1];
    cellLabel.font=[UIFont fontWithName:@"CurseCasualRegular" size:17];
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableviewbg.png"]];
    UIImage *skin=[UIImage imageWithData:[skinInfo objectAtIndex:0]];
    UIImage *frontView=[[SkinManipulator alloc]createFrontViewOfSkin:skin];
    cellImage.image=frontView;
    cellImage.layer.magnificationFilter=kCAFilterNearest;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray *skinInfo=[array objectAtIndex:indexPath.row];

    CreateASkinView *createASkin;
    createASkin = [[CreateASkinView alloc] initWithNibName:@"CreateASkinView" bundle:nil];
    createASkin.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    createASkin.indexPathOfSkin=indexPath.row+1;
    createASkin.skin=[UIImage imageWithData:[skinInfo objectAtIndex:0]];
    createASkin.skinName=[skinInfo objectAtIndex:1];
    [self presentViewController:createASkin animated:YES completion:nil];
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        alertView=[[UIAlertView alloc]initWithTitle:@"Are you sure you want to delete this skin?" message:nil delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
        deletedIndexPath=indexPath.row;
        [alertView show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *arrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];
        [array removeObjectAtIndex:deletedIndexPath];
        NSString *errString;
        NSData *serialized =
        [NSPropertyListSerialization dataFromPropertyList:array
                                                   format:NSPropertyListBinaryFormat_v1_0
                                         errorDescription:&errString];
        [serialized writeToFile:arrayFileName atomically:YES];
        [TableView reloadData];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

当我点击打开我的皮肤的按钮时会发生这种情况。请告知您是否可以找到错误。

4

3 回答 3

2

好吧,这很可能是由于尝试获取空数组中的第一个元素而导致的简单崩溃。检查看起来像这样的代码行:

createASkin.skinName=[skinInfo objectAtIndex:1];

如果 skinInfo NSArray 包含零个元素,那么您的代码将像那样崩溃。您的代码在多个地方像这样调用 objectAtIndex,因此您应该重新检查它们以确保不会发生这种崩溃。

于 2013-07-06T03:20:35.940 回答
1

这里没有太多的错误检查或防御性编程,特别是考虑到它是从随机文件导入数据。

该问题可能与 example.dat 数据文件(或缺少该文件)有关。在 viewDidAppear 期间是否记录了 errString?数据文件是否存在并且格式正确?您应该发布 example.dat 的内容。

是的,错误正在发生,因为您在数组中调用了一个超出范围的项目。这很容易解决(只需在评估数组之前进行一些错误检查),但首先要弄清楚为什么数组格式错误需要一些调查。

于 2013-07-06T03:49:15.813 回答
0

试图访问空数组中的元素。我的建议是通过安慰它来检查你的数组。

于 2014-04-05T07:42:49.993 回答