views:

236

answers:

2

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.
    }
+1  A: 

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

Daniel
It looks like Matlab code in the .m file. In matlab you don't need to initialize variables.
yelinna
what? soundArray has nothing in it, going by the code above that is
Daniel
@yelinna: the OP says in the tags that it's objc on the iphone.
Tim
+1  A: 

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.

Jakob Dam Jensen
Thank you that helped get rid of all the errors except that unused variable error. Essentially what I would like to do use the names generated in the array as the name of the sound file and then play it, I've tried a few methods such as the AudioServicesPlaySystemSound function but no luck. I've checked the Apple documentation for it and did a few Google searches but I wasn't able to find a solid, working answer.
infiniteloop91
The thing is that AudioServicesPlaySystemSound takes a SystemSoundID and not a not a NSString. This is from the documentation: The system sound to play. Before using this function, call the AudioServicesCreateSystemSoundID function to obtain a system sound. Have you read to docs? I don't have much experience with this part of the frameworks.. But you're storing NSStrings in the array and you can't parse the content directly to the function. That is why you're getting an error now...
Jakob Dam Jensen
try looking into this example code: https://developer.apple.com/iphone/prerelease/library/samplecode/SysSound/index.html
Jakob Dam Jensen