无法在iOS中分配表达式

时间:2013-09-23 22:14:31

标签: ios nsmutablearray

我有一个名为Letters的NSMutableArray,它包含一个UIImageView子类的实例。我试图循环遍历它们并为它们分配新值

 for (int i=0 ;i<26;i++)
{
    [letters objectAtIndex:i] = [[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]];
}

但这不起作用。 Xcode说“无法分配给表达式”,但是当我尝试在数组之外单独为每个数组项分配新值时,它可以工作。你知道为什么会这样吗?

编辑:没关系。对不起。我应该多看一下数组。

3 个答案:

答案 0 :(得分:1)

您正在寻找的方法是.-

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

for (int i=0 ;i<26;i+=1) {
    [letters replaceObjectAtIndex:i withObject:[[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]]];
}

答案 1 :(得分:0)

您需要使用replaceObjectAtIndex:withObject:,但您应该使用新的数组语法:

letters[i] = ...

答案 2 :(得分:0)

数组表示法有效:letters[i] = blah

但你可能应该使用快速枚举:

for(id letter in letters) { letter = blah; }