Selaa lähdekoodia

Merge pull request #352 from facebook/nlutsenko.instancetype

Make all initializers subclass/type-safe friendly.
Nikita Lutsenko 9 vuotta sitten
vanhempi
commit
ef22e3a488

+ 13 - 13
SRWebSocketTests/SRTAutobahnTests.m

@@ -22,12 +22,12 @@
 @end
 
 @interface TestOperation : SRTWebSocketOperation <SRWebSocketDelegate>
-- (id)initWithBaseURL:(NSURL *)url testNumber:(NSInteger)testNumber agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url testNumber:(NSInteger)testNumber agent:(NSString *)agent;
 @end
 
 
 @interface CaseGetterOperation : SRTWebSocketOperation <SRWebSocketDelegate>
-- (id)initWithBaseURL:(NSURL *)url;
+- (instancetype)initWithBaseURL:(NSURL *)url;
 
 @property (nonatomic, readonly) NSInteger caseCount;
 
@@ -41,7 +41,7 @@
 
 @interface SRTBlockInvoker
 
-- (id)initWithBlock:(dispatch_block_t)block;
+- (instancetype)initWithBlock:(dispatch_block_t)block;
 
 - (void)invoke;
 
@@ -49,7 +49,7 @@
 
 @interface UpdateOperation : SRTWebSocketOperation <SRWebSocketDelegate>
 
-- (id)initWithBaseURL:(NSURL *)url agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url agent:(NSString *)agent;
 
 @end
 
@@ -57,7 +57,7 @@
 
 @property (nonatomic) NSDictionary *info;
 
-- (id)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber;
+- (instancetype)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber;
 
 @end
 
@@ -65,7 +65,7 @@
 
 @property (nonatomic) NSDictionary *info;
 
-- (id)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber agent:(NSString *)agent;
 
 @end
 
@@ -80,7 +80,7 @@
     NSString *_description;
 }
 
