3

まず、Objective C は初めてです。

属性のペアを持つクラス Song があります。私のメインクラスでは、NSMutableArray である変数 allSongs を取得し、この配列にすべての曲オブジェクトを追加しました。[self.allSongs removeObject:OBJECT]; を呼び出そうとすると問題が発生します。デバッガーを使用すると、呼び出しの前に、リストが期待どおりに見えることがわかります。ただし、呼び出し後、対象のオブジェクトは削除されますが、配列の最初の要素も nil になります。これは一般的なポインターの問題ですか、それとも何ですか?

これが私のコードです:

hファイルで

@property (nonatomic, strong) NSMutableArray *songs;
@property (nonatomic, strong) NSMutableArray *allChapters;

mファイルで

- (void)viewDidLoad
{

self.chosenChapter = [[NSString alloc]initWithFormat:self.chosenChapter];
self.allChapters = [[NSMutableArray alloc]init];

//Chapter names and chapter page range
chapters = [[NSArray alloc]initWithObjects:@"chapter1", @"chapter2", @"chapter3", nil];
chaptersRange = [[NSArray alloc]initWithObjects:@"25", @"51", @"88", nil];
//Filnames of every song
files = [[NSMutableArray alloc] initWithObjects:@"test", @"Feta_fransyskor", nil];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
self.songs = [[NSMutableArray alloc]init];

for(int i = 0; i < chapters.count; i++){
    Song *chapter = [[Song alloc]init];
    [chapter setPage:(NSString *)self.chaptersRange[i]];
    [chapter setTitle:(NSString *)self.chapters[i]];
    [self.allChapters addObject:chapter];
    [self.songs addObject:chapter];
}

NSString *filePath;
int i;
for (i = 0; i < files.count; i++) {
    filePath = [[NSBundle mainBundle] pathForResource:files[i] ofType:@"txt"];
    if(filePath){
        NSError *error;
        NSString *textFromfile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: &error];
        /*
         index
         0 for page number
         1 for title name
         2 for melody name
         3 -> for lyrics
         */

        NSMutableArray *newLineseparatedText = (NSMutableArray *)[textFromfile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

        if(newLineseparatedText){
            Song *newSong = [[Song alloc]init];
            [newSong setPage:newLineseparatedText[0]];
            [newSong setTitle:newLineseparatedText[1]];
            [newSong setMelody:newLineseparatedText[2]];

            [newLineseparatedText removeObjectAtIndex:0]; //remove page number
            [newLineseparatedText removeObjectAtIndex:0]; //remove title name
            [newLineseparatedText removeObjectAtIndex:0]; //remove melody name

            [newSong setLyric:[newLineseparatedText componentsJoinedByString:@"\n"]];

            [songs addObject:newSong];
        }
    }
}

[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

[super viewDidLoad];


}

-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
    if([self.songs containsObject:self.allChapters[i]] == false)
        [self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

}

-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
    if([self.songs containsObject:self.allChapters[i]] == false)
        [self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

}

-(void)removeChaptersExcept:(Song *) chapter{
for(int i = 0; i < self.allChapters.count; i++){
    if(self.allChapters[i] != chapter && [self.songs containsObject:self.allChapters[i]])
        [self.songs removeObject:self.allChapters[i]];
}

}

このコードの最後の行は、mutableArray に nil 要素がいくつかあるため、エラーが発生した場合です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.chosenChapter isEqualToString:@"Alla"]){
    [self addChapters];
}
else{
    Song *chapter = nil;
    for(int i = 0; i < self.allChapters.count; i++){
        if([((Song *)self.allChapters[i]).title isEqualToString:self.chosenChapter]){
            chapter = self.allChapters[i];
            break;
        }

    }
    [self addChapters];
    [self removeChaptersExcept:chapter];
}




NSString *cellIdentifier = nil;
UITableViewCell *cell = nil;

NSString *page = ((Song *)self.songs[indexPath.row]).page;

ここにいくつかの画面の隆起があります

これは最初のオブジェクトを削除する前です

ここに画像の説明を入力

これは、最初のオブジェクトが削除された後です。1 つの要素が期待どおりに消え、もう 1 つの要素がゼロに設定されていることがわかりますか?

ここに画像の説明を入力

4

1 に答える 1

2

変数ビューに誤った値が表示されることがあります。配列に nil 値を含めることはできないため、これは間違いなく値が間違っている場合です。この行でアプリケーションがクラッシュすると述べています。

NSString *page = ((Song *)self.songs[indexPath.row]).page;

私の推測では、self.songs[indexPath.row] には page というプロパティがありません。この行を次のコードに置き換えてみてください。

Song *s = self.songs[indexPath.row];
if (s == nil)
{
    NSLog("Song is nil! How can that be?");
}
else
{
    NSLog("Page is %@", s.page);
}

問題を特定するのに役立ちます。幸運を。

于 2013-03-17T14:21:18.547 に答える