瀏覽代碼

Merge pull request #4182 from AFNetworking/init-nullable-specifier-fix

Remove conflicting nullable specifier on init
Jeff Kelley 7 年之前
父節點
當前提交
9e005d9714

+ 7 - 4
AFNetworking/AFNetworkReachabilityManager.h

@@ -108,11 +108,14 @@ NS_ASSUME_NONNULL_BEGIN
 - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability NS_DESIGNATED_INITIALIZER;
 
 /**
- *  Initializes an instance of a network reachability manager
- *
- *  @return nil as this method is unavailable
+ *  Unavailable initializer
  */
-- (nullable instancetype)init NS_UNAVAILABLE;
++ (instancetype)new NS_UNAVAILABLE;
+
+/**
+ *  Unavailable initializer
+ */
+- (instancetype)init NS_UNAVAILABLE;
 
 ///--------------------------------------------------
 /// @name Starting & Stopping Reachability Monitoring

+ 4 - 1
AFNetworking/AFNetworkReachabilityManager.m

@@ -170,8 +170,11 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
     return self;
 }
 
-- (instancetype)init NS_UNAVAILABLE
+- (instancetype)init
 {
+    @throw [NSException exceptionWithName:NSGenericException
+                                   reason:@"`-init` unavailable. Use `-initWithReachability:` instead"
+                                 userInfo:nil];
     return nil;
 }
 

+ 15 - 0
Tests/Tests/AFNetworkReachabilityManagerTests.m

@@ -23,6 +23,7 @@
 
 #import "AFNetworkReachabilityManager.h"
 #import <netinet/in.h>
+#import <objc/message.h>
 
 @interface AFNetworkReachabilityManagerTests : AFTestCase
 @property (nonatomic, strong) AFNetworkReachabilityManager *addressReachability;
@@ -47,6 +48,20 @@
     [super tearDown];
 }
 
+- (void)testInitializerThrowsExceptionWhenCalled {
+    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager alloc];
+    id (*custom_msgSend)(id, SEL) = (id(*)(id, SEL))objc_msgSend;
+
+    XCTAssertThrows(custom_msgSend(manager, @selector(init)));
+}
+
+- (void)testNewThrowsExceptionWhenCalled {
+    id (*custom_msgSend)(id, SEL) = (id(*)(id, SEL))objc_msgSend;
+
+    XCTAssertThrows(custom_msgSend([AFNetworkReachabilityManager class],
+                                   @selector(new)));
+}
+
 - (void)testAddressReachabilityStartsInUnknownState {
     XCTAssertEqual(self.addressReachability.networkReachabilityStatus, AFNetworkReachabilityStatusUnknown,
                    @"Reachability should start in an unknown state");