2012-12-29 42 views
-1

我正在使用單例類來跨視圖傳輸NSMutableArray。防止在NSMutableArray中重複打印輸出元素

我遇到的問題是我的數組顯示多次。

現在我正在處理三個UITextFields。每個都被添加到陣列並以特定格式輸出。這裏是我的輸出如下:

A/B

Ç

A/B

Ç

A/B

Ç

所有我需要顯示的是:

A/B

Ç

這裏是我的代碼,如果有人能幫助我找到了丟失或需要返工:

要顯示的文字:

UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 200)]; 
    [myLabel setFont:[UIFont fontWithName:@"Helvetica" size:10.0]]; 
    myLabel.numberOfLines = 0; 

    NSMutableString * string = [NSMutableString string]; 

    Education * myEducation = [Education sharedEducation]; 

    for (Education * output in myEducation.educationOutputArray) 
    { 
     [string appendString:[NSString stringWithFormat:@"%@/%@ \n%@\n", [myEducation.educationOutputArray objectAtIndex:0], [myEducation.educationOutputArray objectAtIndex:1], [myEducation.educationOutputArray objectAtIndex:2], nil]]; 

    } 

    myLabel.text = string; 

    [self.view addSubview:myLabel]; 

這裏是我正在保存的文本:

-(void)saveButtonTapped:(id)sender 
{ 
    [_educationArray addObject:_aTextField.text]; 
    [_educationArray addObject:_bTextField.text]; 
    [_educationArray addObject:_cTextField.text]; 

    Education * myEducation = [Education sharedEducation]; 
    myEducation.educationOutputArray = _educationArray; 

    [self.navigationController popViewControllerAnimated:YES]; 
} 

編輯*

當用戶在文本字段中輸入文本時,他們還可以使用相同的三個文本字段添加一組新文本。

如果我刪除了for循環,只顯示第一個。我怎樣才能讓所有的文字顯示?

編輯兩個*

我嘗試這樣做:

[string appendString:[NSString stringWithFormat:@"%@/%@ \n%@\n", [output major], [output university], [output timeAtSchool]]]; 

導致此錯誤:

2012-12-29 17:03:07.928 EasyTable[14501:c07] -[__NSCFString major]: unrecognized selector sent to instance 0x7176850 
2012-12-29 17:03:07.941 EasyTable[14501:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString major]: unrecognized selector sent to instance 0x7176850' 
*** First throw call stack: 
(0x1c99012 0x10d6e7e 0x1d244bd 0x1c88bbc 0x1c8894e 0x6162 0xff817 0xff882 0xffb2a 0x116ef5 0x116fdb 0x117286 0x117381 0x117eab 0x1184a3 0x118098 0x2c10 0x10ea705 0x21920 0x218b8 0xe2671 0xe2bcf 0xe1d38 0x2e5213 0x1c61afe 0x1c61a3d 0x1c3f7c2 0x1c3ef44 0x1c3ee1b 0x1bf37e3 0x1bf3668 0x1e65c 0x240d 0x2335 0x1) 
libc++abi.dylib: terminate called throwing an exception 
+0

它可能有更多的關於如何將對象添加到數組而不是如何打印,你可以添加代碼嗎? – wattson12

+0

如果可以,應用程序的屏幕截圖可能會有幫助。你可能會讓自己太難過了。 –

回答

0

一次向陣列中添加三個元素,稍後您要一次處理三個元素。

創建容納三個元素的容器類,並將容器類的實例添加到數組中可能會更好。

但是,如果你真的想這樣做,你不能使用for/in循環。你必須使用索引變量:

NSArray *array = myEducation.educationOutputArray; 
for (NSUInteger i = 0, count = array.count; i < count; i += 3) { 
    [string appendFormat:@"%@/%@\n%@\n", array[i], array[i+1], array[i+2]]; 
} 
0

您不必在數組中重複:問題是在對迴圈

for (Education * output in myEducation.educationOutputArray) 
{ 
    [string appendString:[NSString stringWithFormat:@"%@/%@ \n%@\n", [myEducation.educationOutputArray objectAtIndex:0], [myEducation.educationOutputArray objectAtIndex:1], [myEducation.educationOutputArray objectAtIndex:2], nil]]; 

} 

在這裏,您正在循環數組3次(因爲數組中有3個對象),然後每次都打印每個對象。我認爲你應該只是做appendString:行沒有for循環

+0

問題在於我允許用戶添加多個字段。所以如果我這樣做,只有第一個得到顯示 –

0

您的for循環真的很奇怪;你循環訪問數組中的每個對象,但是你在for循環中硬編碼索引。我不確定那裏發生了什麼,但它確實是奇數輸出的來源。指向Education對象的指針是您應該訪問的內容。每次通過循環時,使用值output

如果您也有唯一性問題,請考慮使用NSSet而不是NSMutableArray。如果您關心訂單,請使用NSOrderedSet。設置集合類型不允許重複值。

還有一點:如果您知道您只會訪問索引0,1和2處的值,那麼確實沒有要求輸入NSArray。只需分別存儲每個值並直接訪問它。至少沒有for循環的理由。

編輯:

你崩潰,因爲你認爲你的陣中有Education對象,但它確實具有普通字符串:

-[__NSCFString major]: unrecognized selector sent to instance 0x7176850

注意,它說你試着撥打方法major__NSCFStringNSString的私人子類,蘋果在內部使用)。這是一個很好的指標,表明你的數組不包含你的想法。確保通過用三部分數據構建Education對象,將數據正確加載到陣列中。

也建議你嘗試

[string appendString:[NSString stringWithFormat:@"%@/%@ \n%@\n", output]];

基本上什麼stringWithFormat:確實是採取格式字符串和使用它像一個模板,然後將它作爲你需要填寫佔位符在模板中需要儘可能多的參數。 %@是一個對象描述的佔位符,因此stringWithFormat:將在格式字符串後面預期三個參數以填充每個%@

+0

他增加了相同的東西,他沒有意外的欺騙 –

+0

我不認爲這可以保證一票否決,但沒關係。我在我的回答中覆蓋了重複和循環問題。 –

+0

當我這樣做,你沒有覆蓋for循環;)+你現在重新調整其餘的..把-1退回;) –