将Swift代码转换为Objective-C错误

时间:2019-05-30 11:04:31

标签: objective-c swift

我尝试从Swift转换为Objective-C,但是有错误

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    print(url)

    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)

    let host = urlComponents?.host ?? ""

    print(host)

    return true
}

在对象中尝试:

  - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    printf("%s", url);

    NSURLComponents * const urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true);

    NSString * const host = urlComponents.host ?? ""

    printf(host);
    return true;
    }

但是有错误

  

函数“ URLComponents”的隐式声明在C99中无效

2 个答案:

答案 0 :(得分:1)

您可以尝试

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    printf("%s", url);

    NSURLComponents *  urlComponents = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:true];

    NSString *  host = urlComponents.host;

    NSLog(host);

    return true;
}

答案 1 :(得分:0)

尝试一下。,它将正常运行...

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
   NSLog(@"%@", url);
   NSURLComponents * const urlComponents = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:true];
   NSString * const host = urlComponents.host;
   NSLog(@"%@", host);
   return true;
}