0

i would like to create a function called "playSound" with one (but later two parameters - one, which is the filename and one, which is the musictype (mp3,...))

but i cant get the paramter working in my function...please help me

here's my code

    //
//  MainView.m
//
//  Created by Christopher Fuchs on 26.01.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MainView.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>

@implementation MainView

void playSound(char filename){
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mein Alert" message:filename delegate:nil cancelButtonTitle:@"Abbrechen" otherButtonTitles:nil];
 [alert show];
 [alert release];

}

- (IBAction)PlayFatGuyWalking:(id)sender {

 playSound(@"MusicFile");

}
@end

For now, i just added an alert instead of the playing sound framework the paramater is called "filename" and should be called as message of the alert

thanks in advance

greez

Chris
  • 200
  • 2
  • 4
  • 12
  • This has been covered many times, e.g. http://stackoverflow.com/questions/683211/method-syntax-in-objective-c , even Wikipedia describes the differences between C functions and Obj-C methods: http://en.wikipedia.org/wiki/Objective-C#Messages You need to change the prototype for playSound to Obj-C syntax. Moreover, the @".." literal is for a NSString, not a char (nor char*, which is probably what you meant). – pmdj Jan 26 '11 at 08:44

2 Answers2

3
- (void)playSound:(NSString *)filename {
  // method body
}

// calling
[self playSound:@"MusicFile"];
taskinoor
  • 45,586
  • 12
  • 116
  • 142
1

Make this line:

void playSound(char filename)

into this:

void playSound(NSString *filename)

The @ "foo" construction says "this is an NSString".

Also, if you want to keep it a C function, you might want to move it out of the @implementation block.

You will find that virtually everything asking for a string in Cocoa will want an NSString, and not a char *.

Oh yeah, and the (char filename) won't work in plain C either, that a single byte variable, not a pointer to a potential string.

Hack Saw
  • 2,741
  • 1
  • 18
  • 33
  • If you really want it to be a C function, not an Objective-C method, you also need to move it outside the class. – pmdj Jan 26 '11 at 08:53