尝试访问“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的按钮时会发生这种情况。请告知您是否可以找到错误。
答案 0 :(得分:2)
嗯,这很可能是因为尝试获取空数组中的第一个元素而导致的简单崩溃。检查代码行如下:
createASkin.skinName=[skinInfo objectAtIndex:1];
如果skinInfo NSArray包含零元素,那么您的代码将会崩溃。你的代码有多个地方像这样调用objectAtIndex,所以你应该重新检查它们以确保不会发生这种崩溃。
答案 1 :(得分:1)
这里没有很多错误检查或防御性编程,特别是考虑到从随机文件导入数据。
该问题可能与example.dat数据文件(或缺少)有关。在viewDidAppear期间是否记录了errString?数据文件是否存在且格式正确?你应该发布example.dat的内容。
是的,错误正在发生,因为你正在调用一个超出数组范围的项目。这很容易解决(在评估数组之前只是进行了一些错误检查),但是首先要弄清楚阵列出错的原因需要进行一些调查。
答案 2 :(得分:0)
尝试访问数组中为空的元素。我的建议是通过安慰来检查你的阵列。