Problem with parsing strings
I am trying to put a line of dialog on each of a series of images. To match the dialog line with the correct image, I end each line with a forward slash (/) followed by a number to identify the matching image. I then parse each line to get the dialog and then the reference number for the image. It all works fine except that when I put the dialog line into a textView I get the whole line in the textView instead of the dialog part. What is confusing is that the console seems to indicate that the parsing of the dialog line has been carried out correctly.
Here开发者_运维技巧 are the details of my coding:
@interface DialogSequence_1ViewController : UIViewController {
IBOutlet UIImageView *theImage;
IBOutlet UITextView *fullDialog;
IBOutlet UITextView *selectedDialog;
IBOutlet UIButton *test_1;
IBOutlet UIButton *test_2;
IBOutlet UIButton *test_3;
NSArray *arrayLines;
IBOutlet UISlider *readingSpeed;
NSArray *cartoonViews;
NSMutableString *dialog;
NSMutableArray *dialogLineSections;
int lNum;
}
@property (retain,nonatomic) UITextView *fullDialog;
@property (retain,nonatomic) UITextView *selectedDialog;
@property (retain,nonatomic) UIButton *test_1;
@property (retain,nonatomic) UIButton *test_2;
@property (retain,nonatomic) UIButton *test_3;
@property (retain,nonatomic) NSArray *arrayLines;
@property (retain,nonatomic) NSMutableString *dialog;
@property (retain,nonatomic) NSMutableArray *dialogLineSections;
@property (retain,nonatomic) UIImageView *theImage;
@property (retain,nonatomic) UISlider *readingSpeed;
-(IBAction)start:(id)sender;
-(IBAction)counter:(id)sender;
-(IBAction)runNextLine:(id)sender;
@end
@implementation DialogSequence_1ViewController
@synthesize fullDialog;
@synthesize selectedDialog;
@synthesize test_1;
@synthesize test_2;
@synthesize test_3;
@synthesize arrayLines;
@synthesize dialog;
@synthesize theImage;
@synthesize readingSpeed;
@synthesize dialogLineSections;
-(IBAction)runNextLine:(id)sender{
//Get dialog line to display from the arrayLines array
NSMutableString *dialogLineDetails;
dialogLineDetails =[arrayLines objectAtIndex:lNum];
NSLog(@"dialogLineDetails = %@",dialogLineDetails);
//Parse the dialog line
dialogLineSections = [dialogLineDetails componentsSeparatedByString: @"/"];
selectedDialog.text =[dialogLineSections objectAtIndex: 0];
NSLog(@"Dialog part of line = %@",[dialogLineSections objectAtIndex: 0]);
NSMutableString *imageBit;
imageBit = [dialogLineSections objectAtIndex: 1];
NSLog(@"Image code = %@",imageBit);
//Select right image
int im = [imageBit intValue];
NSLog(@"imageChoiceInteger = %i",im);
//------more code
}
I get a warning on the line:
dialogLineSections = [dialogLineDetails componentsSeparatedByString: @"/"];
warning: incompatible Objective-C types assigning 'struct NSArray *', expected 'struct NSMutableArray *'
I don't quite understand this and have tried to change the types but to no avail.
Would be grateful for some advice here.
The warning tells you exactly what the problem is. -componentsSeparatedByString:
returns an immutable instance of NSArray
, but you're assigning that result to a variable of type NSMutableArray
. So you need to either change the variable to NSArray
(in which case you can't modify it) or make a mutable copy of the components array (via -mutableCopy
, which you must balance with -release
or -autorelease
to avoid memory leaks.)
The forward slash character is an escape character so you shouldn't use it as a delimiter. This can lead to random errors in string processing. Pick something else, preferably an arbitrary string like !123!
You are getting the warning because componentsSeparatedByString:
returns a NSArray instead of an NSMutableArray and you are assigning the static array to a mutable array pointer. Instead use:
self.dialogSections=[NSMutableArray arrayWithArray:[dialogLineDetails componentsSeparatedByString: @"/"]];
精彩评论