1. #import <Foundation/Foundation.h> 2. 3. int main(void) 4. { 5. 6. NSAutoreleasePool* pool = [NSAutoreleasePool new]; 7. 8. typedef struct 9. { 10. char* name; 11. int hiredate; 12. }Emp; 13. 14. Emp emps[] = {{"Mouhammad", 2010}, {"Moutaz", 2010}, {"Ahmad", 2010}}; 15. 16. NSData* data = [NSData dataWithBytes : emps length: sizeof(Emp) * 3 ]; // here we specify the number of BYTES to be holded 17. 18. Emp* newEmps = [data bytes]; 19. int count = [data length]/ sizeof(Emp); // here we go the number of bytes holded, 20. //watch the division of the total number of bytes 21. // over the size of each struct element 22. 23. int i=0; 24. for (; i< count; i++) 25. { 26. Emp e = *newEmps; 27. NSLog(@"%s %i", e.name, e.hiredate); 28. newEmps++; 29. } 30. 31. 32. [pool release]; 33. return 0; 34. }
13 September 2011
NSData wraps array of bytes (8bits) so beware when use it
SA,
Objc NSData object wraps array of bytes so you should send the size parameter properly..
suppose you want to wrap an array of a custom struct Emp.. you have to specify the size of the Array as number of bytes as following:
sizeof(StructType)* array_length
See the following example carefully:
Labels:
objective-c
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment