1

Can someone show me how to initialize an NSMutableOrderedSet?

I tried the following

NSMutableOrderedSet *answerSet = [NSMutableOrderedSet orderedSet];

but hang there an the log is just (lldb)

will insert object Answer

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


@implementation Answer

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

@end

EDIT 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);

        }

and now I got exception below when saving to core data:

   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;
    })
    )}.'
Mark Thien
  • 1,108
  • 2
  • 22
  • 35
  • 1
    The way you are creating it is okay. The problem is elsewhere. Some __relevant__ code would be useful. – Abizern Jun 25 '13 at 15:20
  • can you show the exact line... if not sure mark breakpoint and check.. – Anoop Vaidya Jun 25 '13 at 15:26
  • If you haven't done it already. update to the latest Xcode (Version 4.6.3). For me OS X Update 10.8.4 introduced some problems with lldb that look similar to yours. Apps were just hanging at a line of code that was a hundred percent correct. – Matthias Bauch Jun 25 '13 at 17:18
  • If you're getting the `(lldb)` prompt, something went wrong. Type `bt` in the debug console (after the `(lldb)` prompt) and press return. Then copy the output from the log and paste it into your question. – rob mayoff Jun 26 '13 at 04:09
  • format the code, that i pasted in question...plz. Then i will look into. – Anoop Vaidya Jun 27 '13 at 12:52

1 Answers1

1

You can do like this:

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

And reading this will be helpful to you.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 1
    Not necessarily. the +orderedSet method in NSOrderedSet has the note: "This method is declared primarily for the use of mutable subclasses of NSOrderedSet." – Abizern Jun 25 '13 at 15:21
  • it still hang there with log message : (lldb) – Mark Thien Jun 27 '13 at 12:42
  • if I changed to : `NSMutableSet *answerSet = [[NSMutableSet alloc] initWithCapacity:4];` then it's working fine. – Mark Thien Jun 27 '13 at 14:52