-1

I think I would like to pack a section index and a row index together into an int and assign the result to the tag property of a UIView. I thought I could do it like this but it doesn’t work:

typedef struct
{
    int16_t section;
    int16_t row;
} SecRow;

SecRow sr = {2,3};
UIView* aview = [[UIView alloc] init];
[aview setTag:sr];//Error - Sending ‘SecRow’ to parameter of incompatible type ’NSInteger’ (aka ‘int’)
or
[aview setTag:(int32_t)sr];//Error - Operand of type ‘SecRow’ where arithmetic or pointer type is required

I realize this would limit maximum values for section and row but I think 16 bit should be enough. In the past I would simply multiply section by 1000 or 10000 and add it to row, but I’d like to come up with the least limiting way of doing this. I would also like to avoid manipulating bit fields if I can.

How can I do this?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
David U
  • 943
  • 1
  • 8
  • 22
  • What are you doing ? You can't assign struct to int. You can typecast struct to int like that. So your previous approach was good in my opinion. Or subclass UIView and declare a `SecRow` property and assign the value to it. – Midhun MP Jan 28 '15 at 21:36
  • 1
    `*((int32_t *)&sr)` should do the trick. – Jean-Baptiste Yunès Jan 28 '15 at 21:39
  • I am working with a UICollectionView. Sometimes an event happens in a cell and I need to know who the cell's header is. I can assign the cell's row to its tag when I create the cell, but I'd like for the cell to know who its section is as well. – David U Jan 28 '15 at 21:39
  • 1
    The right way to do it is to use C shift and mask operations to combine your two values into a single int. It's very elementary C stuff which you should already know how to do. – Hot Licks Jan 28 '15 at 21:39
  • @Jean-BaptisteYunès -- You're assuming that the struct is packed. Is that the default for the Objective-C compiler?? – Hot Licks Jan 28 '15 at 21:40
  • See this for struct packing http://stackoverflow.com/questions/13688625/how-to-declare-packed-struct-without-padding-for-llvm – Jean-Baptiste Yunès Jan 28 '15 at 21:42
  • 1
    @Jean-BaptisteYunès - So apparently you need to specify `__attribute__((packed))`? I think using shift/mask is probably safer and more robust. – Hot Licks Jan 28 '15 at 21:52
  • Use a custom cell type (class) and add any additional properties that you need. Besides that, I don't get the problem because didSelectRowAtIndexPath, etc., all provide an NSIndexPath which includes row and section. – Marcus Adams Jan 28 '15 at 21:53
  • @HotLicks yes but you need to define some operators, to pack and unpack. I have no preference for one or the other. Both are valuable. – Jean-Baptiste Yunès Jan 29 '15 at 06:26

1 Answers1

1

You could use union:

typedef union {
  struct {
    int16_t section;
    int16_t row;
  } fields;
  int32_t bits;
} SecRow;

Then sr.bits. It does make your assignment a little longer:

SecRow sr = { .fields.section = 2, .fields.row = 3 };
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51