View Full Version : NSMutableString appendWhat?

Jun 22, 2008, 10:41 AM
Hi all,

I'm new to XCode and Objective-C, coming from Eclipse and programming in Java.
After deciding to learn Obj-C, I thought I could test my knowledge by reprogramming some of my assignments. One such exersise was to create a caesar cipher (take a string, shift all the characters by a defined number).

I have some code which gets an NSString, and a number. I have then managed to convert the NSString to uppercase, pick out a char and shift that char.
Problems occur when I try to amend an NSMutableString.

Here's some of my code:

/*set up variables*/
int myShift = shift + 1;
NSString *myText = text;
NSMutableString *toReturn;
unichar toProcess;

myText = [text uppercaseString]; //make UPPERCASE

for (int i = 0; i < [myText length]; i++) {
toProcess = [myText characterAtIndex:i];

if(toProcess < 'A'){
if(toProcess > 'Z'){

toProcess = toProcess + myShift;

if(toProcess > 'Z'){
toProcess = toProcess - 26;

NSLog(@"%C", toProcess); //This works!

[toReturn appendFormat:@"%C", toProcess]; //This doesn't

That last line I got from this (http://forums.macrumors.com/showthread.php?t=118753) thread, but it doesn't seem to work. I get this message:
2008-06-22 16:25:00.213 Encryption[1803:813] *** -[NSButton appendFormat:]: unrecognized selector sent to instance 0x12caf0

Thanks for any help :)


Jun 22, 2008, 11:00 AM
You never initialized toReturn, so its contents are just random bits, which happen to be a pointer to an NSButton instance.

Jun 22, 2008, 11:14 AM
Ah, there we go! Thanks very much kpua. It all works now :)