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

时间:2014-05-14 13:47:25

标签: ios objective-c

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

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

这是我的代码。我已经定义了一个包含多个字符串的数组:

<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];    
        }

此代码有什么问题?

2 个答案:

答案 0 :(得分:2)

您应该执行以下操作:

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!!!");
        }


    }

这可以让你做两件事:

1)初始化可变数组。 2)解密并记录错误后,不要尝试插入nil字符串。

答案 1 :(得分:1)

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

将其更改为:

  

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