I am implementing state restoration in my app. The windows are restored fine, so now I am trying to encode certain properties of a view controller related to an index used to preserve the state of the view of the view controller. I have:
@property (nonatomic) int index; //prefer to use an int
@property (nonatomic) NSInteger *testint; //I tried with NSInteger as well and got the same error code
And I added the following to my view controller .m file (also tried with NSInteger, but I'm pasting the int code)
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.index forKey:@"self.index"];
[super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
self.index = [coder decodeObjectForKey:@"self.index"];
[super decodeRestorableStateWithCoder:coder];
}
Whether I use an int or an NSInteger, I get this same error message as soon as I type these out (can't build)
For
[coder encodeObject:self.index forKey:@"self.index"];
the error message is "Incompatible integet to pointer conversion sending 'int' to parameter of type id."
And for
self.index = [coder decodeObjectForKey:@"self.index"];
the error is "incompatible pointer to integer conversion assigning to 'int' from 'id'"
Clearly these errors are one of the same. But there must be a way to encode an int, and even if there isn't, why do I get these errors even when I use NSInteger test int and replace all the above with self.testint instead of self.index. In the latter case, I am using a pointer.
So my question is how DO I encode either an int (preferable) or an NSInteger? Thank you.
Were you really trying to use pointer for integer '*testint'. I think it is rather, 'testint'. And the following could be able to fix your error.
@property (nonatomic) int index; //prefer to use an int
@property (nonatomic) NSInteger testint; //I tried with NSInteger as well and got the same error code
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeInt:self.index forKey:@"self.index"];
[super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
self.index = [coder decodeIntForKey:@"self.index"];
[super decodeRestorableStateWithCoder:coder];
}