__NSArrayM insertObject:atIndex:]; 对象不能为零

我是iOS开发的新手。 我想做encryption和解密。 我的问题是:当我运行我的代码时,解密在模拟器中工作正常,但它不在iPhone设备上运行。 我收到以下错误消息,如下所示:

[__NSArrayM insertObject:atIndex:]: object cannot be nil 

这是我的代码。 我定义了一个包含多个string的数组:

 <dictionary> <array> <string>india,chennai,salem,coimbatore,krishnagiri,hosur,palghat</string> <string>india1,chennai1,salem1,coimbatore1,krishnagiri1,hosur1,palghat1</string> </array> </dictionary> NSString *stringIndex1 = [[NSString alloc]init]; NSMutableArray *arrAthigaaramList1; dictDecryptList=[[NSMutableDictionary alloc]init]; for(int i=0;i<[arrD_Chapter count];i++)//50 Chapter wise { stringIndex1 = [arrD_Chapter objectAtIndex:i]; NSData *b64DecData = [Base64 decode:stringIndex1]; NSData *decryptedData = [[NSData alloc]init]; decryptedData= [b64DecData AESDecryptWithPassphrase:@"mypassword"]; NSString *decryptedStr = [[NSString alloc] initWithData:decryptedData encoding:NSUTF32StringEncoding]; NSLog(@"Decryped Data Base 64 encoded = %@",decryptedStr); [arrAthigaaramList1 addObject:decryptedStr]; } 

这段代码有什么问题?

您应该执行以下操作:

 NSString *stringIndex1 = [[NSString alloc]init]; NSMutableArray *arrAthigaaramList1 = [[NSMutableArray alloc] init]; dictDecryptList=[[NSMutableDictionary alloc]init]; for(int i=0;i<[arrD_Chapter count];i++)//50 Chapter wise { stringIndex1 = [arrD_Chapter objectAtIndex:i]; NSData *b64DecData = [Base64 decode:stringIndex1]; NSData *decryptedData = [[NSData alloc]init]; decryptedData= [b64DecData AESDecryptWithPassphrase:@"mypassword"]; NSString *decryptedStr = [[NSString alloc] initWithData:decryptedData encoding:NSUTF32StringEncoding]; if (decryptedStr) { NSLog(@"Decryped Data Base 64 encoded = %@",decryptedStr); [arrAthigaaramList1 addObject:decryptedStr]; } else { NSLog(@"ERROR decrypting!!!"); } } 

这可以让你做2件事情:

1)初始化可变数组。 2)解密并logging错误后,不要尝试插入零string。

起初看,我会说你的NSMutableArray *arrAthigaaramList1; 没有初始化:)所以你不能添加项目!

将其更改为:

NSMutableArray * arrAthigaaramList1 = [[NSMutableArray alloc] init];