5

I am making an app that connect between iPhone.
It use enum specify UInt32. but sizeof() is different size.
Why not equal a UInt32?

enum Message:UInt32{
    case A = 1
    case B = 2
    case C = 3
}

sizeof(UInt32)    // 4
sizeof(Message)   // 1
kuninori
  • 51
  • 3

2 Answers2

0

They must be optimising the storage, the following works:

sizeofValue(Message.A.toRaw()) // 4
Howard Lovatt
  • 968
  • 1
  • 8
  • 15
  • Changing third member to `case C = 32767` makes no difference to `sizeof(Message)`. – Grimxn Jul 30 '14 at 10:43
  • I wouldn't expect it to. My guess is that Message is a pointer into an array of types. In the case of short examples there are less than 256 types in use, therefore Message is 8 bit. The values A, B, and C are not stored in the Message constant they are stored in the Type Message. – Howard Lovatt Jul 31 '14 at 00:30
  • I see what you mean now you elaborate! – Grimxn Jul 31 '14 at 06:58
  • "enum Message:UInt32{...}", this to become UInt32, not type Message but values A,B and C ?. – kuninori Aug 12 '14 at 06:21
  • @kuninory The type of message isn't UInt32 it is Message and despite what the syntax implies (which I find misleading) it is not derived from UInt32 (Message isNotA UInt32). However the enumerations inside Message, A, B, and C, are UInt32s. Therefore Message doesn't have to have a size of 4, but the enumerations A,B, and C, do have to have a size of 4. – Howard Lovatt Aug 15 '14 at 09:14
0

When calling an instance of an enum object with no additional method, it returns the type of enum it is (UInt32, in this case).

In reference to this answer, Types in objective-c on iPhone , an Unsigned Int is 32 4 bytes.

Community
  • 1
  • 1
Jonah Katz
  • 5,230
  • 16
  • 67
  • 90