swift 4 xcode 9

时间:2018-03-22 11:03:29

标签: xcode swift4 wkwebview reachability

我想在xcode 9中创建可访问性功能但是在做可达性类时我遇到了这个错误,如何解决这个错误? “类型'SCNetworkReachability的价值?'没有成员'takeRetainedValue'“。这是我的代码

import UIKit
import Foundation
import SystemConfiguration

public class reachability {

    class func isConnectedToNetwork() -> Bool {

        var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
        zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)


        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
        }

        var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
            return false
        }

        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

        return (isReachable && !needsConnection) ? true : false
    }

}

enter image description here

1 个答案:

答案 0 :(得分:4)

在Swift 4中编译代码时会出现许多错误。

Swift 4兼容版本是(请用起始大写字母命名类)

...
1>Using "ResolveComReference" task from assembly "Microsoft.Build.Tasks.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>Task "ResolveComReference"
1>  Creating new cache file at "obj\x86\Debug\MyAssembly.csproj.ResolveComReference.cache".
1>  Adding a matching tlbimp reference for the aximp reference "AxTeeChart".
1>  Resolving COM reference for item "stdole" with a wrapper "primary".
1>  Determining dependencies of the COM reference "stdole".
1>  Resolved COM reference for item "stdole": "C:\Windows\assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a\stdole.dll".
1>  Resolving COM reference for item "AxTeeChart" with a wrapper "primaryortlbimp".
1>  Determining dependencies of the COM reference "AxTeeChart".
1>  C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\TlbImp.exe C:\Windows\SysWOW64\teechart.ocx /namespace:TeeChart /machine:X86 /out:obj\x86\Debug\Interop.TeeChart.dll /sysarray /transform:DispRet /reference:"C:\Program Files (x86)\ABCD\abcdSDK\DLL\x86\abcdsdk.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:C:\Windows\assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a\stdole.dll 
1>  Microsoft (R) .NET Framework Type Library to Assembly Converter 4.6.81.0
1>  Copyright (C) Microsoft Corporation.  All rights reserved.
1>  
1>  TlbImp : Type library imported to C:\repSVN\Prod2NCG2\prodmspeqspcp\trunk\src\MyAssembly\obj\x86\Debug\Interop.TeeChart.dll
1>  Resolved COM reference for item "AxTeeChart": "obj\x86\Debug\Interop.TeeChart.dll".
1>  Resolving COM reference for item "AxTeeChart" with a wrapper "aximp".
1>  Determining dependencies of the COM reference "AxTeeChart".
1>  C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\AxImp.exe C:\Windows\SysWOW64\teechart.ocx /out:obj\x86\Debug\AxInterop.TeeChart.dll /rcw:obj\x86\Debug\Interop.TeeChart.dll 
1>  Generated Assembly: C:\repSVN\Prod2NCG2\prodmspeqspcp\trunk\src\MyAssembly\obj\x86\Debug\AxInterop.TeeChart.dll
1>  Resolved COM reference for item "AxTeeChart": "obj\x86\Debug\AxInterop.TeeChart.dll".
1>Done executing task "ResolveComReference".
1>Done building target "ResolveComReferences" in project "MyAssembly.csproj".
...
...
...
2>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3258: The primary reference "MyAssembly" could not be resolved because it has an indirect dependency on the .NET Framework assembly "System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework.
2>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3258: The primary reference "MyAssembly" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework.
...
...
...
2>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3268: The primary reference "MyAssembly" could not be resolved because it has an indirect dependency on the framework assembly "System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "MyAssembly" or retarget your application to a framework version which contains "System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
2>  Primary reference "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
2>      Resolved file path is "C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll".
2>      Reference found at search path location "C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll".
2>      This reference is not "CopyLocal" because it's a prerequisite file.
2>      The ImageRuntimeVersion for this reference is "v2.0.50727".
2>  Primary reference "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
2>      Resolved file path is "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll".
2>      Reference found at search path location "{TargetFrameworkDirectory}".
2>          For SearchPath "{TargetFrameworkDirectory}".
2>          Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.winmd", but it didn't exist.
2>      This reference is not "CopyLocal" because it's a prerequisite file.
2>      The ImageRuntimeVersion for this reference is "v2.0.50727".
2>      Resolved file path is "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll".
...
...
...
2>      Reference found at search path location "{TargetFrameworkDirectory}".
2>          For SearchPath "{TargetFrameworkDirectory}".
2>          Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.winmd", but it didn't exist.
2>      This reference is not "CopyLocal" because it's a prerequisite file.
2>      The ImageRuntimeVersion for this reference is "v2.0.50727".
2>Done executing task "ResolveAssemblyReference".
....