开发者

Semantic warning on xcode 4

开发者 https://www.devze.com 2023-03-19 06:03 出处:网络
I\'m getting a semantic warning on Xcode 4 : *Declaration of \'struct sockaddr_in\' will not be visible outside of this function*

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 r开发者_开发知识库eachability 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>

)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消