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

奥列格

我尝试从 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 中无效

什_汗

你可以试试

- (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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章