嵌套方法调用或使用一次性变量时的最佳做法是什么?
你永远不应该使用一次性变量吗?
例子:
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:[NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata"]]
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]
error:&error];
您是否应该始终将嵌套方法分解为一次性变量?
例子:
NSNumber *yesNumber = [NSNumber numberWithBool:YES];
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:yesNumber
forKey:NSMigratePersistentStoresAutomaticallyOption];
NSString *urlPath = [applicationSupportDirectory stringByAppendingPathComponent:@"storedata"];
NSURL *url = [NSURL fileURLWithPath: urlPath];
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:url
options:optionsDict
error:&error];
还是应该结合使用两者?
例子:
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent:@"storedata"]];
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:url
options:optionsDict
error:&error];
我倾向于将两者结合起来,但我想听听其他人对此有何看法。如果上面没有说清楚,这些“一次性变量”的创建只是为了打破嵌套方法,不会在任何地方使用。
最佳答案
我个人认为您应该使用“一次性”变量,因为它 doesn't come with any overhead (在“发布”模式下,即启用优化)并且它使调试更容易。当然,除非您有特定的理由不这样做。
关于objective-c - 嵌套方法调用与一次性变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401704/