2012-12-29 39 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退回;) –