NSMutableString appendWhat?

Discussion in 'Mac Programming' started by Forquare1, Jun 22, 2008.

  1. Forquare1 macrumors newbie

    Jun 22, 2008
    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 thread, but it doesn't seem to work. I get this message:
    Thanks for any help :)

  2. kpua macrumors 6502

    Jul 25, 2006
    You never initialized toReturn, so its contents are just random bits, which happen to be a pointer to an NSButton instance.
  3. Forquare1 thread starter macrumors newbie

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

Share This Page