1

I know in C int cannot be nil, which brings me to ask this question. I am using a rails API that uses an int for a certain property (rating). This API call could return nothing for the rating because it doesn't have a rating yet. I am using RestKit to map the results of the API call.

Maybe I should convert the int passed back from server API call to an NSNumber instead of an int? Or can I check in int is nil some how?

2
  • You tagged "Objective-C" and you say you use Rails. Am I missing something? Also, I believe Obj-C is defined as a "strict superset of C". Commented Nov 10, 2015 at 15:09
  • 3
    int cannot be nil in Obj-C. Only object types (pointer) can be nil (null). If you want to have a way to handle nil, you need to use objects (e.g. NSNumber). This is what most parsing frameworks do. Commented Nov 10, 2015 at 15:10

1 Answer 1

4

You should use NSNumber, which is an object and can therefore be nil. It can wrap integers, floating points and booleans:

NSNumber *number = nil;
if (!number) {
    NSLog(@"Number not assigned");
}

number = @1;
if (number) {
    NSLog(@"Number assigned: %d", [number intValue]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.