2012-12-12 45 views
-2

我有一個聲音文件,當我加載應用程序時想播放,但是我只能在按下按鈕時播放。我是否需要有.h文件或者它僅僅.m文件Xcode幫助預期表達式

 #import "ViewController.h" 

//sound 
@implementation ViewController 


-(void) viewDidLoad { 


-(IBAction)play {   Here i get error Expected expression. 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileURLRef; 
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"app3", CFSTR 
    ("m4a"), NULL); 
    UInt32 (soundID); 
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
    } 

[super viewDidLoad]; 
} 

-(IBAction)Facebook { 
[[UIApplication sharedApplication] 
openURL: [NSURL URLWithString:@"http://www.facebook.com/synicapps?fref=ts]"]]; 
} 



@end 
+0

請考慮重新格式化你的代碼,它的醜陋。 – 2012-12-12 19:54:52

+1

爲什麼你在'viewDidLoad'內嵌入'play'方法?這本身就是一個錯誤。 – trudyscousin

回答

0

那裏有一些簡單的錯誤。我建議首先閱讀Objective-C的基礎知識。

總之,這裏的搞掂代碼:

#import "ViewController.h" 

//sound 
@implementation ViewController 

-(void) viewDidLoad { 
    [super viewDidLoad]; 
    [self play]; 
} 

-(IBAction)play { 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileURLRef; 
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"app3", CFSTR ("m4a"), NULL); 
    UInt32 soundID; 
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
} 

-(IBAction)Facebook { 
    [[UIApplication sharedApplication] 
     openURL: [NSURL URLWithString:@"http://www.facebook.com/synicapps?fref=ts]"]]; 
} 

@end 
0

您從佈局鏈接buuton到IBAction工作或聲明UIButton並添加標記把它 @implementation ViewController中

-(void) viewDidLoad { 
    [super viewDidLoad]; 
} 


-(IBAction)play {   
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileURLRef; 
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"app3", CFSTR 
    ("m4a"), NULL); 
    UInt32 (soundID); 
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
} 

-(IBAction)Facebook { 
[[UIApplication sharedApplication] 
openURL: [NSURL URLWithString:@"http://www.facebook.com/synicapps?fref=ts]"]]; 
} 

@end 

我知道這還意味着什麼:秒,但什麼是你想用這個做:■

0

爲什麼你得到一個「預期的表達」錯誤的原因是因爲你一直在努力,聲明和定義一個方法 (播放)在另一個方法(viewDidLoad)中。它將該聲明視爲一個強制轉換,其中方法名稱是要轉換的變量。因此,它期望將一個有效的類型放在括號內。然而,IBAction是一個無所事事的宏。刪除IBAction,錯誤依然存在。將它替換爲一個類型(如void),它現在會給出一個「未聲明的變量」錯誤,這證明這是因爲它認爲它是一個變量。

TL; DR:您不能在另一箇中定義一種方法。你可能想考慮閱讀一本關於Objective-C的好書。

編輯:對於那些有興趣的人來說,這是它認爲提問者的意思。

減去一元運算符(類型)變量

這是一個組合的演員和一元負鑄造後得到一個變量的負值。防爆。

int foo = 10; 
float bar = -(float)play; 

它看起來像一個方法聲明,不是嗎?