我想从核心数据实体中获取记录,并希望输入其他记录。
我有两个核心数据实体Category和Product
我也建立了他们的关系。
我已经完成了类别部分。
现在我想针对所选类别添加产品。
因为我使用UIPICKERVIEW
来显示类别,然后在产品中存储名称。
这是NSObject子类Product.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Category;
@interface Product : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * is_active;
@property (nonatomic, retain) NSString * descript;
@property (nonatomic, retain) Category *category;
@end
和.m文件
#import "Product.h"
#import "Category.h"
@implementation Product
@dynamic name;
@dynamic is_active;
@dynamic descript;
@dynamic category;
@end
我想在哪里展示选择器。
IMSAddProductViewController.h
#import <UIKit/UIKit.h>
@interface IMSAddProductViewController : UIViewController < UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *categoryPicker;
@property (weak, nonatomic) IBOutlet UITextField *txtEnterName;
@property (weak, nonatomic) IBOutlet UITextField *txtEnterDescription;
@property (weak, nonatomic) IBOutlet UISwitch *txtIsActive;
@property(nonatomic,retain)NSArray *arr;
@property (readonly, strong, nonatomic) NSMutableArray *categoryArray;
- (IBAction)btnSave:(id)sender;
@end
我不知道在此代码中应该编辑什么来显示选择器记录..
IMSAddProductViewController.m
#import "IMSAddProductViewController.h"
#import "IMSAppDelegate.h"
#import "Product.h"
#import "Category.h"
@interface IMSAddProductViewController ()
{
NSManagedObjectContext *context;
}
@end
@implementation IMSAddProductViewController
@synthesize arr;
@synthesize txtIsActive;
@synthesize categoryArray;
@synthesize txtEnterName;
@synthesize txtEnterDescription;
- (void)viewDidLoad
{
[super viewDidLoad];
IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];
NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Category"];
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = [NSArray arrayWithObject:[[category propertiesByName] objectForKey:@"arr"]];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
if (results == nil) {
//error handle here
}
[self setArr:results];
NSLog (@"name: %@",categoryArray);
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
// return _countryNames.count;
return arr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
//return _countryNames[row];
return arr[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
categoryArray = [self.arr objectAtIndex:row];
}
当我进入此视图时,它会抛出异常
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
' * - [__ NSPlaceholderArray initWithObjects:count:]:尝试插入nil对象 来自对象[0]'
答案 0 :(得分:0)
根据official documentation。,您需要将resultType
的{{1}}设置为NSFetchRequest
。
在NSDictionaryResultType
之后的viewDidLoad
中加入此内容:
request.propertiesToFetch
另外,替换
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
带
[self setArr:results];