I'm getting a semantic warning on Xcode 4 : *Declaration of 'struct sockaddr_in' will not be visible outside of this function* the struct seems to be declared in netinet/in.h
The warning is getting marked on Reachability.h, its a class that I downloaded from Apple examples.
#import <Foundation/Foundation.h> #import <SystemConfiguration/SystemConfiguration.h> typedef enum { NotReachable = 0, ReachableViaWiFi, ReachableViaWWAN } NetworkStatus; #define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification" @interface Reachability: NSObject { BOOL localWiFiRef; SCNetworkReachabilityRef reachabilityRef; } //reachabilityWithHostName- Use to check the reachability of a particular host name. + (Reachability*) reachabilityWithHostName: (NSString*) hostName; //reachabilityWithAddress- Use to check the reachability of a particular IP address. + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; //reachabilityForInternetConnection- checks whether the default route is available. // Should be used by applications that do not connect to a particular host + (Reachability*) reachabilityForInternetConnection; //reachabilityForLocalWiFi- checks whether a local wifi connection is available. + (Reachability*) reachabilityForLocalWiFi; //Start listening for reachability notifications on the current run loop - (BOOL) startNotifier; - (void) stopNotifier; - (NetworkStatus) currentReachabilityStatus; //WWAN may be available, but not active until a connection has been established. //WiFi may require a connection for VPN on Demand. - (BOOL) connectionRequired; @end
I don't understand the warning, can someone explain it to me? Thank you.
Someone filed a bug report against the behavior and got a response from someone here. Essentially, the problem is that you're declaring a new struct (so far as the compiler can tell) in the parameter of the method, so it will not be accessible elsewhere.
There is a quick fix for it. Simply add the following line to Reachability.h
:
#import <netinet/in.h>
You're declaring a new struct in a method parameter, as opposed to at file scope.
The warning will go away if you add a forward declaration at the beginning of the file (somewhere before the @interface
section).
struct sockaddr_in ;
Doing this instead of #import <netinet/in.h>
avoids header file bloat.
(Speaking of reducing header bloat you can cut down header use in Reachability.h
by replacing the lines
#import <Foundation/Foundation.h> #import <SystemConfiguration/SystemConfiguration.h>
with
#import <SystemConfiguration/SCNetworkReachability.h>
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With