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

これは、My Skins を開くボタンをタップすると発生します。バグを見つけることができる場合は、アドバイスしてください。

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 に答える