Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Adding multiple strings to a property NSMutableString

I have had difficulty with creating a method of NSMutableStrings and moving the value into a property. For example:

  • (NSMutableString *)setNames { NSMutableString *theNames = nil;

    [theNames appendString:Name_1]; [theNames appendString:Name_2]; [theNames appendString:Name_3];

    return theNames; } from other properties Name_1 = @"Bob" Name_2 = @"Sid" Name_3 = @"Pip"

place them like so: self.Names = [_Names setNames]; when I try to place them in a separate function I get the error No visible @interface for NSMutableString declares the selector

4 Answers

Chris McKnight
PLUS
Chris McKnight
Courses Plus Student 11,045 Points

You are sending the message appendString to nil. Change NSMutableString *theNames = nil; to NSMutableString *theNames = [[NSMutableString alloc] init];

- (NSMutableString *)setNames
{
    NSMutableString *theNames = [[NSMutableString alloc] init];
    [theNames appendString:Name_1];
    [theNames appendString:Name_2];
    [theNames appendString:Name_3];
    return theNames;
}

Chris McKnight thank you, but the solution posted didn't change the error.

Chris McKnight
Chris McKnight
Courses Plus Student 11,045 Points

Is _Names an instance of NSMutableString? I am not clear on what you are trying to accomplish other than appending 3 strings together.

I'm sorry I did not make that clear. Yes, I have a MutableString property 'names', which is to receive the names from the 3 strings.

the instance variable in which the property 'Names' should be stored. I don't know why it is causing the error

If your error is "No visible @interface for NSMutableString declares the selector", then it sounds like you're missing a -(NSMutableString *)setNames; declaration in your class's .h file.

Another thing you could try is calling setNames on self, rather than _Names, since setNames is a method of your object, not of NSMutableString. So instead of self.Names = [_Names setNames]; try self.Names = [self setNames];, which should return your NSMutableString theNames, and then set self.Names to it.

And Chris is right too, make sure you alloc and init your theNames object before calling appendString on it!

...And one other teeny thing is that the convention, as far as I can tell, is to use lowercase instance variable names (so "names", not "Names").

Hope this helps—good luck!

-csj

Chris McKnight
Chris McKnight
Courses Plus Student 11,045 Points

Great explanation. I had not gotten around to mentioning these.