1

This is my code but i can't getting the data

NSData *wrtData = [NSKeyedArchiver archivedDataWithRootObject:my_Array];
Inder
  • 29
  • 8

2 Answers2

1

If you want to convert a NSArray to NSData you should make sure that all objects inside this array conform to NSCoding protocol. Same goes for NSDictionary.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
1

Use this sample:

   NSArray *mapPoints = @[
                      @{ @"latitude":@"10.010490",
                          @"longitude":@"76.360779",
                          @"altitude":@"30.833334",
                          @"timestamp":@"11:17:23",
                          @"speed":@"0.00",
                          @"distance":@"0.00"
                      },
                      @{
                          @"latitude":@"10.010688",
                          @"longitude":@"76.361378",
                          @"altitude":@"28.546305",
                          @"timestamp":@"11:19:26",
                          @"speed":@"1.614",
                          @"distance":@"198.525711"
                      }
                      ];

NSLog(@"Countries array = %@",mapPoints);
NSData *mapPointsData = [NSKeyedArchiver archivedDataWithRootObject:mapPoints];
NSLog(@"countries data=%@",mapPointsData);
NSLog(@"mapPointsData to mapPointsArray = %@",[NSKeyedUnarchiver unarchiveObjectWithData:mapPointsData]);

Github link:

https://github.com/k-sathireddy/JsonArrayToNSData

KSR
  • 1,699
  • 15
  • 22
  • my array is in json formate, when archivedDataWithRootObject: then i got an error reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x15f2b550 – Inder Dec 13 '16 at 12:21
  • i have the same json formate but on my end its not working, MY Array Count 4000, i got error ::: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x160ebed0 – Inder Dec 13 '16 at 12:34
  • shall i add sample what i have done(same as the answer)? – KSR Dec 13 '16 at 12:38
  • sure @Sathi Reddy – Inder Dec 13 '16 at 12:41