2014-09-03 43 views
2

我只想打开提供URL的网页。我的代码是:在Objective-C中使用变量

void openURL(char const *message) 
{ 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:message]]; 
} 

而我得到的错误是:

cannot initialize a parameter of type 'NSString *' with an lvalue of type 'const char *' 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:message]]; 

我不熟悉Objective-C的,也没有学习它。我只需要在一个项目中使用这个简单的代码。

回答

3

一个解决方案是message转换为NSString,像这样:

void openURL(char const *message) 
{ 
    NSString *messageStr = [NSString stringWithCString:message 
              encoding:NSUTF8StringEncoding]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:messageStr]]; 
} 

另一种解决方案是通过NSString(这尔德要求在openURL被称为所有地方建设NSString S:

void openURL(NSString *message) 
{ 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:message]]; 
} 
+0

第一个解决方案工作。非常感谢你。 – rootpanthera 2014-09-03 10:05:46

0

尝试先将char数组转换为nsstring对象,然后再创建该字符串的url。 likebelow: - void openURL (char const message) NDString s = [[NSString alloc] initWithBytes:buffer length:sizeof(message)encoding:NSASCIIStringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:s]]; }