把它压缩成一个函数?

时间:2010-09-09 14:23:12

标签: iphone objective-c function

嗨我有以下代码,当然很长,如何将其压缩成函数?然后在本周的其他工作日和课程中调用此功能?目前我只是一遍又一遍地使用相同的代码来替换变量(即字符串)。

//Allocate and add objects to monday week one.
    monArrayA = [[NSMutableArray alloc] init];
    [monArrayA addObject:@"Before School"];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP1A]];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP2A]];
    [monArrayA addObject:@"Break"];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP3A]];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP4A]];
    [monArrayA addObject:@"Lunch"];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP5A]];
    [monArrayA addObject:[NSString stringWithFormat:@"%@", monP6A]];
    [monArrayA addObject:@"After School"];

    //Allocate and add objects to monday week two.
    monArrayB = [[NSMutableArray alloc] init];
    [monArrayB addObject:@"Before School"];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP1B]];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP2B]];
    [monArrayB addObject:@"Break"];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP3B]];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP4B]];
    [monArrayB addObject:@"Lunch"];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP5B]];
    [monArrayB addObject:[NSString stringWithFormat:@"%@", monP6B]];
    [monArrayB addObject:@"After School"];

    //Allocate and add objects to tuesday week one.
    tueArrayA = [[NSMutableArray alloc] init];
    [tueArrayA addObject:@"Before School"];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP1A]];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP2A]];
    [tueArrayA addObject:@"Break"];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP3A]];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP4A]];
    [tueArrayA addObject:@"Lunch"];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP5A]];
    [tueArrayA addObject:[NSString stringWithFormat:@"%@", tueP6A]];
    [tueArrayA addObject:@"After School"];

    //Allocate and add objects to tuesday week two.
    tueArrayB = [[NSMutableArray alloc] init];
    [tueArrayB addObject:@"Before School"];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP1B]];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP2B]];
    [tueArrayB addObject:@"Break"];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP3B]];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP4B]];
    [tueArrayB addObject:@"Lunch"];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP5B]];
    [tueArrayB addObject:[NSString stringWithFormat:@"%@", tueP6B]];
    [tueArrayB addObject:@"After School"];

    //Allocate and add objects to wednesday week one.
    wedArrayA = [[NSMutableArray alloc] init];
    [wedArrayA addObject:@"Before School"];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP1A]];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP2A]];
    [wedArrayA addObject:@"Break"];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP3A]];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP4A]];
    [wedArrayA addObject:@"Lunch"];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP5A]];
    [wedArrayA addObject:[NSString stringWithFormat:@"%@", wedP6A]];
    [wedArrayA addObject:@"After School"];

    //Allocate and add objects to wednesday week two.
    wedArrayB = [[NSMutableArray alloc] init];
    [wedArrayB addObject:@"Before School"];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP1B]];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP2B]];
    [wedArrayB addObject:@"Break"];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP3B]];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP4B]];
    [wedArrayB addObject:@"Lunch"];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP5B]];
    [wedArrayB addObject:[NSString stringWithFormat:@"%@", wedP6B]];
    [wedArrayB addObject:@"After School"];

    //Allocate and add objects to thursday week one.
    thuArrayA = [[NSMutableArray alloc] init];
    [thuArrayA addObject:@"Before School"];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP1A]];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP2A]];
    [thuArrayA addObject:@"Break"];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP3A]];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP4A]];
    [thuArrayA addObject:@"Lunch"];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP5A]];
    [thuArrayA addObject:[NSString stringWithFormat:@"%@", thuP6A]];
    [thuArrayA addObject:@"After School"];

    //Allocate and add objects to thursday week two.
    thuArrayB = [[NSMutableArray alloc] init];
    [thuArrayB addObject:@"Before School"];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP1B]];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP2B]];
    [thuArrayB addObject:@"Break"];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP3B]];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP4B]];
    [thuArrayB addObject:@"Lunch"];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP5B]];
    [thuArrayB addObject:[NSString stringWithFormat:@"%@", thuP6B]];
    [thuArrayB addObject:@"After School"];

    //Allocate and add objects to friday week one.
    friArrayA = [[NSMutableArray alloc] init];
    [friArrayA addObject:@"Before School"];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP1A]];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP2A]];
    [friArrayA addObject:@"Break"];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP3A]];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP4A]];
    [friArrayA addObject:@"Lunch"];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP5A]];
    [friArrayA addObject:[NSString stringWithFormat:@"%@", friP6A]];
    [friArrayA addObject:@"After School"];

    //Allocate and add objects to friday week two.
    friArrayB = [[NSMutableArray alloc] init];
    [friArrayB addObject:@"Before School"];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP1B]];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP2B]];
    [friArrayB addObject:@"Break"];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP3B]];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP4B]];
    [friArrayB addObject:@"Lunch"];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP5B]];
    [friArrayB addObject:[NSString stringWithFormat:@"%@", friP6B]];
    [friArrayB addObject:@"After School"];

3 个答案:

答案 0 :(得分:2)

试试这个:

