1

有人可以告诉我如何初始化 NSMutableOrderedSet 吗?

我尝试了以下

NSMutableOrderedSet *answerSet = [NSMutableOrderedSet orderedSet];

但挂在那里日志只是(lldb)

将插入对象答案

#import "Answer.h"
#import "Survey.h"


@implementation Answer

@dynamic count;
@dynamic text;
@dynamic survey;

@end

编辑1:

  #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>

    @interface Survey : NSManagedObject

    @property (nonatomic, retain) NSDate * dateCreated;
    @property (nonatomic, retain) NSString * id;
    @property (nonatomic, retain) NSString * note;
    @property (nonatomic, retain) NSString * question;
    @property (nonatomic, retain) NSMutableOrderedSet *answers;
    @end

    @interface Survey (CoreDataGeneratedAccessors)

    - (void)addAnswersObject:(NSManagedObject *)value;
    - (void)removeAnswersObject:(NSManagedObject *)value;
    - (void)addAnswers:(NSMutableOrderedSet *)values;
    - (void)removeAnswers:(NSMutableOrderedSet *)values;

    @end


    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>

    @class Survey;

    @interface Answer : NSManagedObject

    @property (nonatomic, retain) NSNumber * count;
    @property (nonatomic, retain) NSString * text;
    @property (nonatomic, retain) Survey *survey;

    @end


        NSMutableOrderedSet *answerSet = [[NSMutableOrderedSet alloc] initWithCapacity:4];

        Answer *ans1 = [NSEntityDescription insertNewObjectForEntityForName:@"Answer"inManagedObjectContext:managedObjectContext];
        ans1.text = trimedAns1;
        ans1.count = 0;
        ans1.survey = survey;

        Answer *ans2 = [NSEntityDescription insertNewObjectForEntityForName:@"Answer"inManagedObjectContext:self.managedObjectContext];
        ans2.text = trimedAns2;
        ans2.count = 0;
        ans2.survey = survey;

        [answerSet addObject:ans1];
        [answerSet addObject:ans2];

       [survey setAnswers:answerSet];

        NSError *error = nil;

        if (![managedObjectContext save:&error]) {

            NSLog(@"Can't Save! %@ %@", error, [error userInfo]);

        } else {

            NSLog(@"Saved question : %@", self.question.text);

        }

现在保存到核心数据时出现以下异常:

   2013-06-27 22:58:33.955 Simple Survey[718:11903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "answers"; desired type = NSSet; given type = __NSOrderedSetM; value = {(
        <Answer: 0x8168070> (entity: Answer; id: 0x81c8880 <x-coredata:///Answer/t0F1038D5-DD97-4066-AB7D-E92386A9010B3> ; data: {
        count = nil;
        survey = "0x81be560 <x-coredata:///Survey/t0F1038D5-DD97-4066-AB7D-E92386A9010B2>";
        text = 6;
    }),
        <Answer: 0x74ec7f0> (entity: Answer; id: 0x74eed40 <x-coredata:///Answer/t0F1038D5-DD97-4066-AB7D-E92386A9010B4> ; data: {
        count = nil;
        survey = "0x81be560 <x-coredata:///Survey/t0F1038D5-DD97-4066-AB7D-E92386A9010B2>";
        text = 7;
    })
    )}.'
4

1 回答 1

1

你可以这样做:

NSMutableOrderedSet *answerSet = [[NSMutableOrderedSet alloc] initWithCapacity:100];

阅读本文将对您有所帮助。

于 2013-06-25T15:20:54.317 回答