Ad
Binary Operator '&' Cannot Be Applied To Operands Of Type 'SCNetworkReachabilityFlags' And 'Int'
I am getting this error when try to check for internet connectivity in my app.
I use Xcode 7.1.1 Swift 2.0
The code block is as follows
public class Reach {
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(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))//.takeRetainedValue()
})
var flags: SCNetworkReachabilityFlags = []
if SCNetworkReachabilityGetFlags( defaultRouteReachability!, &flags) == false {
return false
}
//The following two lines shows the error
let isReachable = (flags & (kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & (kSCNetworkFlagsConnectionRequired)) != 0
return isReachable && !needsConnection
}
}
I tried casting the flag to Int and the NetworkFlag to UInt32 and vice versa but still it shows warnings and gets error while executing. I tried the following
(flags & UInt32(kSCNetworkFlagsReachable))
(Int(flags) & (kSCNetworkFlagsReachable))
(flags & (kSCNetworkFlagsReachable) as! UInt32)
(flags as! Int & (kSCNetworkFlagsReachable))
But nothing worked. Can anyone solve it
Content of SCNetworkReachabilityFlags
public struct SCNetworkReachabilityFlags : OptionSetType {
public init(rawValue: UInt32)
public static var TransientConnection: SCNetworkReachabilityFlags { get }
public static var Reachable: SCNetworkReachabilityFlags { get }
public static var ConnectionRequired: SCNetworkReachabilityFlags { get }
public static var ConnectionOnTraffic: SCNetworkReachabilityFlags { get }
public static var InterventionRequired: SCNetworkReachabilityFlags { get }
public static var ConnectionOnDemand: SCNetworkReachabilityFlags { get } // __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_3_0)
public static var IsLocalAddress: SCNetworkReachabilityFlags { get }
public static var IsDirect: SCNetworkReachabilityFlags { get }
public static var IsWWAN: SCNetworkReachabilityFlags { get }
// TARGET_OS_IPHONE
public static var ConnectionAutomatic: SCNetworkReachabilityFlags { get }
}
Ad
Answer
Try just go
let isReachable = flags.contains(SCNetworkReachabilityFlags.Reachable)
let needsConnection = flags.contains(SCNetworkReachabilityFlags.ConnectionRequired)
the OptionSetType has a convenience method for contains, so hopefully that will check if the flag is set
Ad
source: stackoverflow.com
Related Questions
- → Function Undefined in Axios promise
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Click to navigate on mobile devices
- → Playing Video - Server is not correctly configured - 12939
- → How to allow api access to android or ios app only(laravel)?
- → Axios array map callback
- → Access the Camera and CameraRoll on Android using React Native?
- → Update React [Native] View on Day Change
- → Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct
- → BigCommerce and shopify API
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → React Native - Differences between Android and IOS
- → What is the difference between React Native and React?
Ad