NSMutableArray* helper(id a, id b, id c, id d, id e, id f) {
    NSMutableArray* arr = [[NSMutableArray alloc] init];
    [arr addObject:@"Before School"];
    [arr addObject:[NSString stringWithFormat:@"%@", a]];
    [arr addObject:[NSString stringWithFormat:@"%@", b]];
    [arr addObject:@"Break"];
    [arr addObject:[NSString stringWithFormat:@"%@", c]];
    [arr addObject:[NSString stringWithFormat:@"%@", d]];
    [arr addObject:@"Lunch"];
    [arr addObject:[NSString stringWithFormat:@"%@", e]];
    [arr addObject:[NSString stringWithFormat:@"%@", f]];
    [arr addObject:@"After School"];
    return arr;
}

...

monArrayA = helper(monP1A, monP2A, monP3A, monP4A, monP5A, monP6A);
monArrayB = helper(monP1B, monP2B, monP3B, monP4B, monP5B, monP6B);

tueArrayA = helper(tueP1A, ...);
tueArrayB = helper(monP1B, ...);

...

如果你的对象是数组而不是一组离散变量,你可能会更加浓缩它。

答案 1 :(得分:2)

这种结构化数据通常更容易在外部文本或XML文件(如plist)中创建和维护。这是一个将所有课程计划信息读入包含嵌套子词典和数组的NSDictionary的方法:

- (NSDictionary *)lessonPlanDictionary
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LessonPlans" ofType:@"plist"];
    NSDictionary *lessonPlanDict = [NSDictionary dictionaryWithContentsOfFile:path];

    NSLog(@"%@", lessonPlanDict);

    return lessonPlanDict;
}

plist文件看起来像这样(如果你想使用文本plist格式;否则你可以使用XML plist格式):

{
    "Monday" = {
        "Before School" = (
            "Lesson 1",
            "Lesson 2"
        );
        "Break" = (
            "Lesson 3",
            "Lesson 4"
        );
        "Lunch" = (
            "Lesson 5",
            "Lesson 6"
        );
        "After School" = (
        );
    };
    "Tuesday" = {
        "Before School" = (
            "Lesson 1",
            "Lesson 2"
        );
        "Break" = (
            "Lesson 3",
            "Lesson 4"
        );
        "Lunch" = (
            "Lesson 5",
            "Lesson 6"
        );
        "After School" = (
        );
    };
    "Wednesday" = {
        "Before School" = (
            "Lesson 1",
            "Lesson 2"
        );
        "Break" = (
            "Lesson 3",
            "Lesson 4"
        );
        "Lunch" = (
            "Lesson 5",
            "Lesson 6"
        );
        "After School" = (
        );
    };
    "Thursday" = {
        "Before School" = (
            "Lesson 1",
            "Lesson 2"
        );
        "Break" = (
            "Lesson 3",
            "Lesson 4"
        );
        "Lunch" = (
            "Lesson 5",
            "Lesson 6"
        );
        "After School" = (
        );
    };
    "Friday" = {
        "Before School" = (
            "Lesson 1",
            "Lesson 2"
        );
        "Break" = (
            "Lesson 3",
            "Lesson 4"
        );
        "Lunch" = (
            "Lesson 5",
            "Lesson 6"
        );
        "After School" = (
        );
    };
}

您甚至可以根据需要使用Xcode的spiffy图形plist编辑器来创建和修改plist。如果需要修改课程计划字符串。但是,我仍然不确定你的应用程序如何使用这些数据,所以我不确定plist的结构是否最适合你的应用程序需要做的事情。

修改

由于Marcelo的方法显然更符合您的要求,所以让我建议稍微简化一下他的解决方案:

NSMutableArray *lessonPlans(NSString *lesson1, NSString *lesson2,
                            NSString *lesson3, NSString *lesson4,
                            NSString *lesson5, NSString *lesson6)
{
    return [NSMutableArray arrayWithObjects:
            @"Before School", lesson1, lesson2,
            @"Break",         lesson3, lesson4,
            @"Lunch",         lesson5, lesson6,
            @"After School",
            nil];
}

答案 2 :(得分:0)

就像上面提到的评论者一样,我不知道你想要达到的目标。此代码仅进行了重构,以最大限度地减少重复,而无需真正了解您的问题空间。

-(NSMutableArray *) arrayWithSchedule:(NSArray *) schedule
{
    NSMutableArray *toReturn = [[NSMutableArray alloc] init];
    [toReturn addObject:@"Before School"];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:0]]];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:1]]];
    [toReturn addObject:@"Break"];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:2]]];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:3]]];
    [toReturn addObject:@"Lunch"];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:4]]];
    [toReturn addObject:[NSString stringWithFormat:@"%@", [schedule objectAtIndex:5]]];
    [toReturn addObject:@"After School"];

    return toReturn;
}

monArrayA = [self arrayWithSchedule:[NSArray arrayWithObjects:monP1A, monP2A, monP3A, monP4A, monP5A, monP6A, nil]];
monArrayB = [self arrayWithSchedule:[NSArray arrayWithObjects:monP1B, monP2B, monP3B, monP4B, monP5B, monP6B, nil]];