问题
I am having big issues displaying records from my entity Product and OrderItems. This is one to Many relationships.
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];
}
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.
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;
}
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!
回答1:
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;
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;
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.
来源:https://stackoverflow.com/questions/20157803/display-core-data-entity-records-with-relationship-1m