JoeyParis
JoeyParis

Reputation: 27

Whats wrong with this array? Whenever I run the action it locks up

Why isn't this code working? The app loads fine, however whenever I press the button the application locks up (doesn't crash) and returns me to Xcode and there is a red arrow pointing to one of the lines (Indicated on the line below).

I also get three warnings (not errors), one says 'NSMutableArray' may not respond to '-objectAt:' and the second one says unused variable 'soundToPlay' The third one say implicit declaration of function 'AudioServicesPlaySystemSound' Should I be concerned about those?

    //In MainViewController.h

    @interface MainViewController : UIViewController {
        IBOutlet UIButton *Button;      
    }

    @property (nonatomic, retain) UIButton *Button;

    - (IBAction)playSound;

    NSInteger currentSound;
    NSMutableArray *soundArray;

    @end




    //In MainViewController.m

        - (void)viewDidLoad {
        currentSound = 0;
        NSMutableArray *sounds = [[NSMutableArray alloc] init];
        [sounds addObject: @"0.wav"];
        [sounds addObject: @"1.wav"];
        [sounds addObject: @"2.wav"];
        [sounds addObject: @"3.wav"];
        [sounds addObject: @"4.wav"];
        [sounds addObject: @"5.wav"];
        [sounds addObject: @"6.wav"];
        [sounds addObject: @"7.wav"];
        [sounds addObject: @"8.wav"];
        [sounds addObject: @"9.wav"];
        [sounds addObject: @"10.wav"];
        [sounds addObject: @"11.wav"];
        [sounds addObject: @"12.wav"];
        [sounds addObject: @"13.wav"];
        [super viewDidLoad];
    }

    - (IBAction)playSound {
        NSString *soundToPlay = [soundArray objectAt: currentSound];
//First two errors here.
        currentSound = (currentSound + 1) % [soundArray count]; //Red arrow points to this line.
        AudioServicesPlaySystemSound (currentSound);
//Third error here.
    }

Upvotes: 0

Views: 777

Answers (2)

Jakob Dam Jensen
Jakob Dam Jensen

Reputation: 434

First one: The methodname doesn't exist. You meant ot write objectAtIndex instead of objectAt. When it says that the object might now respond to the '-something' it's because the methods isn't declared in the header file. This results in the compiler not being sure whether or not the method exists in the implementation file or not. When you're using built-in classes (NSArray, NSView etc.) these kinds of warnings usually means that you misspelled a method-name. The only thing to do is to lookup in the documentation and doublecheck. These kind of type-mistakes will cause a crash in the app when trying to send a message to an object which doesn't repond to the selector.

Second one: That's just a warning telling you that a variable isn't being used. Remove the line. As you can see soundToPlay isn't being used in the playSound method, it's only being declared. If you use it fx. with a NSLog function call or in a method this warning should disappear.

Third one: You might be using the function wrong. Can't say from the code you posted..

Other: Daniel is right. Your viewDidLoad method is creating a local array and never storing this in the soundArray instancevariable. To do so add a

soundArray = sounds

in the bottom of the method. Or just write directly to soundsArray instead of sounds.

Upvotes: 2

Daniel
Daniel

Reputation: 22395

From the code seen, doesnt seem you ever intialize soundArray, you are populating a local variable in viewDidLoad called sounds, and never setting soundArray...

Upvotes: 1

Related Questions