0

I have this string: "str is {( You, Me )}"

Looking on SO, I found this post and this one (among others)

I want to remove the leading {( and the trailing )}

Every solution so far has been met with an error:

-[__NSCFSet length]: unrecognized selector sent to instance

or a similar variant. I can print the value to the console and I can use the unfiltered "peopleExport" elsewhere in my app.

The code I've tried so far includes :

NSString *str = (NSString *)[managedObject valueForKeyPath:@"people.name"];

    NSLog(@"str is %@", str);

    //NSString *string = @"hello one two three";

    //NSString *newStr = [str substringFromIndex:3];
    str = [str substringWithRange:NSMakeRange(2, str.length-2)];


    NSString *newStr;

    if ( [str length] > 0){
        NSLog(@"a");
    newStr = [str substringFromIndex:2];
    } else {
        NSLog(@"b");
        newStr = str;
    }

    NSLog(@"%@", newStr);

    peopleExport = newStr;

The source is from a core data store. Does that matter?

I would appreciate any pointers.

Community
  • 1
  • 1
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • 2
    Are you sure it's a string coming out of coredata? It looks like you are trying to cast a NSSet to a NSString. – shawnwall Jan 30 '12 at 13:44
  • 1
    The contents of the NSSet will depend on your data model. Usually an NSSet will contain the entities at the end of a to-many relationship. What does your data model look like? – colincameron Jan 30 '12 at 13:52

1 Answers1

1

It looks to me like str is not actually an NSString, but is an NSSet, and what you're seeing is the result of the -description method being called on that set. If what you want is a string of the form "You, Me", try this:

NSSet *set = [managedObject valueForKeyPath:@"people.name"];
NSString *newString = [[set allObjects] componentsJoinedByString:@", "];
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • You guys are great.. That was it.. I will mark you all up, but I will give Wevah the answer as I used his code. Thank you all!! – ICL1901 Jan 30 '12 at 14:15