xcode 4上的语义警告

the*_*end 24 macos cocoa forward-declaration

我在Xcode 4上收到语义警告: *在这个函数之外,'struct sockaddr_in'的声明将不可见* 结构似乎在netinet/in.h中声明

该警告已在Reachability.h上标记,它是我从Apple示例下载的类.

#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
Run Code Online (Sandbox Code Playgroud)

我不明白这个警告,有人能解释一下吗?谢谢.

Nat*_*orn 46

有人提出了bug报告针对的行为,并得到了别人的响应这里.从本质上讲,问题是你在方法的参数中声明了一个新的结构(就编译器可以告诉的那样),因此在其他地方无法访问它.

有一个快速解决方案.只需将以下行添加到Reachability.h:

#import <netinet/in.h>
Run Code Online (Sandbox Code Playgroud)

  • 请参阅@ brainjam的答案,了解更优雅的解决方案,但不会引入头文件膨胀. (2认同)

bra*_*jam 20

您在方法参数中声明了一个新结构,而不是在文件范围.

如果在文件开头添加前向声明(在该@interface部分之前的某个位置),警告将消失.

struct sockaddr_in ;
Run Code Online (Sandbox Code Playgroud)

这样做而不是#import <netinet/in.h>避免头文件膨胀.

(谈到减少标题膨胀,你可以Reachability.h通过替换行来减少标题使用

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
Run Code Online (Sandbox Code Playgroud)

同

#import <SystemConfiguration/SCNetworkReachability.h>
Run Code Online (Sandbox Code Playgroud)

)