2011-03-10 97 views
0

我正試圖在一系列圖像上添加一行對話框。 爲了使對話線與正確的圖像相匹配,我用正斜槓(/)結尾每行,後面跟着一個數字以標識匹配的圖像。然後我解析每一行以獲取對話框,然後解析圖像的參考號。 這一切工作正常,除了當我把對話框行到一個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 *'

我不太明白這一點,並試圖改變類型,但無濟於事。

感謝您的建議。

+2

歡迎在SA!一個提示:使用對話框按鈕「代碼」,讓你的代碼看起來像代碼(我編輯你的q。)。 – Abel 2011-03-10 15:47:49

+0

順便提一下,控制檯讀數是: – 2011-03-10 15:50:42

+0

您需要確保使用'self.'訪問器來獲取類屬性,例如'self.dialogLineSections'因爲否則你沒有保留管理,屬性中的對象可能會在沒有警告的情況下消失。 – TechZen 2011-03-10 16:52:39

回答

0

警告告訴你到底是什麼問題。 -componentsSeparatedByString:返回NSArray的不可變實例,但您將該結果分配給類型爲NSMutableArray的變量。因此,您需要將變量更改爲NSArray(在這種情況下,您不能修改它)或製作組件陣列的可變副本(通過-mutableCopy,您必須必須-release-autorelease平衡以避免內存泄漏。)

+0

不需要mutableCopy。簡單地用返回的靜態數組的內容創建mutableArray就足夠了。沒有理由將重複的字符串對象放在周圍。 – TechZen 2011-03-10 16:50:49

+0

'-mutableCopy'是一個淺拷貝;它不會複製字符串,只是將它們保留爲新數組初始化程序的一部分。 – 2011-03-10 17:27:52

+0

在這種情況下是NSArray,但並非總是如此。該協議不會強制執行對象副本的深度。如果您在實際上不需要副本時習慣性地使用'mutableCopy',則可以發現自己的副本沒有任何警告。當然,我對這樣的事情有些偏執。 – TechZen 2011-03-10 17:45:30

0

正斜槓字符是一個轉義字符,因此您不應將其用作分隔符。這可能會導致字符串處理中的隨機錯誤。挑選別的東西,最好是任意字符串,如!123!

由於componentsSeparatedByString:返回一個NSArray而不是NSMutableArray,並且您將靜態數組分配給一個可變數組指針,您將收到警告。相反使用:

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