0

我想从核心数据实体中获取记录,并希望针对该实体输入其他记录。

我有两个核心数据实体类别和产品

我也建立了他们的关系。

我已经完成了类别部分。

现在我想针对所选类别添加产品。

为此,我使用 aUIPICKERVIEW来显示类别,然后将名称存储在产品中。

这是 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;

@结尾

我不知道在这段代码中应该编辑什么来显示选择器记录..

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:]: 尝试从 objects[0] 插入 nil 对象'

4

1 回答 1

0

根据官方文档. ,您需要将 的设置resultType为. 将其包含在after中:NSFetchRequestNSDictionaryResultType
viewDidLoadrequest.propertiesToFetch

request.returnsDistinctResults = YES;   
request.resultType = NSDictionaryResultType;

另外,更换

[self setArr:results];

NSArray* results2 = [resultsvalueForKeyPath:@"arra"];
[self setArr:results2];
于 2013-09-27T15:46:35.547 回答