objective-c - 嵌套方法调用与一次性变量

标签 objective-c coding-style

嵌套方法调用或使用一次性变量时的最佳做法是什么?

你永远不应该使用一次性变量吗?

例子:

[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/

相关文章:

iphone - 在 UICollectionView 中设置边框

objective-c - 仅当对象不在数组中时才将对象添加到数组中

c++ - 浮点运算的编程风格

ios - Coredata 获取托管对象,而对象的其他实例正在编辑导致崩溃

ios - 在 iOS 应用程序中使用具有不同目标的不同 Assets.xcassets?

php - 如何与多个项目共享 Symfony2 模型

c# - 检查字符串是否包含列表(字符串)中的元素

android - 在 Android 中使用默认字体样式

java - 如何在 Java 中正确转换泛型集合?

ios - 更改方向后如何更改选择器 View 的位置