首页 > 解决方案 > 在 Swift 4 中处理 SIGPIPE

问题描述

我正在尝试将我的 Obj-C 代码移植到 Swift 项目中,并且在编译 SIGPIPE 处理程序时遇到了困难:

func SigPipeHandler()
{
  print(@"We Got a Pipe Single :%d____________",s);
}


func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    signal(SIGPIPE, SigPipeHandler);
    // Override point for customization after application launch.
    return true
}

我收到错误消息:

无法将类型“()->()”的值转换为预期的参数类型“(@convention(c)(Int32)-> Void)?”

标签: swift

解决方案


let handler: @convention(c) (Int32) -> () = { sig in
    // handle the signal somehow
    print("error", sig)
}


func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    signal(SIGPIPE, handler);
    // Override point for customization after application launch.
    return true
}

推荐阅读