Error[8]: Undefined offset: 35, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中使用SCNetworkReachability

(此答案由于Swift语言的更改而反复扩展,这使它有点混乱。我现在将其重写并删除了所有与Swift
1.x相关的内容。如果有人需要,可以在编辑历史记录中找到较旧的代码。它。)

这就是您在 Swift 2.0(Xpre 7)中的 做法:

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(
var zeroAddress = sockaddr_in()
)) }) else { return false } var flags : SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return false } let isReachable = flags.contains(.Reachable) let needsConnection = flags.contains(.ConnectionRequired) return (isReachable && !needsConnection)}

说明:

并检查标志

UnsafeMutablePointer<SCNetworkReachabilityFlags>

另请注意,从Swift2开始,可以注册一个通告程序回调,将使用Swift和Swift2中的C API与Swift 2-UnsafeMutablePointer

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {        [+++].withMemoryRebound(to: sockaddr.self, capacity: 1) { SCNetworkReachabilityCreateWithAddress(nil, [+++])        }    }) else {        return false    }    var flags: SCNetworkReachabilityFlags = []    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {        return false    }    let isReachable = flags.contains(.reachable)    let needsConnection = flags.contains(.connectionRequired)    return (isReachable && !needsConnection)}
与object进行比较。


[+++]

不安全的指针不能再简单地转换为其他类型的指针(请参见-SE-0107 UnsafeRawPointer
API)。这里是更新的代码:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 36, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中使用SCNetworkReachability

(此答案由于Swift语言的更改而反复扩展,这使它有点混乱。我现在将其重写并删除了所有与Swift
1.x相关的内容。如果有人需要,可以在编辑历史记录中找到较旧的代码。它。)

这就是您在 Swift 2.0(Xpre 7)中的 做法:

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(
var zeroAddress = sockaddr_in()
)) }) else { return false } var flags : SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return false } let isReachable = flags.contains(.Reachable) let needsConnection = flags.contains(.ConnectionRequired) return (isReachable && !needsConnection)}

说明:

并检查标志

UnsafeMutablePointer<SCNetworkReachabilityFlags>

另请注意,从Swift2开始,可以注册一个通告程序回调,将使用Swift和Swift2中的C API与Swift 2-UnsafeMutablePointer

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {        .withMemoryRebound(to: sockaddr.self, capacity: 1) { SCNetworkReachabilityCreateWithAddress(nil, [+++])        }    }) else {        return false    }    var flags: SCNetworkReachabilityFlags = []    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {        return false    }    let isReachable = flags.contains(.reachable)    let needsConnection = flags.contains(.connectionRequired)    return (isReachable && !needsConnection)}
与object进行比较。


[+++]

不安全的指针不能再简单地转换为其他类型的指针(请参见-SE-0107 UnsafeRawPointer
API)。这里是更新的代码:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 37, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中使用SCNetworkReachability

(此答案由于Swift语言的更改而反复扩展,这使它有点混乱。我现在将其重写并删除了所有与Swift
1.x相关的内容。如果有人需要,可以在编辑历史记录中找到较旧的代码。它。)

这就是您在 Swift 2.0(Xpre 7)中的 做法:

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(
var zeroAddress = sockaddr_in()
)) }) else { return false } var flags : SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return false } let isReachable = flags.contains(.Reachable) let needsConnection = flags.contains(.ConnectionRequired) return (isReachable && !needsConnection)}

说明:

并检查标志

UnsafeMutablePointer<SCNetworkReachabilityFlags>

另请注意,从Swift2开始,可以注册一个通告程序回调,将使用Swift和Swift2中的C API与Swift 2-UnsafeMutablePointer

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {        .withMemoryRebound(to: sockaddr.self, capacity: 1) { SCNetworkReachabilityCreateWithAddress(nil, )        }    }) else {        return false    }    var flags: SCNetworkReachabilityFlags = []    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {        return false    }    let isReachable = flags.contains(.reachable)    let needsConnection = flags.contains(.connectionRequired)    return (isReachable && !needsConnection)}
与object进行比较。


[+++]

不安全的指针不能再简单地转换为其他类型的指针(请参见-SE-0107 UnsafeRawPointer
API)。这里是更新的代码:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 38, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中使用SCNetworkReachability

(此答案由于Swift语言的更改而反复扩展,这使它有点混乱。我现在将其重写并删除了所有与Swift
1.x相关的内容。如果有人需要,可以在编辑历史记录中找到较旧的代码。它。)

