Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic warning on xcode 4

Tags:

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.

like image 338
the Reverend Avatar asked Jul 11 '11 18:07

the Reverend


2 Answers

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> 
like image 81
Nate Thorn Avatar answered Sep 21 '22 09:09

Nate Thorn


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> 

)

like image 38
brainjam Avatar answered Sep 18 '22 09:09

brainjam