objective c - Store bytes into correct wrapper -


i developing app ios parses libcap formatted files. that, need implement different protocols appaear in files parse. problem don't know how store different headers read. first started using c types such uint8_t, uint16_t, etc. these encapsulated c structs.

for instance, here header of ethernet packet :

#define ether_addr_len 6 typedef struct ethernet_hdr_s {     uint8_t mac_dst[ether_addr_len];     uint8_t mac_src[ether_addr_len];     uint32_t tag;     uint16_t ethertype;     uint32_t crc; }   ethernet_hdr_t; 

then realized won't work since i'm going have many different sized header real mess work with. thought of using uint8_t , arrays. ethernet header transformed :

#define ether_addr_len 6 typedef struct ethernet_hdr_s {     uint8_t mac_dst[ether_addr_len];     uint8_t mac_src[ether_addr_len];     uint8_t tag[4];     uint8_t ethertype[2];     uint8_t crc[4]; }   ethernet_hdr_t; 

but problem classic c arrays, size unknown , it's not best solution in eye. besides, arc on, can't use objects in c structs.

so thought of using objects. why program in objective c, idea. header class looking :

@interface pcapheader : nsobject @property (readwrite, strong) nsdata *mac_dst; @property (readwrite, strong) nsdata *mac_src; @property (readwrite, strong) nsdata *tag; @property (readwrite, strong) nsdata *ethertype; @property (readwrite, strong) nsdata *crc; @end 

i want use similar format headers simplify code comes later.

but i'm new objective-c here have lot of questions. 1) okay use nsdata above, contain few bytes. feel it's not optimized @ all. 2) should use properties here or instance variables more appropriate ?

thank in advance.

you don't need use nsdata properties. use c types. c arrays, need store int of count of elements. common practice in c data structures , c functions. count can struct member or property in object. use nsvalue or nsnumber box c types objects can used in objective-c collections nsarray. optimizations, build first, determine if sufficiently optimized. objects scripting languages tend introduce overhead, waste of time optimize first. nsdata not heavy class. objective-c objects glorified structs themselves.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -