0

I added Book object in bookController (NSCreeController). Now i want to get stored Book object when i select the row.

- (IBAction)addClicked:(id)sender {
    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    // NSTimeInterval is defined as double

    NSUInteger indexArr[] = {0,0};
    Book *obj = [[Book alloc] init];
    NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]                                                          dateStyle:NSDateFormatterNoStyle                                                          timeStyle:NSDateFormatterLongStyle];
    obj.title = [NSString stringWithFormat:@"New %@",dateString];
    obj.filename = [NSString stringWithFormat:@"%d",arc4random()%100000];
    [self.booksController insertObject:obj atArrangedObjectIndexPath:[NSIndexPath indexPathWithIndexes:indexArr length:2]];
}
A O
  • 5,516
  • 3
  • 33
  • 68
user869123
  • 257
  • 4
  • 15

1 Answers1

0

I concede there perhaps could be a better solution--

I am unfamiliar with how NSTreeController works, but I looked a the class reference and noticed that it has a content property, similar to an NSArrayController (Which I am familiar with grabbing specific objects from).
I believe that if the content property is actually of type of some kind of tree data structure, my answer here probably won't work. The class reference says this about content:

The value of this property can be an array of objects, or a single root object. The default value is nil. This property is observable using key-value observing.

So this is what I historically have done with the expected results:

NSString *predicateString = [NSString stringWithFormat:NEVER_TRANSLATE(@"(filename == %@) AND (title == %@)"), @"FILENAME_ARGUMENT_HERE", @"TITLE_ARGUMENT_HERE"];
NSArray *matchingObjects = [[self content] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:predicateString]];

Then simply calling -objectAtIndex: will grab you your object. Note that the NSArray will be empty if the object doesn't exist, and if you have duplicate objects, there will be multiple objects in the array.

I also searched for an answer to your question, and found this SO thread: Given model object, how to find index path in NSTreeController?

It looks pretty promising if my solution doesn't work, the author just steps through the tree and does an isEqual comparison.

If you could (if it's not too much trouble), leave a comment here to let me know what works for you, I'm actually curious :)

Community
  • 1
  • 1
A O
  • 5,516
  • 3
  • 33
  • 68