这就是您在 Swift 2.0(Xpre 7)中的 做法:

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(
var zeroAddress = sockaddr_in()
)) }) else { return false } var flags : SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return false } let isReachable = flags.contains(.Reachable) let needsConnection = flags.contains(.ConnectionRequired) return (isReachable && !needsConnection)}

说明:

并检查标志

UnsafeMutablePointer<SCNetworkReachabilityFlags>

另请注意,从Swift2开始,可以注册一个通告程序回调,将使用Swift和Swift2中的C API与Swift 2-UnsafeMutablePointer

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {        .withMemoryRebound(to: sockaddr.self, capacity: 1) { SCNetworkReachabilityCreateWithAddress(nil, )        }    }) else {        return false    }    var flags: SCNetworkReachabilityFlags = []    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {        return false    }    let isReachable = flags.contains(.reachable)    let needsConnection = flags.contains(.connectionRequired)    return (isReachable && !needsConnection)}
与object进行比较。


不安全的指针不能再简单地转换为其他类型的指针(请参见-SE-0107 UnsafeRawPointer
API)。这里是更新的代码:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
如何在Swift中使用SCNetworkReachability_随笔_内存溢出

如何在Swift中使用SCNetworkReachability

如何在Swift中使用SCNetworkReachability,第1张

如何在Swift中使用SCNetworkReachability

(此答案由于Swift语言的更改而反复扩展,这使它有点混乱。我现在将其重写并删除了所有与Swift
1.x相关的内容。如果有人需要,可以在编辑历史记录中找到较旧的代码。它。)

这就是您在 Swift 2.0(Xpre 7)中的 做法:

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(
var zeroAddress = sockaddr_in()
)) }) else { return false } var flags : SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return false } let isReachable = flags.contains(.Reachable) let needsConnection = flags.contains(.ConnectionRequired) return (isReachable && !needsConnection)}

说明:

  • 从Swift 1.2(Xpre 6.3)开始,导入的C结构在Swift中具有默认的初始化程序,该初始化程序将该结构的所有字段初始化为零,因此可以使用以下方式初始化套接字地址结构:

    sizeofValue()
  • UInt8
    给出此结构的大小,必须将其转换
    sin_len
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))

    AF_INET
  • Int32
    sin_family
    ,必须将其转换为以下类型的正确类型
    zeroAddress.sin_family = sa_family_t(AF_INET)

    withUnsafePointer(&zeroAddress) { ... }
  • SCNetworkReachabilityCreateWithAddress()
    将结构的地址传递到闭包,在闭包处作为实参
    UnsafePointer(
    sockaddr
    )
    。该
    sockaddr_in
    转换是必要的,因为该功能需要一个指针
    withUnsafePointer()
    ,而不是
    SCNetworkReachabilityCreateWithAddress()

  • 从中

    SCNetworkReachability?
    返回的值是从的返回值
    guard let
    ,其类型为
    defaultRouteReachability
    ,即它是可选的。的
    nil
    声明(在夫特2.0新功能)分配展开值到
    else
    如果不是可变
    SCNetworkReachabilityCreateWithAddress()
    。否则
    SCNetworkReachabilityFlags
    执行该块,然后函数返回。

  • 从Swift 2开始,

    OptionSetType
    返回一个托管对象。您不必显式释放它。

  • 从Swift 2开始,
    var flags : SCNetworkReachabilityFlags = []
    符合的
        let isReachable = flags.contains(.Reachable)let needsConnection = flags.contains(.ConnectionRequired)
    具有类似set的界面。您使用创建一个空标志变量
    SCNetworkReachabilityGetFlags

并检查标志

UnsafeMutablePointer<SCNetworkReachabilityFlags>
  • 的第二个参数地址具有type
    <Void>
    ,这意味着您必须传递flags变量的 Swift 3/4的更新:

另请注意,从Swift2开始,可以注册一个通告程序回调,将使用Swift和Swift2中的C API与Swift 2-UnsafeMutablePointer

import SystemConfigurationfunc connectedToNetwork() -> Bool {    var zeroAddress = sockaddr_in()    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)    zeroAddress.sin_family = sa_family_t(AF_INET)    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {        .withMemoryRebound(to: sockaddr.self, capacity: 1) { SCNetworkReachabilityCreateWithAddress(nil, )        }    }) else {        return false    }    var flags: SCNetworkReachabilityFlags = []    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {        return false    }    let isReachable = flags.contains(.reachable)    let needsConnection = flags.contains(.connectionRequired)    return (isReachable && !needsConnection)}
与object进行比较。


不安全的指针不能再简单地转换为其他类型的指针(请参见-SE-0107 UnsafeRawPointer
API)。这里是更新的代码:



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5126478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存