google / google/GoogleSignIn-iOS

Reconsider our usage of dereference and access arrow operator (`->`)

Offen
#232 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Objective-C
Sterne
750
Forks
282
Ø Merge
2 T. 15 Std.
Gemergte PRs (30 T.)
9

Beschreibung

**Is your feature request related to a problem you're having? Please describe.**

We should consider replacing the usage of the arrow operator in our codebase. Doing so would, in many cases, make our code easier to read and in some cases make for less code in general. See the "Additional Context" below.

**Describe the solution you'd like**

Replace the use of `->` with property access through `self`.

**Describe alternatives you've considered**

We could leave things as they are, but this would push the "problem" into the future.

**Additional context**

Here is an example of the enhancement I am thinking of, which arose in conversation during the review of [#230](https://github.com/google/GoogleSignIn-iOS/pull/230#discussion_r983897020). The suggestion below replaces the use of `self->_delegate` with `weakSelf.delegate`. Note that in cases where we are confident that there is no strong reference cycle in the block, we could simply use `self.delegate` as we do in `self->_delegate`. The example below also obviates the need for `GTMAppAuthFetcherAuthorizationEMMChainedDelegate *_retained_self;`, which is used within the preexisting code moved in #230.

```
#pragma mark - GTMAppAuthFetcherAuthorizationEMMChainedDelegate

@interface GTMAppAuthFetcherAuthorizationEMMChainedDelegate ()

@property (weak, nonatomic) id delegate;
@property (nonatomic) SEL selector;

@end

@implementation GTMAppAuthFetcherAuthorizationEMMChainedDelegate

- (instancetype)initWithDelegate:(id)delegate selector:(SEL)selector {
self = [super init];
if (self) {
_delegate = delegate;
_selector = selector;
}
return self;
}

- (void)authentication:(GTMAppAuthFetcherAuthorization *)auth
request:(NSMutableURLRequest *)request
finishedWithError:(nullable NSError *)error {
__weak GTMAppAuthFetcherAuthorizationEMMChainedDelegate *weakSelf = self;
[GIDAppAuthFetcherAuthorizationWithEMMSupport handleTokenFetchEMMError:error
completion:^(NSError *_Nullable error) {
if (weakSelf.delegate || weakSelf.selector) {
return;
}
NSMethodSignature *signature = [weakSelf.delegate methodSignatureForSelector:weakSelf.selector];
if (!signature) {
return;
}
id argument1 = auth;
id argument2 = request;
id argument3 = error;
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:weakSelf.delegate]; // index 0
[invocation setSelector:weakSelf.selector]; // index 1
[invocation setArgument:&argument1 atIndex:2];
[invocation setArgument:&argument2 atIndex:3];
[invocation setArgument:&argument3 atIndex:4];
[invocation invoke];
}];
}

@end
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.