I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this?
If you are certain that your NSInteger
is greater than or equal to zero, you simply cast it as NSUInteger
. You should not be casting to NSUInteger
if your NSInteger
represents a negative number, as it may lead to inconsistency.
NSInteger signedInteger = -30;
NSUInteger unsignedInteger = (NSUInteger)signedInteger;
results in
if (unsignedInteger == signedInteger) {
// is TRUE
}
if (signedInteger == 4294967266) {
// is FALSE
}
if (signedInteger == -30) {
// is TRUE
}
if (unsignedInteger == 4294967266) {
// is TRUE
}
if (unsignedInteger == -30) {
// is TRUE
}