简单地将字符串添加到可变阵列

时间:2014-12-26 05:27:39

标签: objective-c model-view-controller nsstring nsmutablearray

我试图做一些看似非常微不足道的事情,但我遇到了一些奇怪的问题。我试图创建包含主包中对象路径的字符串,然后将字符串存储在数组中,如下所示:

-(void)loadSounds {

    NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
    [soundsArray addObject:soundPath1];

    NSString *soundPath2 = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
    [soundsArray addObject:soundPath2];

    NSString *soundPath3 = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"Wav"];
    [soundsArray addObject:soundPath3];

    NSLog(@"In the model, soundsArray is: %lu", (unsigned long)soundsArray.count);

现在,由于某种原因,NSLog读取数组为空...即使我正在添加对象。我认为这是一个简单直接的方法是向数组添加一个字符串,但显然不是。我必须错过一些非常简单的事情。有没有人有想法?作为旁注,模型中应加载诸如将声音加载到应用程序中的逻辑,并由控制器根据MVC设计模式进行控制,是否正确?

1 个答案:

答案 0 :(得分:2)

试试这个

 -(void)loadSounds {


    soundsArray=[NSMutableArray alloc]init];

        NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
        [soundsArray addObject:soundPath1];

        NSString *soundPath2 = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
        [soundsArray addObject:soundPath2];

        NSString *soundPath3 = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"Wav"];
        [soundsArray addObject:soundPath3];

        NSLog(@"In the model, soundsArray is: %lu", (unsigned long)soundsArray.count);
    }