2

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?

MByD
  • 135,866
  • 28
  • 264
  • 277
n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

8

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

klaustopher
  • 6,702
  • 1
  • 22
  • 25
0

The suggested way to deal with these notifications is to not check for if(state == UIDocumentStateInConflict) but with a logical AND like this:

if (state & UIDocumentStateInConflict) {
    // do something...
}

see "An Example: Letting the User Pick the Version" in "Document-based app programming guide" from the official docs

auco
  • 9,329
  • 4
  • 47
  • 54