【问题标题】:How do I batch assign values to a property of objects returned by the fetched results controller?如何将值批量分配给 fetched results 控制器返回的对象的属性?
【发布时间】:2011-12-15 12:55:24
【问题描述】:

好的,所以我的数据库中存在一堆对象,除了两个之外,它们的所有字段都已完成...因为在创建对象时无法确定值。

现在我想在 payment received method 中插入代码,该代码会将相同的值(例如 $120.50)写入 fetchedResultsController 返回的所有对象的空白字段。

这是我之前创建新对象并设置属性值的方法。

newInvoice = [NSEntityDescription insertNewObjectForEntityForName:@"Invoice" inManagedObjectContext:managedObjectContext];


[newInvoice setValue:productTextField.text forKey:@"itemCode"]; 

对于 SQL 数据库中当前为空的以下属性,我应该使用什么代码来设置所有返回对象的值?

[newInvoice setValue:amountPaidLabel.text forKey:@"amountPaid"];
[newInvoice setValue:paymentMethod.text forKey:@"PaymentMethod"];

我希望这是有道理的,任何意见将不胜感激

【问题讨论】:

    标签: iphone objective-c object properties nsfetchedresultscontroller


    【解决方案1】:

    我会在您的发票类中添加一个方法...

    - (void)updateEmptyPaid:(NSNumber *)aValue {
      [self setValue:aValue forKey:@"amountPaid"];
    }
    

    ...对付款方式执行相同操作,然后您就可以...

    [myInvoices makeObjectsPerformSelector:@selector(updateEmptyPaid:) withObject:newValue];
    [myInvoices makeObjectsPerformSelector:@selector(updatePaymentMethod:) withObject:newValue];
    

    其中 myInvoices 是 NSFetchRequest 的结果数组。

    【讨论】:

    • 嘿,谢谢我已经尝试实现你的代码,但我一定做错了,我收到以下错误:[Invoice updatePaymentFields:]: unrecognized selector sent to instance 0x6b5af70 这是方法-(void)updatePaymentFields:(NSString*) aValue {NSLog(@"I'm inside Update Payment Fields");[self setValue:aValue forKey:@"amountPaid"]; 这是它被称为- (IBAction)saleCompleteButtonTapped:(id)sender { [mutableFetchResults makeObjectsPerformSelector:@selector(updatePaymentFields:) withObject:amountPaidStr]; } 的地方你知道我为什么会收到这个错误吗?
    • 我收到错误是因为 updatePaymentFields 在 Invoice 类中是完整的,就像你说的那样,我在我的 firstViewController 中有它
    猜你喜欢
    • 2019-05-27
    • 1970-01-01
    • 2015-10-14
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2010-11-15
    • 2014-04-05
    相关资源
    最近更新 更多