2010-09-09 47 views
0

嗨我有以下代碼,當然真的很長,我如何將它壓縮到一個函數?然後在其他工作日和本週的課程中調用此函數?目前我只是使用相同的代碼反覆更換變量(即字符串)。將它壓縮成一個函數?

//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"]; 
+0

你能提供一點這個代碼在做什麼的概述?例如,傳遞給''stringWithFormat:'的對象的數據類型是什麼?爲什麼每個標籤都有兩個條目?我猜你在這裏可能比NSArray更適合使用NSDictionary,但我不確定是否完全理解代碼需要做什麼。 – jlehr 2010-09-09 14:31:19

+0

Apoliges,感謝您的幫助到目前爲止。基本上,每天上課有6節課,上課前,放學後,午餐時間和休息時間,以及兩週的時間表,所以有A周和B周。所以monP1A意味着星期一,第一節,第一週。我是一個使用stringWithFormat並使用放入一個nsstring,所以我可以編輯這些字符串。 – 2010-09-09 15:49:48

+0

這有一點幫助,但我仍然不清楚你的程序將如何使用這些數據;像這樣的東西:這是如何呈現在用戶界面?用戶是否可以修改訂單,添加或刪除數組中的項目,還是修復了這些項目?我仍然覺得可能有更方便的方法來模擬數據。 – jlehr 2010-09-09 19:28:41

回答

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

... 

你也許可以凝結,如果更你的對象是在數組,而不是離散變量的一個星座。

+0

2問題:你永遠不會聲明'arr'的類型。另外,由於您要返回帶+1保留計數的對象,您應該在返回之前重命名該函數以包含「create」或autorelease'arr'這個詞(以遵循適當的命名和內存管理規則)。 – 2010-09-09 16:08:18

+0

這很奇怪。也許「幫手」太常見了,而且與別的東西相沖突。嘗試一個不同的名字(抓着吸管)。 – 2010-09-09 16:28:37

+0

哈哈,現在不用擔心它的工作正常。原來我只是需要將函數放在monArrayA = helper ...代碼的上面。我的錯。謝謝,它的作品魅力! – 2010-09-09 16:43:55

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]]; 
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的漂亮圖形的plist編輯器創建和plist中作必要的修改。如果您需要修改課程計劃字符串。但是,我仍不確定您的應用程序如何使用此數據,因此我不確定plist的結構是否適合您的應用程序需要執行的操作。

編輯

由於馬塞洛的做法顯然是沿着你要找的線路多,讓我提出一個稍微精簡的執行情況的解決方案:

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]; 
} 
相關問題