Recently I was guessing, how to work with flags in MailCore, where a flag is NSUInteger. I got answer from MailCore Librelist.
The flags are stored as bitwise operations. If you want to check to see if a message is deleted, for example, you can do the following bitwise operation:
1 2 3 |
if (coreMessage.flags & CTFlagDeleted) { NSLog(@"Message has been deleted."); } |
More explanation from me.
This is a list of Flags:
1 2 3 4 5 6 7 8 9 |
/** List of Message Flags **/ #define CTFlagNew MAIL_FLAG_NEW #define CTFlagSeen MAIL_FLAG_SEEN #define CTFlagFlagged MAIL_FLAG_FLAGGED #define CTFlagDeleted MAIL_FLAG_DELETED #define CTFlagAnswered MAIL_FLAG_ANSWERED #define CTFlagForwarded MAIL_FLAG_FORWARDED #define CTFlagCancelled MAIL_FLAG_CANCELLED |
Meaning of the right column here is:
1 2 3 4 5 6 7 8 9 |
enum { MAIL_FLAG_NEW = 1 << 0, MAIL_FLAG_SEEN = 1 << 1, MAIL_FLAG_FLAGGED = 1 << 2, MAIL_FLAG_DELETED = 1 << 3, MAIL_FLAG_ANSWERED = 1 << 4, MAIL_FLAG_FORWARDED = 1 << 5, MAIL_FLAG_CANCELLED = 1 << 6 }; |
Let’s use a Decimal to Binary converter.
For instance, 34 is 100010. We see, that 2nd and 6th bits are not null. So Flags are CTFlagSeen and CTFlagForwarded. Since CTFlagSeen = MAIL_FLAG_SEEN = 1 << 1 in binary. It means, that we should shift by 1 symbol and compare 2nd symbol in 100010. It is 1, or YES. The same is for CTFlagForwarded which is a shift by 6. If flags = 50, then binary is 110010. Flags are CTFlagSeen, CTFlagAnswered, CTFlagForwarded. Let's now assume, that we want to remove CTFlagSeen flag. What should we do? If we do this: flags = flags^CTFlagSeen we will get this in bit level: (110010)^(1<<1)=(110000), so the flag will be removed. If there was no flag, it will be installed, when we use XOR operator, represented by ^ symbol. If 18, then 10010. Flags are CTFlagSeen (10) and CTFlagAnswered(101).