在核心数据编程指南中的以下代码示例中,创建了 NSFetchRequest
使用 autorelease,而 NSSortDescriptor 不是使用 autorelease 创建的。为什么 NSSortDescriptor 不使用 autorelease 创建?这是一个偏好问题吗?
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee"
inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
// Set example predicate and sort orderings...
NSNumber *minimumSalary = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName LIKE[c]'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName"
ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil){
// Deal with error...
}
当你自动释放时,你基本上是在说:“我不再需要这个了,但其他人可以自由地拿起它(在自动释放池耗尽之前)”。当您显式释放一个对象时,您是在说:“我不再需要这个对象,除非其他人已经另有说明(已获取),否则应立即释放它。”
因此,自动释放通常并不是错误的事情。这是required当您想要将对象传递回消息的发送者而不要求发送者负责释放该对象时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)