0

有两个文件。让我们打电话给fileOne他们fileTwo

每个都有几个具有相同名称的 NSMutableDictionary 属性。列举几个:

@property (retain, nonatomic) NSMutableDictionary * lunchStartTimeObject;
@property (retain, nonatomic) NSMutableDictionary * lunchLocationNameObject;
@property (retain, nonatomic) NSMutableDictionary * lunchLocationAddressObject;
@property (retain, nonatomic) NSMutableDictionary * activity1NameObject;
@property (retain, nonatomic) NSMutableDictionary * activity1StartTimeObject;
@property (retain, nonatomic) NSMutableDictionary * activity1LocationNameObject;
@property (retain, nonatomic) NSMutableDictionary * activity1CommentsFieldObject;
@property (retain, nonatomic) NSMutableDictionary * activity1LocationAddressObject;
@property (retain, nonatomic) NSMutableDictionary * activity2NameObject;
@property (retain, nonatomic) NSMutableDictionary * activity2StartTimeObject;
@property (retain, nonatomic) NSMutableDictionary * activity2LocationNameObject;
@property (retain, nonatomic) NSMutableDictionary * activity2CommentsFieldObject;
@property (retain, nonatomic) NSMutableDictionary * activity2LocationAddressObject;

我想通过调用下面的方法(或类似的方法)来比较两个文件中同名的字典:

-(NSMutableDictionary *)cellColorForChanges:(NSMutableDictionary *)newdictionary :(NSMutableDictionary *)oldDictionary;
{
    if(![newdictionary isEqualToDictionary:oldDictionary])
    {
        [newdictionary  setValue:@"UIColor yellowColor" forKey:@"cellColor"];
    }
    return newdictionary;
}

我试图避免手动为每个 NSMutableDictionary 编写代码。有没有办法避免以下情况:

if(![fileOne.lunchStartTimeObject isEqualToDictionary:fileTwo.lunchStartTimeObject])
{  
    fileOne.lunchStartTimeObject setValue:@"UIColor yellowColor" forKey:@"cellColor"]; 
}

我无法找出完成上述任务的最有效方法。是否有可能将每个字典发送到一个方法并取回字典(如果它不相等,则用另一个键更新)?或者我试图避免的是不可避免的?

4

1 回答 1

0

您遇到了这个问题,因为您没有充分抽象您的对象。在我看来,您上面的财产清单是荒谬的多余。即使是您提供的简单代码也难以阅读。

尝试以更具概念性的方式思考您的问题。尝试考虑可以封装您正在寻找的功能的对象。

查看您的属性,您可能想要这样的课程

@interface Activity : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSDate *startTime;
@property (nonatomic, strong) Location *location;

-(BOOL)isEqualToActivity:(Activity*)activity;

@end

也许您需要一个位置类来存储有关位置的更多信息;也许您可以使用更多字段,例如 firstName、lastName;也许你需要一个type(枚举或字符串)属性来说明它是什么类型的活动,等等——你明白了。

如前所述,您可以编写自己的比较方法,您可以在其中调整以允许或多或少严格的大写或小写字母、数字格式等。

于 2013-10-14T00:02:42.987 回答