Here are a few methods from some Apple classes:
- (NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID; - (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; + (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context
It is reasonable to expect that all of these methods will return subclasses of the return type. In this regard, the returned object is often assigned to a variable of the expected type, for example:
BCCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BCCustomTableViewCell cellIdentifier]];
This makes the compiler unhappy; casting solves this, but casting like this is dirty:
BCCustomTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:[BCCustomTableViewCell cellIdentifier]];
However, when the return type is id , this cast can be removed, albeit at the cost of some type safety, as a result of which my code becomes cleaner:
BCPerson *person = [NSEntityDescription insertNewObjectForEntityForName:BCPersonEntityName inManagedObjectContext:context];
Personally, I prefer when the return type is id . Is there any rationale that Apple used to choose one approach over another, or is it just because of the preference of the developer who wrote the methods?
source share