解析字符串时出现问题
我正试图在一系列图像上添加一行对话框。 为了使对话线与正确的图像相匹配,我用正斜杠(/)结尾每行,后面跟着一个数字以标识匹配的图像。然后我解析每一行以获取对话框,然后解析图像的参考号。 这一切工作正常,除了当我把对话框行到一个textView我在textView而不是对话框部分整个行。 令人困惑的是控制台似乎表明对话行的解析已经正确执行。解析字符串时出现问题
这里是我的编码的细节:
@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
}
我上线警告:
dialogLineSections = [dialogLineDetails componentsSeparatedByString: @"/"];
警告:不兼容的Objective-C类型分配 '结构的NSArray *',预计' struct NSMutableArray *'
我不太明白这一点,并试图改变类型,但无济于事。
感谢您的建议。
警告告诉你到底是什么问题。 -componentsSeparatedByString:
返回NSArray
的不可变实例,但您将该结果分配给类型为NSMutableArray
的变量。因此,您需要将变量更改为NSArray
(在这种情况下,您不能修改它)或制作组件阵列的可变副本(通过-mutableCopy
,您必须必须与-release
或-autorelease
平衡以避免内存泄漏。)
不需要mutableCopy。简单地用返回的静态数组的内容创建mutableArray就足够了。没有理由将重复的字符串对象放在周围。 – TechZen 2011-03-10 16:50:49
'-mutableCopy'是一个浅拷贝;它不会复制字符串,只是将它们保留为新数组初始化程序的一部分。 – 2011-03-10 17:27:52
在这种情况下是NSArray,但并非总是如此。该协议不会强制执行对象副本的深度。如果您在实际上不需要副本时习惯性地使用'mutableCopy',则可以发现自己的副本没有任何警告。当然,我对这样的事情有些偏执。 – TechZen 2011-03-10 17:45:30
正斜杠字符是一个转义字符,因此您不应将其用作分隔符。这可能会导致字符串处理中的随机错误。挑选别的东西,最好是任意字符串,如!123!
由于componentsSeparatedByString:
返回一个NSArray而不是NSMutableArray,并且您将静态数组分配给一个可变数组指针,您将收到警告。相反使用:
self.dialogSections=[NSMutableArray arrayWithArray:[dialogLineDetails componentsSeparatedByString: @"/"]];
欢迎在SA!一个提示:使用对话框按钮“代码”,让你的代码看起来像代码(我编辑你的q。)。 – Abel 2011-03-10 15:47:49
顺便提一下,控制台读数是: – 2011-03-10 15:50:42
您需要确保使用'self.'访问器来获取类属性,例如'self.dialogLineSections'因为否则你没有保留管理,属性中的对象可能会在没有警告的情况下消失。 – TechZen 2011-03-10 16:52:39