objective-c - 许多参数的最佳实践方法

标签 objective-c

我正在编写一个需要很多参数的注册方法。最初,我为我的方法选择了以下方法。

// APPROACH #1
- (void)signUpWithInformation:(NSDictionary *)information
                      success:(void (^)(NSDictionary *))success
                      failure:(void (^)(NSError *))failure
{
    // ...

    NSDictionary *parameters = @{@"email": information[@"email"],
                                 @"new_password": information[@"password"],
                                 @"user_name": information[@"username"],
                                 @"sex": ([information[@"sex"] isEqualToNumber:@(EFTUserSexMale)]) ? @"M" : @"F",
                                 @"phone_nubmer": information[@"phoneNumber"],
                                 @"weight": information[@"weight"],
                                 @"height": information[@"height"],
                                 @"birthday": [formatter stringFromDate:information[@"birthday"]]};

    // ...
}

但我觉得这种做法有些不适。尽管 information 给了我一些灵 active ,但它需要装箱和拆箱才能将值存储到字典中。

所以我想到了下面的方法。

// APPROACH #2
- (void)signUpWithEmail:(NSString *)email
               password:(NSString *)password
                   name:(NSString *)name
                    sex:(EFTUserSex)sex
            phoneNumber:(NSString *)phoneNumber
                 weight:(double)weight
                 height:(double)height
               birthday:(NSDate *)birthday
                success:(void (^)(NSDictionary *))success
                failure:(void (^)(NSError *))failure
{
    // ...

    NSDictionary *parameters = @{@"email": email,
                                 @"new_password": password,
                                 @"user_name": username,
                                 @"sex": (sex == EFTUserSexMale) ? @"M" : @"F"
                                 @"phone_nubmer": phoneNumber,
                                 @"weight": weight,
                                 @"height": height,
                                 @"birthday": [formatter stringFromDate:birthday]};

    // ...
}

因为objective-c有named parameter结构,我觉得这种方法看起来像字典,不需要装箱和拆箱。

但是,我无法决定哪种方法最好。也许还有其他好的方法?

最佳答案

将所有这些数据抽象到自定义模型类中会更容易。例如,您可以创建一个 ETFUser 类,它具有与字典中所有键相对应的属性。这是一个更好的面向对象设计(将类中的相似数据相关联,而不是将数据作为参数或字典中的值相关联),并且会让您在编译时检查所有提供的值都是正确的类型,这将所有内容放入字典时情况并非如此。使用字典还需要您记录所有要使用的正确键和类型,但使用模型类时,需要什么数据以及它应该是什么类型会立即显而易见。

因此您将拥有一个具有此接口(interface)的类 EFTUser:

@interface ETFUser : NSObject

@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSString *newPassword;
@property (nonatomic) EFTUserSex sex;
@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic, strong) NSNumber *weight;
@property (nonatomic, strong) NSNumber *height;
@property (nonatomic, strong) NSDate *birthday;

@end

现在,您可能仍然需要生成此数据的字典版本,以便作为正文参数传递给网络请求。在那种情况下,现在你有一个完美的地方来放置这段代码!将 - (NSDictionary *)dictionaryRepresentation 方法添加到您的新 EFTUser 类,该类将生成格式正确的字典以传递给网络请求。这可以防止该代码在您需要使用用户数据发出请求的任何地方被复制。

您示例中的方法现在可以简化为:

- (void)signUpWithUser:(EFTUser *)user
                  success:(void (^)(NSDictionary *))success
                  failure:(void (^)(NSError *))failure
{
    NSDictionary *parameters = [user dictionaryRepresentation];

    //...
}

关于objective-c - 许多参数的最佳实践方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476292/

相关文章:

objective-c - Objective C 类初始化错误

ios - PKInAppPaymentService 连接上的 ApplePay iOS 错误 - com.apple.passd.in-app-payment - 连接到远程警报 View 服务失败

ios - 显示对象的 X 坐标和 Y 坐标?

iphone - 在 settings.bundle 中指定不可编辑的字符串?

iphone - 如何在 iPhone 上扩展键盘渐变?

ios - 在类型 'allowIDFACollection' Google Analytics iOS 的对象上找不到属性 '__strong id'

objective-c - UINavigationController 保留

ios - XCode 性能测量

objective-c - 在启用沙箱的情况下在 OSX 10.10 上制作 App 默认处理程序

ios - 如何通过名称和标签获取 textField 的值 - objective c