Iphone 将代码转换为函数

Iphone 将代码转换为函数,iphone,objective-c,cocoa-touch,programming-languages,audiotoolbox,Iphone,Objective C,Cocoa Touch,Programming Languages,Audiotoolbox,大家好,我需要把这段代码转换成一个有两个参数的函数。在这种情况下,第一个参数是News,第二个参数是aif,你能这样做吗 -(IBAction)news { CFURLRef soundFileURLRef; SystemSoundID soundFileObject; CFBundleRef mainBundle; mainBundle = CFBundleGetMainBundle (); // Get the URL to the sound file to play

大家好,我需要把这段代码转换成一个有两个参数的函数。在这种情况下,第一个参数是News,第二个参数是aif,你能这样做吗

-(IBAction)news
{
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Get the URL to the sound file to play
soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
                                             CFSTR ("News"),
                                             CFSTR ("aif"),
                                             NULL);

// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

AudioServicesPlaySystemSound (soundFileObject);

return;

}

如果这是一个类从使用控件的用户处接收到的操作,那么您唯一能做的就是
-(iAction)nameOfAction:(id)sender。否则,您需要编写命令以获取所需的两个参数,并从
iAction

调用该参数。如果问题出在CFSTR上,那么解决方案是:

-(void) playNews: (NSString*) news type: (NSString*) type
{
    CFURLRef        soundFileURLRef;
    SystemSoundID   soundFileObject;
    CFBundleRef mainBundle;
    mainBundle = CFBundleGetMainBundle ();

    // Get the URL to the sound file to play
    soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
            (CFStringRef)news,
            (CFStringRef)type,
            NULL);

    // Create a system sound object representing the sound file
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

    AudioServicesPlaySystemSound (soundFileObject);
}

但是(iAction)声明在这里没有意义,因为这个方法不能直接从Interface Builder中使用。

您好,谢谢您的回答,这就是我想要的,但是当我写PlayNews时:@“news1”的类型:@“wav”;xcode给了我一个错误:应该是“;”在“type”之前,你能帮我解决你需要把它放在尖括号中的问题吗:[obj playNews:@“news”of type:@“wav”];