-1

I am using a singleton class to transfer a NSMutableArray across views.

The issue I am having is my array is getting displayed multiple times.

Right now I am working with three UITextFields. Each is getting added to the array and outputted in a specific format. Here is what my output looks like:

A / B

C

A / B

C

A / B

C

All I need shown is:

A / B

C

Here is my code if someone can help me find what is missing or needs to be reworked:

To display the text:

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

Here is where I am saving the text:

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

EDIT *

When a user is entering text into the text fields they can also add a new set of text using the same three text fields.

If I removed the for loop only the first is displayed. How can I get all of the text to display?

EDIT TWO *

I tried this:

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

Results in this error:

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
4

3 回答 3

0

你的for循环真的很奇怪;您遍历数组中的每个对象,然后在 for 循环中硬编码索引。我不确定那里发生了什么,但它肯定是奇怪输出的来源。指向Education对象的指针是您应该访问的。每次通过循环,使用 的值output

如果您也遇到唯一性问题,请考虑使用 anNSSet而不是NSMutableArray. 如果您关心订单,请使用NSOrderedSet. 集合集合类型不允许重复值。

还有一点:如果您知道您只会访问索引 0、1 和 2 处的值,那么拥有NSArray. 只需单独存储每个值并直接访问它。至少没有理由 for 循环。

编辑:

你崩溃是因为你认为你的数组中有Education对象,但它确实有纯字符串:

-[__NSCFString major]:无法识别的选择器发送到实例 0x7176850

请注意,它是说您试图major在(该苹果内部使用__NSCFString的私有子类)上调用该方法。NSString这是一个很好的指标,表明您的数组不包含您认为的内容。Education通过使用这三个数据构建对象,确保您将数据正确加载到数组中。

您还建议您尝试过

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

基本上,stringWithFormat:所做的就是采用该格式字符串并将其用作模板,然后它需要尽可能多的参数来填充模板中的占位符。%@是对象描述的占位符,因此stringWithFormat:在格式字符串之后需要三个参数来填充每个%@.

于 2012-12-29T22:43:37.917 回答
0

您在数组中没有重复项:问题出在 for 循环中

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 循环的行

于 2012-12-29T22:43:07.760 回答
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]];
}
于 2012-12-29T23:12:15.103 回答