I made Sender class and sender object:
Sender *sender = [[Sender alloc] init];
Then, I wrote test:
// should success
[sender upload:[UIImage imageNamed:@"test2.jpg"] withName:@"test2.jpg"];
// should fail
[sender upload:nil withName:@"test2.jpg"];
Then, I wrote nil check code:
- (void)upload:(UIImage *)image withName:(NSString *)name
{
if ([image isEqual:nil]) {
...
}
...
}
But nil check is ignored.
How can I check whether the image parameter is nil or not?
Thank you for your kindness.
Short answer, replace
[image isEqual:nil]withimage == nil.Long answer, when you write:
you are asking for the message
isEqual:to be sent to the object whose reference is stored in the variableimage.However you don’t wish to send a message to an object, you wish to determine whether the variable
imagecontains a reference to an object or not. To do this you need to compare the value of your reference with the “no reference” value – which is writtennil. The value comparison operator in Objective-C (and C et al) is==so you need to test:Now you might ask why did the code you have execute without error, just not producing the result you expected, given that there is no object to send a message to? Objective-C supports sending messages to “no object”, aka
nil, and returns the “zero” value for the result type, i.e.0,niletc. When converted to a boolean value, as in yourifstatement, a “zero” value producesNO.HTH