Code:
#import <Foundation/Foundation.h>
#import "class.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
class* cls = [class new];
//NSLog(@"Struct is:%d",cls->s.shot);
You should have left this NSLog() uncommented, so we can see what you're expecting to get.
Code:
NSString* szEventDescriptions = [[NSString alloc]init];
You're leaking this NSString.
Code:
NSNumber* s = [NSNumber new];
You're leaking this NSNumber.
Code:
NSMutableDictionary* dict = [NSMutableDictionary new];
NSMutableData* myData = [NSMutableData new];
You're leaking both of these objects.
Code:
myData = [NSMutableData dataWithBytes:[NSNumber numberWithShort:cls->s.shot] length:sizeof(cls->s.shot)];
This is nonsense. You seem to be expecting that an NSNumber* is simply a pointer to a struct. This expectation is wrong.
The NSNumber* returned by this code:
Code:
[NSNumber numberWithShort:cls->s.shot]
is an Objective-C object. It is not simply a pointer to a struct. It's not a pointer to a short, either. Nor a pointer to an int.
Any time the -> operator is used with an object, it refers to an instance variable. An instance variable, however, is not a struct member. It's an instance variable. The compiler knows the difference, and the two things are not interchangeable.
So the expression cls->s.shot yields an unsigned int 32 by dereferencing an instance variable. That value (32) is then used to make an NSNumber*. That part is correct. The error lies in thinking that the NSNumber* is a plain pointer to an int, or to a short, or to any other non-opaque data type. It's none of those things. It's a pointer to an opaque object.
So given the NSNumber*, which is not a simple pointer to a struct, the following is meaningless:
Code:
[NSMutableData dataWithBytes:[NSNumber numberWithShort:cls->s.shot] length:sizeof(cls->s.shot)];
What this does is take some number of bytes from the NSNumber*, and stuff them into an NSMutableData object. The number of bytes is the sizeof an unsigned int (4). The actual bytes, however, are whatever the first 4 bytes of the NSNumber object are. Those bytes ARE NOT the s.shot value (32). The bytes are something internal to the object, as defined by the Objective-C runtime's requirements for what constitutes an object.
My guess is that you're expecting Objective-C to be like C++. It's not. You need to learn Objective-C on its own terms, as the language it actually is, not some preconceived idea that an object is just like a C or C++ struct.
Code:
[dict setObject:myData forKey:@"1"];
Nothing wrong here.
Code:
szEventDescriptions = [dict objectForKey:@"1"];
This is wrong. The declared type for szEventDescriptions is NSString* (see above). The actual object that was just stored into the dictionary, however, is not an NSString*, but an NSMutableData*.
Code:
NSLog(@"%@",szEventDescriptions);
The %@ format specifier uses the -description method of the object to return an NSString*. For an NSData object, the -description method returns the hex bytes of data between <>'s.
To summarize, what you've effectively done is store 4 bytes of unpredictable data into an NSMutableData object, then printed its -description string.
The reason the 4 bytes are unpredictable is because you're using an NSNumber* as if it were a plain ordinary struct pointer, when in fact it's an object pointer. So you're getting the first 4 bytes of an object, which is an internal value, not a pointer to an instance variable.
What book or online tutorial are you learning Objective-C from?
What is your programming background? Do you already know plain C? Do you know C++?
You should probably start with this:
http://developer.apple.com/library/.../ObjectiveC/Introduction/introObjectiveC.html
For all the places where you're leaking objects, you'll need this:
http://developer.apple.com/library/...timeGuide/Articles/ocrtHowMessagingWorks.html
I recommend that you skip the low-level stuff for now, and simply learn the fundamentals of the language. Until you get that right, and stop expecting it to be just like C structs, I think the low-level stuff will be more a hindrance than a help.