-- (id)initWithInvocation:(NSInvocation *)anInvocation description:(NSString *)description;
+- (instancetype)initWithInvocation:(NSInvocation *)anInvocation description:(NSString *)description;
 {
     self = [self initWithInvocation:anInvocation];
     if (self) {
@@ -89,7 +89,7 @@
     return self;
 }
 
-- (id)initWithInvocation:(NSInvocation *)anInvocation;
+- (instancetype)initWithInvocation:(NSInvocation *)anInvocation;
 {
     self = [super initWithInvocation:anInvocation];
     if (self) {
@@ -233,7 +233,7 @@
     NSInteger _testNumber;
 }
 
-- (id)initWithBaseURL:(NSURL *)url testNumber:(NSInteger)testNumber agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url testNumber:(NSInteger)testNumber agent:(NSString *)agent;
 {   
     
     NSString *path = [[url URLByAppendingPathComponent:@"runCase"] absoluteString];
@@ -258,7 +258,7 @@
 
 @synthesize caseCount = _caseCount;
 
-- (id)initWithBaseURL:(NSURL *)url;
+- (instancetype)initWithBaseURL:(NSURL *)url;
 {
     self = [super initWithURL:[url URLByAppendingPathComponent:@"getCaseCount"]];
     if (self) {
@@ -276,7 +276,7 @@
 
 @implementation UpdateOperation
 
-- (id)initWithBaseURL:(NSURL *)url agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url agent:(NSString *)agent;
 {
     NSString *path = [[url URLByAppendingPathComponent:@"updateReports"] absoluteString];
     path = [path stringByAppendingFormat:@"?agent=%@", agent];
@@ -297,7 +297,7 @@
 
 @synthesize info = _info;
 
-- (id)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber;
+- (instancetype)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber;
 {
     NSString *path = [[url URLByAppendingPathComponent:@"getCaseInfo"] absoluteString];
     path = [path stringByAppendingFormat:@"?case=%@", @(caseNumber)];
@@ -317,7 +317,7 @@
 
 @synthesize info = _info;
 
-- (id)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber agent:(NSString *)agent;
+- (instancetype)initWithBaseURL:(NSURL *)url caseNumber:(NSInteger)caseNumber agent:(NSString *)agent;
 {
     NSString *path = [[url URLByAppendingPathComponent:@"getCaseStatus"] absoluteString];
     path = [path stringByAppendingFormat:@"?case=%@&agent=%@", @(caseNumber), agent];

+ 1 - 1
SRWebSocketTests/SRTWebSocketOperation.h

@@ -12,7 +12,7 @@
 
 @interface SRTWebSocketOperation : NSOperation <SRWebSocketDelegate>
 
-- (id)initWithURL:(NSURL *)URL;
+- (instancetype)initWithURL:(NSURL *)URL;
 
 @property (nonatomic) BOOL isFinished;
 @property (nonatomic) BOOL isExecuting;

+ 1 - 1
SRWebSocketTests/SRTWebSocketOperation.m

@@ -22,7 +22,7 @@
 @synthesize isExecuting = _isExecuting;
 @synthesize error = _error;
 
-- (id)initWithURL:(NSURL *)URL;
+- (instancetype)initWithURL:(NSURL *)URL;
 {
     self = [super init];
     if (self) {

+ 6 - 6
SocketRocket/SRWebSocket.h

@@ -72,14 +72,14 @@ extern NSString *const SRHTTPResponseErrorKey;
 @property (nonatomic, readonly, copy) NSString *protocol;
 
 // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol.
-- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
-- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
-- (id)initWithURLRequest:(NSURLRequest *)request;
+- (instancetype)initWithURLRequest:(NSURLRequest *)request;
+- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
+- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
 
 // Some helper constructors.
-- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
-- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
-- (id)initWithURL:(NSURL *)url;
+- (instancetype)initWithURL:(NSURL *)url;
+- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
+- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
 
 // Delegate queue will be dispatch_main_queue by default.
 // You cannot set both OperationQueue and dispatch_queue.

+ 49 - 57
SocketRocket/SRWebSocket.m

@@ -163,7 +163,7 @@ typedef void (^data_callback)(SRWebSocket *webSocket,  NSData *data);
 // This class is not thread-safe, and is expected to always be run on the same queue.
 @interface SRIOConsumerPool : NSObject
 
-- (id)initWithBufferCapacity:(NSUInteger)poolSize;
+- (instancetype)initWithBufferCapacity:(NSUInteger)poolSize;
 
 - (SRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
 - (void)returnConsumer:(SRIOConsumer *)consumer;
@@ -256,85 +256,77 @@ static __strong NSData *CRLFCRLF;
     CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
 }
 
-- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
+- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
 {
     self = [super init];
-    if (self) {
-        assert(request.URL);
-        _url = request.URL;
-        _urlRequest = request;
-        _allowsUntrustedSSLCertificates = allowsUntrustedSSLCertificates;
-        
-        _requestedProtocols = [protocols copy];
-        
-        [self _SR_commonInit];
-    }
-    
-    return self;
-}
-
-- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
-{
-    return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:NO];
-}
-
-- (id)initWithURLRequest:(NSURLRequest *)request;
-{
-    return [self initWithURLRequest:request protocols:nil];
-}
-
-- (id)initWithURL:(NSURL *)url;
-{
-    return [self initWithURL:url protocols:nil];
-}
+    if (!self) return self;
 
-- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
-{
-    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];    
-    return [self initWithURLRequest:request protocols:protocols];
-}
+    assert(request.URL);
+    _url = request.URL;
+    _urlRequest = request;
+    _allowsUntrustedSSLCertificates = allowsUntrustedSSLCertificates;
 
-- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
-{
-    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
-    return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:allowsUntrustedSSLCertificates];
-}
+    _requestedProtocols = [protocols copy];
 
-- (void)_SR_commonInit;
-{
     NSString *scheme = _url.scheme.lowercaseString;
     assert([scheme isEqualToString:@"ws"] || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]);
-    
+
     if ([scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]) {
         _secure = YES;
     }
-    
+
     _readyState = SR_CONNECTING;
     _consumerStopped = YES;
     _webSocketVersion = 13;
-    
+
     _workQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
-    
+
     // Going to set a specific on the queue so we can validate we're on the work queue
     dispatch_queue_set_specific(_workQueue, (__bridge void *)self, maybe_bridge(_workQueue), NULL);
-    
+
     _delegateDispatchQueue = dispatch_get_main_queue();
     sr_dispatch_retain(_delegateDispatchQueue);
-    
+
     _readBuffer = [[NSMutableData alloc] init];
     _outputBuffer = [[NSMutableData alloc] init];
-    
+
     _currentFrameData = [[NSMutableData alloc] init];
 
     _consumers = [[NSMutableArray alloc] init];
-    
+
     _consumerPool = [[SRIOConsumerPool alloc] init];
-    
+
     _scheduledRunloops = [[NSMutableSet alloc] init];
-    
+
     [self _initializeStreams];
-    
-    // default handlers
+
+    return self;
+}
+
+- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols
+{
+    return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:NO];
+}
+
+- (instancetype)initWithURLRequest:(NSURLRequest *)request
+{
+    return [self initWithURLRequest:request protocols:nil];
+}
+
+- (instancetype)initWithURL:(NSURL *)url;
+{
+    return [self initWithURL:url protocols:nil];
+}
+
+- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
+{
+    return [self initWithURL:url protocols:protocols allowsUntrustedSSLCertificates:NO];
+}
+
+- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
+{
+    NSURLRequest *request = [NSURLRequest requestWithURL:url];
+    return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:allowsUntrustedSSLCertificates];
 }
 
 - (void)assertOnWorkQueue;
@@ -1664,7 +1656,7 @@ static const size_t SRFrameHeaderOverhead = 32;
     NSMutableArray *_bufferedConsumers;
 }
 
-- (id)initWithBufferCapacity:(NSUInteger)poolSize;
+- (instancetype)initWithBufferCapacity:(NSUInteger)poolSize;
 {
     self = [super init];
     if (self) {
@@ -1674,7 +1666,7 @@ static const size_t SRFrameHeaderOverhead = 32;
     return self;
 }
 
-- (id)init
+- (instancetype)init
 {
     return [self initWithBufferCapacity:8];
 }
@@ -1866,7 +1858,7 @@ static NSRunLoop *networkRunLoop = nil;
     sr_dispatch_release(_waitGroup);
 }
 
-- (id)init
+- (instancetype)init
 {
     self = [super init];
     if (self) {

+ 1 - 1
TestChat/TCChatCell.m

@@ -13,7 +13,7 @@
 @synthesize nameLabel = _nameLabel;
 @synthesize textView = _textView;
 
-- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
+- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {

+ 2 - 2
TestChat/TCViewController.m

@@ -12,7 +12,7 @@
 
 @interface TCMessage : NSObject
 
-- (id)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
+- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
 
 @property (nonatomic, retain, readonly) NSString *message;
 @property (nonatomic, readonly)  BOOL fromMe;
@@ -173,7 +173,7 @@
 @synthesize message = _message;
 @synthesize fromMe = _fromMe;
 
-- (id)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
+- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
 {
     self = [super init];
     if (self) {