【问题标题】:call Swift function with completion handler in Objective-C在 Objective-C 中使用完成处理程序调用 Swift 函数
【发布时间】:2020-02-10 13:20:35
【问题描述】:

我有一个 react 原生应用。我正在尝试将 react native 与 ios 联系起来:

RCT_EXPORT_METHOD(getUserProfile:(RCTResponseSenderBlock)callback) {
  Cosmote *cosmote = [[Cosmote alloc] init];
  NSString * x;
  NSString * str;
  x = [cosmote getUserProfileWithCompletionHandler:^(NSString* string){string}]
  callback(@[x]);
}

在 Cosmote.swift 中我有这个功能:

@objc
  open func getUserProfile(completionHandler: @escaping (_ s: String) -> ()) {
    let x = UserDetailsManager.self;
    x.getUserDetails(completionBlock: {data in
      print("got user Details")
      var json:String = "{\"data\": {\"guid\": \"\(data.guid)\", \"email\": \"\(data.email)\", \"username\": \"\(data.username)\", \"firstName\": \"\(data.firstname)\", \"lastName\": \"\(data.lastname)\", \"otePortalStatus\": \"\(data.otePortalStatus)\", \"otePortalisCorporate\": \"\(data.otePortalisCorporate)\", \"otePortalUserLevel\": \"\(data.otePortalUserLevel)\", \"otePortalAuthenticationLevel\": \"\(data.otePortalAuthenticationLevel)\", \"otePortalPIN\": \"\(data.otePortalPIN)\", \"otePortalEbppActivationDate\": \"\(data.otePortalEbppActivationDate)\", \"otePortalEbppEcareNumber\": \"\(data.otePortalEbppEcareNumber)\", \"otePortalEbppStatus\": \"\(data.otePortalEbppStatus)\", \"otegroupPasswordDate\": \"\(data.otegroupPasswordDate)\", \"otegroupStatus\": \"\(data.otegroupStatus)\", \"otegroupPasswordExpiration\": \"\(data.otegroupPasswordExpiration)\", \"otegroupPasswordReset\": \"\(data.otegroupPasswordReset)\", \"otegroupRegistrationDate\": \"\(data.otegroupRegistrationDate)\", \"otegroupAlternativeEmail\": \"\(data.otegroupAlternativeEmail)\", \"otegroupAlternativeMSISDN\": \"\(data.otegroupAlternativeMSISDN)\", \"imageURL\": \"\(data.imageURL)\"}}"
      completionHandler(json);
    }, onError: {_ in
      print("did not get user Details")
      completionHandler("{}");
    })
  }

第一个函数:

x = [cosmote getUserProfileWithCompletionHandler:^(NSString* string){string}]

我收到了错误:

从不兼容的类型'void'分配给'NSString *__strong'

不兼容的块指针类型发送 'void (^)(NSString *__strong)' 转换为 'NSString * _Nonnull (^ _Nonnull)(NSString * _Nonnull __strong)' 类型的参数

我该如何解决这个问题?

【问题讨论】:

    标签: objective-c swift react-native


    【解决方案1】:

    只需从方法completionHandler 中调用您的“回调”。 这应该适合你:

    RCT_EXPORT_METHOD(getUserProfile:(RCTResponseSenderBlock)callback) {
       Cosmote *cosmote = [[Cosmote alloc] init];
       [cosmote getUserProfileWithCompletionHandler:^(NSString* string){
          callback(string)
       }];
    }
    

    【讨论】:

    • 我不断收到错误:不兼容的块指针类型将 'void (^)(NSString *__strong)' 发送到类型为 'NSString * _Nonnull (^ _Nonnull)(NSString * _Nonnull __strong)'的参数/跨度>
    • @billysk 在同一行?
    • 是的,字符 ^ 加下划线
    • 好的 :D 我几乎没想到问题是 :D 那么请接受答案 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多