显示关系1:M的核心数据实体记录 [英] Display Core Data entity records with relationship 1:M
问题描述
我在显示来自我的实体Product和OrderItems的记录时遇到大问题。这是一对多关系。
I am having big issues displaying records from my entity Product and OrderItems. This is one to Many relationships.
我向Product实体添加了记录而没有任何问题,我在以下函数中执行OrderItem条目:
I added records to Product entity without any problem and I perform OrderItem entry in the following function:
- (IBAction)cmdSave:(id)sender {
self.product.productName = self.txtProductName.text;
NSManagedObjectContext *context =[self managedObjectContext];
OrderItems *oi=
[NSEntityDescription
insertNewObjectForEntityForName:@"OrderItems" inManagedObjectContext:context];
[oi setValue:[NSNumber numberWithInteger:[self.txtQty.text integerValue]] forKey:@"ordQty"];
[oi setValue:[NSNumber numberWithInteger:[self.txtPrice.text integerValue]] forKey:@"price"];
[oi setValue:[NSNumber numberWithInteger:[self.txtTotal.text integerValue]] forKey:@"total"];
[oi setOrders:self.product];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.navigationController popViewControllerAnimated:YES];
}
这似乎没问题,我最大的问题是显示每个产品的所有订单商品。产品====> OrderItems的关系是:产品和逆是订单。 OrderItems ===>产品是:订单和逆是产品。
This seems ok and my biggest issue is display all order items for each Product. The relationship Product ====> OrderItems is:product and the inverse is orders. OrderItems ===>Product is: orders and inverse is product.
我尝试显示fetchedResult
I try to display for fetchedResult
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Product *prod = [self.fetchedResultsController objectAtIndexPath:indexPath];
*********************
//I wanted to connect to relationship object and get all orders but I get Incompatible pointer types initializing 'OrderItems *_Strong ' with an expression of type NSSet.
******any ideas?
OrderItems *order=prod.product;
**************************************
cell.textLabel.text =prod.productName;
cell.detailTextLabel.text =[NSString stringWithFormat:@"%@",order.price];
return cell;
}
我知道Product中产品中的关系对象是NSSet如下所示:
I know the relationship object in product in Product is NSSet as below:
@interface Product : NSManagedObject
@property (nonatomic, retain) NSString * productName;
@property (nonatomic, retain) NSNumber * qty;
@property (nonatomic, retain) NSSet *product;
@end
@interface Product (CoreDataGeneratedAccessors)
- (void)addProductObject:(NSManagedObject *)value;
- (void)removeProductObject:(NSManagedObject *)value;
- (void)addProduct:(NSSet *)values;
- (void)removeProduct:(NSSet *)values;
@end
我在不同的程序中也采用了相同的方式,但我不知道为什么。
I have done the same way in different program but I don't know really why.
非常感谢任何帮助或建议!
Any help or advice would be much appreciated!
推荐答案
不兼容的指针类型使用NSSet类型的表达式初始化'OrderItems * _Strong'。
Incompatible pointer types initializing 'OrderItems *_Strong ' with an expression of type NSSet.
这非常简单。你几乎似乎知道。问题是:
It's pretty straightforward. You almost seem to know. The problem is:
Product *prod = [self.fetchedResultsController objectAtIndexPath:indexPath];
OrderItems *order=prod.product;
您正在为类型 OrderItems $的指针赋值c $ c>,你指定的东西是
prod.product
。但那是什么?它是 NSSet
:
You're assigning a value to a pointer of type OrderItems
, and the thing you're assigning is prod.product
. But what's that? It's an NSSet
:
@property (nonatomic, retain) NSSet *product;
这就是你得到那条消息的原因。你有一个指向 NSSet
的指针,你试图将它指向一个指向 OrderItems
的指针。我不知道 OrderItems
是什么,但它不是 NSSet
。你可以做出这个任务,但是你不能指望它正常工作。
That's why you get that message. You have a pointer to NSSet
and you're trying to assign it to a pointer to OrderItems
. I don't know what OrderItems
is, but it's not an NSSet
. You can make that assignment, but you can't expect it to work properly.
我在不同的程序中做了同样的事情,但我不知道为什么。
I have done the same way in different program but I don't know really why.
要么你在其他应用程序中做了不同的事情,要么你在那里得到的信息是相同的来到这里。
Either you did things differently in other apps, or you got the same message there that you're getting here.
这篇关于显示关系1:M的核心数据实体记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!