Fix nullability warnings/errors

Summary: ^

Reviewed By: passy

Differential Revision: D50640020

fbshipit-source-id: d42b938520203a6ce232717c1adc43da176457e3
This commit is contained in:
Lorenzo Blasa
2023-10-25 04:44:01 -07:00
committed by Facebook GitHub Bot
parent da7917cefe
commit 7f6d1cf55b
2 changed files with 59 additions and 35 deletions

View File

@@ -19,25 +19,32 @@
} }
- (IBAction)tappedGithubLitho:(UIButton*)sender { - (IBAction)tappedGithubLitho:(UIButton*)sender {
[[[NSURLSession sharedSession] NSURL* url = [NSURL
dataTaskWithURL: URLWithString:
[NSURL @"https://raw.githubusercontent.com/facebook/litho/main/docs/static/logo.png"];
URLWithString: if (!url) {
@"https://raw.githubusercontent.com/facebook/litho/main/docs/static/logo.png"] return;
completionHandler:^( }
NSData* _Nullable data,
NSURLResponse* _Nullable response, [[[NSURLSession sharedSession] dataTaskWithURL:url
NSError* _Nullable error) { completionHandler:^(
if (error && !data) { NSData* _Nullable data,
return; NSURLResponse* _Nullable response,
} NSError* _Nullable error) {
NSLog(@"Got Image"); if (error && !data) {
}] resume]; return;
}
NSLog(@"Got Image");
}] resume];
} }
- (IBAction)tappedPOSTAPI:(UIButton*)sender { - (IBAction)tappedPOSTAPI:(UIButton*)sender {
NSString* post = @"https://demo9512366.mockable.io/FlipperPost"; NSURL* url =
NSURL* url = [NSURL URLWithString:post]; [NSURL URLWithString:@"https://demo9512366.mockable.io/FlipperPost"];
if (!url) {
return;
}
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url]; NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"]; [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
@@ -57,42 +64,56 @@
[[[NSURLSession sharedSession] [[[NSURLSession sharedSession]
dataTaskWithRequest:urlRequest dataTaskWithRequest:urlRequest
completionHandler:^( completionHandler:^(
NSData* _Nullable data, NSData* data,
NSURLResponse* _Nullable response, NSURLResponse* _Nullable response,
NSError* _Nullable dataTaskError) { NSError* _Nullable dataTaskError) {
if (dataTaskError || !data) { if (dataTaskError) {
[weakSelf [weakSelf
showAlertWithMessage:@"Received error in POST API response"]; showAlertWithMessage:@"Received error in POST API response"];
return; return;
} }
NSDictionary* dict = if (data == nil) {
[NSJSONSerialization JSONObjectWithData:data [weakSelf
options:0 showAlertWithMessage:@"No data received in POST API response"];
error:&dataTaskError]; } else {
NSLog(@"MSG-POST: %@", dict[@"msg"]); NSDictionary* dict =
[NSJSONSerialization JSONObjectWithData:data
options:0
error:&dataTaskError];
NSLog(@"MSG-POST: %@", dict[@"msg"]);
[weakSelf showAlertWithMessage:@"Received response from POST API"]; [weakSelf showAlertWithMessage:@"Received response from POST API"];
}
}] resume]; }] resume];
} }
- (IBAction)tappedGetAPI:(UIButton*)sender { - (IBAction)tappedGetAPI:(UIButton*)sender {
NSURL* url =
[NSURL URLWithString:@"https://demo9512366.mockable.io/FlipperGet"];
if (!url) {
return;
}
__weak NetworkViewController* weakSelf = self; __weak NetworkViewController* weakSelf = self;
[[[NSURLSession sharedSession] [[[NSURLSession sharedSession]
dataTaskWithURL: dataTaskWithURL:url
[NSURL URLWithString:@"https://demo9512366.mockable.io/FlipperGet"]
completionHandler:^( completionHandler:^(
NSData* _Nullable data, NSData* data,
NSURLResponse* _Nullable response, NSURLResponse* _Nullable response,
NSError* _Nullable error) { NSError* _Nullable error) {
if (error || !data) { if (error) {
[weakSelf showAlertWithMessage:@"Received error in GET API response"]; [weakSelf showAlertWithMessage:@"Received error in GET API response"];
return; return;
} }
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data if (data == nil) {
options:0 [weakSelf
error:&error]; showAlertWithMessage:@"No data received in GET API response"];
NSLog(@"MSG-GET: %@", dict[@"msg"]); } else {
[weakSelf showAlertWithMessage:@"Received response from GET API"]; NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
NSLog(@"MSG-GET: %@", dict[@"msg"]);
[weakSelf showAlertWithMessage:@"Received response from GET API"];
}
}] resume]; }] resume];
} }

View File

@@ -23,8 +23,11 @@
} }
- (IBAction)tappedSave:(id)sender { - (IBAction)tappedSave:(id)sender {
[self.userDefaults setObject:self.valueTextField.text NSString* key = self.keyTextField.text;
forKey:self.keyTextField.text]; NSString* value = self.valueTextField.text;
if (key != nil) {
[self.userDefaults setObject:value forKey:key];
}
} }
@end @end