Objective c NSArray错误:“;!“表达式不可赋值”;

Objective c NSArray错误:“;!“表达式不可赋值”;,objective-c,xcode,core-data,Objective C,Xcode,Core Data,我已经创建了一个NSArray,我已经初始化它以包含核心数据实体的属性。当我稍后尝试通过数组设置这些属性时,我得到错误“!Expression is not assignable”。如何通过NSArray引用属性值来设置属性值?以下是我的代码的相关部分: headingArray = [[NSArray alloc] initWithObjects: heading1, heading2, h

我已经创建了一个NSArray,我已经初始化它以包含核心数据实体的属性。当我稍后尝试通过数组设置这些属性时,我得到错误“!Expression is not assignable”。如何通过NSArray引用属性值来设置属性值?以下是我的代码的相关部分:

    headingArray = [[NSArray alloc] initWithObjects:
                heading1,
                heading2,
                heading3, ...
头1,头2。。。是核心数据实体的属性

    for (int i=1; [note.headingCount intValue]-1; i++) {
    [note.headingArray objectAtIndex:i] = selectedTemplateForNote.heading1;
  }

[note.headingArray objectAtIndex:i]是标记有错误“!表达式不可赋值”的代码。

NSArray
是不可变的。如果不构造新数组,则无法更改其内容。尝试使用
NSMutableArray
-(void)replaceObjectAtIndex:(nsInteger)索引with object:(id)anObject取而代之

根据新信息编辑:

您可能正在尝试执行以下操作:

NSManagedObject *entity = ...;
for (int i=1; [note.headingCount intValue]-1; i++) {
    [entity setValue:selectedTemplateForNote.heading1 forKey:[note.headingArray objectAtIndex:i]];
}

NSArray
是不可变的。如果不构造新数组,则无法更改其内容。尝试使用
NSMutableArray
-(void)replaceObjectAtIndex:(nsInteger)索引with object:(id)anObject取而代之

根据新信息编辑:

您可能正在尝试执行以下操作:

NSManagedObject *entity = ...;
for (int i=1; [note.headingCount intValue]-1; i++) {
    [entity setValue:selectedTemplateForNote.heading1 forKey:[note.headingArray objectAtIndex:i]];
}

在Objective-C中,不能这样分配值。您需要执行以下操作:

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:note.headingArray];
for (int i=1; [note.headingCount intValue]-1; i++) {
    [tempArray replaceObjectAtIndex:i withObject:selectedTemplateForNote.heading1];
}
[note setHeadingArray:[NSArray arrayWithArray:tempArray]];

在Objective-C中,不能这样分配值。您需要执行以下操作:

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:note.headingArray];
for (int i=1; [note.headingCount intValue]-1; i++) {
    [tempArray replaceObjectAtIndex:i withObject:selectedTemplateForNote.heading1];
}
[note setHeadingArray:[NSArray arrayWithArray:tempArray]];

选择什么类型的模板作为便笺标题1?请确保,这是一个NSValue

选择什么类型的模板作为便笺标题1?请确保,这是一个NSValue

我应该进一步解释,我试图使用数组来引用核心数据实体属性。我不希望数组包含属性的副本。我想做的是读取和写入核心数据实体属性,但要通过数组中包含的引用(指针)来执行。通过这种方式,我可以使用for循环对整个属性进行索引。抄送:@diatrevolo,@JensUpdated的回答试图预测你真正想要做的事情。这是朝着正确的方向发展的。进一步解释,“note”属于“Notes”类型,它是自动生成的NSManagedObject子类(选择Xcode菜单->编辑器选择->创建NSManagedObject子类)。使用您的代码,我将使用“[note setValue:selectedTemplateForNote.heading1 forKey:[note.headingArray objectAtIndex:I]];”。我认为这会起作用,但是对于我创建的子类,我认为应该有一种更简单的方法来收缩语句,隐藏核心数据键/值存储语法。关于如何做到这一点的任何进一步的评论。不是立即的。另一种可能是使用
-[NSKeyValueCoding setValueForKeysWithDictionary:(NSDictionary*)dict]编辑器选择->创建NSManagedObject子类)。使用您的代码,我将使用“[note setValue:selectedTemplateForNote.heading1 forKey:[note.headingArray objectAtIndex:I]];”。我认为这会起作用,但是对于我创建的子类,我认为应该有一种更简单的方法来收缩语句,隐藏核心数据键/值存储语法。关于如何做到这一点的任何进一步的评论。不是立即的。另一种可能是使用
-[NSKeyValueCoding setValueForKeysWithDictionary:(NSDictionary*)dict]