0

Looking at the Error Handling Programming Guide, I understand that in cases like the following I must never evaluate the NSError parameter theError unless the return value is NO

BOOL success = [myDoc writeToURL:[self docURL] ofType:@"html" error:&theError];

What happens in casses when the return value is not a success BOOL, and instead it is a collection object?

Take for example [NSIncrementalStore obtainPermanentIDsForObjects:error]. I must not evaluate an error using the error parameter. What should I look for? I imagine I could receive either a nil pointer or an empty array. In this case an empty array does not seem a good response, although in other functions an empty array might be a good one. I can read that if an error occurs, the error object will contain a description of what happened, but it is not so clear how can I find out that an error actually happened.

What is the normal Cocoa or Foundation way to indicate an error in cases like these?

SystematicFrank
  • 16,555
  • 7
  • 56
  • 102

1 Answers1

1

For that method, you're expected to return an array of as many items as you had objects passed in. I'd say that if you don't do that (either the array is nil or empty), you're in an error condition, since you're violating what the documentation says will happen:

An array containing the object IDs for the objects in array.

The returned array must return the object IDs in the same order as the objects appear in array

Community
  • 1
  • 1
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
  • Thanks! that is what I am actually doing in this case. When I wrote my question, I was being more generic and I was looking beyond that case. I wanted to know if there is such a thing as a global policy. I am just curious about what Cocoa normally does. Empty array or nil array pointer? The docs give a hint about what can be an error, but are not so precise for annyoing people like me who dislike writing one single extra line of code out of non-knowledge (looking for two error conditions when only one can happen) :-P – SystematicFrank Nov 14 '12 at 10:05
  • I don't think there's an actual policy. You can, of course, check for both nil and empty at once by doing `if (array.count==0)`, since messaging nil will return nil. – Amy Worrall Nov 14 '12 at 10:15
  • There is a global policy that `nil` indicates and error when a method returns an object. Checking the array's count in the general case is inadvisable since there are many methods where an empty array indicates success. I believe all such methods are clearly documented to do so. – Mike Abdullah Nov 15 '12 at 00:19