Search code examples
iphoneobjective-cios5iclouduidocument

How to interpret document.state == 12 (iCloud)


Whenever I load a UIDocument from iCloud, I check its state like so:

NSLog(@"Library loadFromContents: state = %d", self.documentState);

In some cases I have received documentState 8 or 12 which have caused crashes. I am now wondering what exactly the 8 and 12 stands for. As far as I'm aware, documentState is a bit field, so it has many different flags. The docs reveal that:

enum {
UIDocumentStateNormal          = 0,
UIDocumentStateClosed          = 1 << 0,
UIDocumentStateInConflict      = 1 << 1,
UIDocumentStateSavingError     = 1 << 2,
UIDocumentStateEditingDisabled = 1 << 3   }; 
typedef NSInteger UIDocumentState;

However, I have no idea how to interpret this in my situation. How do I find out what 8 and 12 stand for?


Solution

  • Inside the enum they do some bit-shifting. They could have also written it like this:

    enum {
    UIDocumentStateNormal          = 0,
    UIDocumentStateClosed          = 1,
    UIDocumentStateInConflict      = 2,
    UIDocumentStateSavingError     = 4,
    UIDocumentStateEditingDisabled = 8   }; 
    typedef NSInteger UIDocumentState;
    

    A bit shift to the left is basically 2 to the power of whatever number is given after the shift operator... 1<<1 means 2^1, 1<<2 means 2^2, etc ...

    A state of 8 means UIDocumentStateEditingDisabled and 12 means UIDocumentStateEditingDisabled and UIDocumentStateSavingError