0
votes

I'm trying to learn the basics of Obj C from "Learning Objective C 2.0 by Robert Clair"

I have an array of NSNumber objects that I write the Array into an NSData objects buffer. What I'd like to do is validate the NSNumber objects were written correctly using something like NSLog but I just get garbage when I attempt this.

Here is my Code :

    int howManyBytes3 = 100;
    NSMutableArray *myArray3 = [NSMutableArray array]; //Creates an empty NSArray
    for (NSInteger i=0; i<50; i++)
    {
        [myArray3 addObject:[NSNumber numberWithInteger:i]];
    }

    NSLog(@"My Array 3 contains : %@", myArray3);


    NSMutableData  *myData3 = [NSMutableData dataWithBytesNoCopy:myArray3 length:howManyBytes3 freeWhenDone:NO];
    NSLog(@"myData3 contains : %@", myData3);

and this is the output I get on console:

2018-09-16 13:24:39.396705+0100     Chapter9[12351:1341392] myData3 contains :   <917f30a8 ffff1d00 00000000 00000000 30186200 

01000000 00000000 44000000 33000000 32000000 00000000 00000000 00000000 00000000 01050610 00000010 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 6ad3364c> Program ended with exit code: 0

What am I doing wrong?

1
What do you expect from NSLog in this case? Did it do anything wrong?trungduc
I was hopping to get a log output of the NSData objects buffer, to confirm the data in the buffer is correct. Similar to when printing the array as an object but I expected the data to just be in hexhoboBob
What you are doing is complete nonsense. You can't access an NSArray as a byte array and expect anything other than complete garbage.gnasher729

1 Answers

1
votes

NSMutableArray is not C array, it is an object reference. You are trying to print internal byte layout of NSObject.

For this,

1) You need object serialization mechanism - e.g. NSKeyedArchiver

2) You need byte to character conversion => How to display hexadecimal bytes using NSLog