【问题标题】:Convert code to function将代码转换为函数
【发布时间】:2010-10-04 19:16:27
【问题描述】:

大家好,我需要将此代码转换为具有 2 个参数的函数,在这种情况下,第一个参数是 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;

}

【问题讨论】:

    标签: iphone objective-c cocoa-touch programming-languages audiotoolbox


    【解决方案1】:

    如果 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);
    }
    

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

    【讨论】:

    • 你好,谢谢你的回答,这就是我想要的,但是当我写 PlayNews:@"news1" ofType:@"wav"; xcode给我一个错误:预期的';'在'type'之前你能帮我解决这个问题吗
    • 需要放在尖括号中:[obj playNews:@"news" ofType:@"wav"];
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 2022-09-26
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 2018-11-07
      • 2011-09-30
      相关资源
      最近更新 更多