Objective c 解析字符串时出现问题
我试图在一系列图像中的每一幅上都放置一行对话。 为了使对话框行与正确的图像匹配,我在每行末尾加上一个正斜杠(/)和一个数字来标识匹配的图像。然后我解析每一行以获得对话框,然后是图像的参考号。 这一切都很好,只是当我将对话框行放入textView时,我会在textView中得到整行,而不是对话框部分。 令人困惑的是,控制台似乎指示对话框行的解析已正确执行 以下是我的编码细节:Objective c 解析字符串时出现问题,objective-c,cocoa-touch,ios,ipad,Objective C,Cocoa Touch,Ios,Ipad,我试图在一系列图像中的每一幅上都放置一行对话。 为了使对话框行与正确的图像匹配,我在每行末尾加上一个正斜杠(/)和一个数字来标识匹配的图像。然后我解析每一行以获得对话框,然后是图像的参考号。 这一切都很好,只是当我将对话框行放入textView时,我会在textView中得到整行,而不是对话框部分。 令人困惑的是,控制台似乎指示对话框行的解析已正确执行 以下是我的编码细节: @interface DialogSequence_1ViewController : UIViewController
@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: @"/"];
警告:分配“struct NSArray*”的Objective-C类型不兼容,应为“struct NSMutableArray*”
我不太明白这一点,并试图改变类型,但没有任何效果
如果您能提供一些建议,我们将不胜感激。警告会准确地告诉您问题所在
-componentsSeparatedByString:
返回NSArray
的一个不可变实例,但您正在将该结果分配给类型为NSMutableArray
的变量。因此,您需要将变量更改为NSArray
(在这种情况下,您不能修改它),或者创建组件数组的可变副本(通过-mutableCopy
,必须与-release
或-autorelease
平衡以避免内存泄漏)正斜杠字符是转义字符,因此不应将其用作分隔符。这可能导致字符串处理中出现随机错误。选择其他内容,最好是像这样的任意字符串!123!代码>
您之所以会收到警告,是因为componentsSeparatedByString:
返回一个NSArray而不是NSMutableArray,并且您正在将静态数组分配给一个可变数组指针。而是使用:
self.dialogSections=[NSMutableArray arrayWithArray:[dialogLineDetails componentsSeparatedByString: @"/"]];
欢迎来到南非!提示:使用对话框按钮“代码”使您的代码看起来像代码(我用这种方式编辑了您的q)。您需要确保对类属性(例如,self.dialogLineSections)使用self.
访问器,因为否则您没有保留管理,属性中的对象可能会在没有警告的情况下消失。不需要可变副本。只要用返回的静态数组的内容创建mutableArray就足够了。没有理由在周围放置字符串对象的副本。-mutableCopy
是浅拷贝;它不会复制字符串,只是将它们作为新数组的初始值设定项的一部分保留。在NSArray中为True,但并不总是如此。协议不强制对象副本的深度。如果你养成了在实际上不需要拷贝时使用mutableCopy
的习惯,你会发现自己拥有一个没有任何警告的深度拷贝。当然,我对这样的事情很偏执。是的,但是,谁在乎呢?这同样适用于-initWithArray:
。这两种方法都不需要进行浅层复制,但在格式良好的程序中,您不需要担心这些事情。