2011-12-30 72 views
1

我嘗試打開一個URL對象C我的代碼如下簡單的Objective-C的腳本打開一個URL

#include <stdio.h> 
int main() 
{ 
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.ithinksw.com/"]]; 
return 0; 
} 

,但是當我嘗試編譯它,我得到這些錯誤

hello.c: In function 'main': 
hello.c:4: error: expected expression before '[' token 
hello.c:4: error: 'NSWorkspace' undeclared (first use in this function) 
hello.c:4: error: (Each undeclared identifier is reported only once 
hello.c:4: error: for each function it appears in.) 
hello.c:4: error: expected ']' before 'openURL' 
hello.c:4: error: stray '@' in program 

IM極值新本所以任何幫助將是巨大的:)

回答

4

似乎你將目標設定爲iphone,所以你必須使用[[UIApplication sharedApplication] openURL]而不是NSWorkspace。

#import <Foundation/Foundation.h> 

#if TARGET_OS_IPHONE 
#import <UIKit/UIKit.h> 
#endif 

// ... 

#if TARGET_OS_IPHONE 
    [[UIApplication sharedApplication] openURL:url]; 
#elif TARGET_OS_MAC 
    [[NSWorkspace sharedWorkspace] openURL:url]; 
#endif 

並且不要忘記添加Foundation框架包含和鏈接部分。

+0

所以這樣? #include #import #include #link 是的他的iPhone版:P – 2011-12-30 12:43:11

+0

是的,那是包括。另外,您必須將Foundation框架添加到您的項目中。 – 4ndrew 2011-12-30 12:49:53

+0

有沒有辦法做到這一點,沒有Xcode是在那裏:( – 2011-12-30 12:55:05

2

你必須

#import <Foundation/Foundation.h> 

此外,文件名應該是hello.m,以便編譯器知道它在這裏處理Objective-C。

並確保將您的二進制文件鏈接到Foundation框架。

+0

感謝您的好建議 – 2011-12-30 12:38:17

1

如果您是iOS編程的新手,我強烈推薦使用xCode中的一些Apples模板作爲您開發的基礎。

NSWorkspace是一個MacOS X構造。我不認爲有任何iOS的等價物。

看看iTunesU上的CP193P視頻,讓您開始使用iOS編程 - 開心編程!

+0

非常感謝您的建議和鼓勵。 p.s偉大的鏈接CP193P iv已經開始看:) – 2011-12-30 12:39:39