the Reverend
the Reverend

Reputation: 12559

Semantic warning on xcode 4

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.

Upvotes: 24

Views: 5464

Answers (3)

brainjam
brainjam

Reputation: 19005

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>

)

Upvotes: 20

Umair Aamir
Umair Aamir

Reputation: 1644

Add #import in Reachability.h to get away with this

Upvotes: 1

Nate Thorn
Nate Thorn

Reputation: 2183

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>

Upvotes: 46

Related Questions