0

I am a newcomer to iOS development. I would like to do encryption and decryption. My question is the following: When I was running my code, the decryption was working fine in the simulator but it is not running on an iPhone device. I get the following error message shown below:

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

Here is my code. I have defined an array containing multiple strings:

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

What is wrong with this code?

1
  • I'm pretty sure your line NSString *decryptedStr = [[NSString alloc] initWithData:decryptedData encoding:NSUTF32StringEncoding]; is resulting in a nil string. What's the output of your log? Commented May 14, 2014 at 13:58

2 Answers 2

2

You should do the following:

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


    }

This would allow you do 2 things:

1) Initialise the mutable array. 2) Do not try to insert a nil string after decrypting and logging the error.

Sign up to request clarification or add additional context in comments.

Comments

1

At first look, I would say that your NSMutableArray *arrAthigaaramList1; is not initialized :) so you can't add item !

Change it to :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.