开发者

Compress this into a function?

Hi I have the following code which is really long of course, how can I compress this into a function? Then call this function with the other weekdays and lessons in the week? Currently Im just using the same code over and over just replacing the variables (which are strings).

//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开发者_JAVA百科]];
    [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"];


Try this:

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, ...);

...

You could probably condense it even more if your objects were in arrays instead of a constellation of discrete variables.


This kind of structured data is often easier to create and maintain in an external text or XML file, such as a plist. Here's a method that will read all the lesson plan info into an NSDictionary that contains nested sub dictionaries and arrays:

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

    NSLog(@"%@", lessonPlanDict);

    return lessonPlanDict;
}

The plist file could look something like this (if you want to use the text plist format; otherwise you can use the XML plist format):

{
    "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" = (
        );
    };
}

You can even use Xcode's spiffy graphical plist editor to create and modify the plist as necessary. If you need to modify the lesson plan strings. However, I'm still not sure how your app uses this data, so I'm not sure if the structure of the plist lends itself best to what your app needs to do.

EDIT

Since Marcelo's approach apparently was more along the lines of what you were looking for, let me suggest a slightly more streamlined implementation of his solution:

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];
}


Like the commenter above implied, I don't know what you are trying to achieve. This code is just refactored to minimize duplication, without any real insight into your problem-space.

-(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]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