NSMutableString, NSMutableAray, scrambled
NSMutableString, NSMutableAray, scrambled
- Subject: NSMutableString, NSMutableAray, scrambled
- From: Michel Haver <email@hidden>
- Date: Wed, 11 Jul 2001 00:58:02 +0200
My Next Attempt
Thanks Art Isbell and John C. Randolph for the help.
I adjust the program to use mutable string variables as instance variable in
the button action.
I add a scrambledPlayStringArray as NSMutableArray to test the
insertObject:atIndex.
The RandomStringTester application looks like this now:
Content of Controller.h file:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet id textField;
// NSMutableString * inString = [NSMutableString string ] ;
// NSMutableString * tempString = [NSMutableString string];
// NSMutableString * outString = [NSMutableString string];
NSMutableString * inString ;
NSMutableString * tempString ;
NSMutableString * testString ;
NSMutableString * outString ;
NSMutableArray * scrambledPlayStringArray;
int currentLength;
NSRange oneCharRange;
}
- (IBAction)buttonAction:(id)sender;
@end
Content of Controller.m file:
#import "Controller.h"
#import <Cocoa/Cocoa.h>
@implementation Controller
- (IBAction)buttonAction:(id)sender
{
scrambledPlayStringArray = [[ NSMutableArray alloc ] initWithCapacity: 10 ];
srand(time(0));
[ scrambledPlayStringArray insertObject: @"sunshineandcocoa" atIndex: 0
];
// use instring as input for tempString
// tempString = inString;
tempString = [ scrambledPlayStringArray objectAtIndex: 0] ;
NSLog(@"String temp: %@.\n", tempString);
currentLength = [tempString length];
while( currentLength )
{
oneCharRange = NSMakeRange(rand() % currentLength,1);
[outString appendString:[tempString
substringWithRange:oneCharRange]];
[tempString deleteCharactersInRange:oneCharRange];
currentLength--;
[ scrambledPlayStringArray insertObject: outString atIndex: 1 ];
}
NSLog(@"Modified the strings...\n");
NSLog(@"String temp 2: %@.\n", tempString);
NSLog(@"String out: %@.\n", outString);
testString = [ scrambledPlayStringArray objectAtIndex: 1] ;
NSLog(@"String test: %@.\n", testString);
[textField setStringValue:outString];
}
@end
---------------
The application RandomStringTester gets compiled. But if you push the button
you get the error message:
Jul 11 00:23:57 RandomStringTester[868] String temp: sunshineandcocoa.
Jul 11 00:23:57 RandomStringTester[868] *** -[NSConstantString
deleteCharactersInRange:]: selector not recognized
Jul 11 00:23:57 RandomStringTester[868] *** -[NSConstantString
deleteCharactersInRange:]: selector not recognized
Using the declaration in @interface Controller in Controller.h
// NSMutableString * inString = [NSMutableString stringWithString:
@"nagelwaus"];
// NSMutableString * tempString = [NSMutableString string];
// NSMutableString * outString = [NSMutableString string];
results in the (now) know parse errors, but the NSMutableString string
inside these declaration seems necessary to let the while loop do it is
work. Right?
The question is how can the while loop construction do its work with the use
of
@interface Controller:NSObject
{
IBOutlet id textField;
NSMutableString *inString;
NSMutableString *tempString;
NSMutableString *outString;
}
Or is the problem assigning a NSMutableString from a NSMutableArray?
tempString = [ scrambledPlayStringArray objectAtIndex: 0] ;
Regards,
Michel