解析字符串时出现问题

时间:2011-03-10 15:46:28

标签: objective-c cocoa-touch ios ipad

我试图在一系列图像的每一个上放置一行对话框。 为了使对话框线与正确的图像匹配,我使用正斜杠(/)后跟一个数字来结束每一行以识别匹配的图像。然后我解析每一行以获得对话框,然后解析图像的参考编号。 这一切都很好,除了当我将对话框行放入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类型分配'struct NSArray *',期望'struct NSMutableArray *'

我不太明白这一点,并试图改变类型,但无济于事。

非常感谢这里的一些建议。

2 个答案:

答案 0 :(得分:0)

警告告诉您问题究竟是什么。 -componentsSeparatedByString:返回NSArray的不可变实例,但您将该结果分配给NSMutableArray类型的变量。因此,您需要将变量更改为NSArray(在这种情况下您无法修改它)或制作组件数组的可变副本(通过-mutableCopy,您必须-release-autorelease保持平衡以避免内存泄漏。)

答案 1 :(得分:0)

正斜杠字符是转义字符,因此您不应将其用作分隔符。这可能导致字符串处理中的随机错误。选择其他内容,最好是!123!

之类的任意字符串

您收到警告,因为componentsSeparatedByString:返回NSArray而不是NSMutableArray,并且您将静态数组分配给可变数组指针。而是使用:

self.dialogSections=[NSMutableArray arrayWithArray:[dialogLineDetails componentsSeparatedByString: @"/"]];