【问题标题】:Problem with parsing strings解析字符串的问题
【发布时间】:2011-03-10 15:46:27
【问题描述】:

我正在尝试在一系列图像中的每一个上放置一行对话。 为了将对话行与正确的图像匹配,我以正斜杠 (/) 结束每一行,后跟一个数字来标识匹配的图像。然后我解析每一行以获取对话框,然后是图像的参考编号。 一切正常,除了当我将对话框行放入 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: @"/"];

警告:分配 'struct NSArray *' 的 Objective-C 类型不兼容,预期为 'struct NSMutableArray *'

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

在此不胜感激。

【问题讨论】:

  • 欢迎来到 SA!提示:使用对话框按钮“代码”使您的代码看起来像代码(我以这种方式编辑了您的 q)。
  • 您需要确保对类属性使用self. 访问器,例如self.dialogLineSections 因为否则您没有保留管理,并且属性中的对象可能会在没有警告的情况下消失。

标签: objective-c cocoa-touch ios ipad


【解决方案1】:

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

【讨论】:

  • 不需要 mutableCopy。只需使用返回的静态数组的内容创建 mutableArray 就足够了。没有理由在周围放置重复的字符串对象。
  • -mutableCopy 是浅拷贝;它不会复制字符串,只是将它们保留为新数组初始化程序的一部分。
  • 在这种 NSArray 的情况下为真,但并非总是如此。该协议不强制执行对象副本的深度。如果您习惯于在实际上不需要副本时使用mutableCopy,您会发现自己拥有一个没有任何警告的深层副本。当然,我对这些事情很偏执。
  • 是的,但是,谁在乎呢? -initWithArray: 也是如此。两者都不需要做浅拷贝,但在格式良好的程序中你不必担心这些事情。
【解决方案2】:

正斜杠字符是转义字符,因此您不应将其用作分隔符。这可能会导致字符串处理中的随机错误。选择别的东西,最好是像!123!这样的任意字符串

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

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

【讨论】:

    猜你喜欢
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多