How to Check Bitfields (Scnetworkreachabilityflags in Particular) for Flags in Swift

How to check bitfields (SCNetworkReachabilityFlags in particular) for flags in Swift?

That error is actually complaining not about the arguments of your flags-check, but about the return value. An if statement expects a boolean (or at least something conforming to Logical), but the & operator for two Int values returns an Int. You just need a comparison in your if statement:

let flags = kSCNetworkReachabilityFlagsReachable
if 0 != flags & kSCNetworkReachabilityFlagsReachable {
println("flags contains kSCNetworkReachabilityFlagsReachable")
}

Since SCNetworkReachabilityFlags and the constants are (strangely) of different types, you'll need to do some casting to make the comparison work:

let reachabilityFlags:SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(kSCNetworkReachabilityFlagsReachable)
if 0 != reachabilityFlags & SCNetworkReachabilityFlags(kSCNetworkReachabilityFlagsReachable) {
println("reachabilityFlags contains kSCNetworkReachabilityFlagsReachable")
}

Wrapping libcups C library in Swift

IMHO the best way to work with such C libraries in Swift is to work with them in Objective-C. Nevertheless, the main issue seems to be that you are not writing back changes to userData, that's why you always get your initial value. You need to update it in your callback like this

let userDataPointer = user_data!.assumingMemoryBound(to: my_user_data_t.self)
var userData = userDataPointer.pointee
...
// make some changes to userData

userDataPointer.pointee = userData

return 1

Also there seems to be some differences between your C code and the Swift one, like checking flags with 'flags == CUPS_DEST_FLAGS_REMOVED' which, in general, is incorrect way to check flags, and comparing result of cupsEnumDests with 1 when in the original code you check that result is not 0.

Reachability framework is not working fine while detecting interface type when there is multiple network interface

This issue has been fixed. Posting it so that user's can get help. The main problem was that I was passing NULL in zeroAddress structure. After assigning my network interface address in the structure it passed all the cases and determined network interface type correctly.



Related Topics



Leave a reply



Submit