1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSMutableString appendWhat?

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

  1. macrumors newbie

    #1
    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:

    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'){
    			break;
    		}
    		if(toProcess > 'Z'){
    			break;
    		}
    		
    		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 :)

    Ben
     
  2. macrumors 6502

    #2
    You never initialized toReturn, so its contents are just random bits, which happen to be a pointer to an NSButton instance.
     
  3. macrumors newbie

    #3
    Ah, there we go! Thanks very much kpua. It all works now :)
     

Share This Page