Help! My Peer Assignment 4 Has No Output

You guys I’ve done everything that is needed in the code and I literally can’t get an output. Also, the button doesn’t convert back to being tappable by the user. I’m turning in my assignment as is because I honestly doubt I’ll get a response back fast enough. Here is a copy of my code:

#import “ViewController.h”
#import <DistanceGetter/DGDistanceRequest.h>

@interface ViewController ()
@property (nonatomic) DGDistanceRequest *req;
@property (weak, nonatomic) IBOutlet UITextField *startLocation;
@property (weak, nonatomic) IBOutlet UITextField *endLocationA;
@property (weak, nonatomic) IBOutlet UILabel *distanceA;
@property (weak, nonatomic) IBOutlet UITextField *endLocationB;
@property (weak, nonatomic) IBOutlet UILabel *distanceB;
@property (weak, nonatomic) IBOutlet UITextField *endLocationC;
@property (weak, nonatomic) IBOutlet UILabel *distanceC;
@property (weak, nonatomic) IBOutlet UIButton *calculateButton;

@end

@implementation ViewController

  • (IBAction)calculateButtonTapped:(id)sender {

{

self.calculateButton.enabled = NO;

self.req = [DGDistanceRequest alloc];
NSString *start = self.startLocation.text;
NSString *destA = self.endLocationA.text;
NSString *destB = self.endLocationB.text;
NSString *destC = self.endLocationC.text;
 NSLog(@"%@\n", start, destA,destB,destC);
 NSArray *dests = @[destA,destB,destC];

self.req = [self.req initWithLocationDescriptions:dests sourceDescription:start];
 
__weak ViewController *weakSelf =self;
 
self.req.callback = ^void(NSArray *responses) {
    
    ViewController *strongSelf = weakSelf;
    
    if(!strongSelf) return;
    
    NSNull *badResult = [NSNull null];
    if(responses [0] != badResult) {
        double num = ([responses[0] floatValue]/1000.0);
NSString *x = [NSString stringWithFormat: @"%.2f km", num];

strongSelf.distanceA.text = x;
strongSelf.distanceB.text = x;
strongSelf.distanceC.text = x;

    }
                       else{
strongSelf.distanceA.text = @"Error";
strongSelf.distanceB.text = @"Error";
strongSelf.distanceC.text =@"Error";
                       };

{
strongSelf.req =nil;

strongSelf.calculateButton.enabled = YES;

};
[strongSelf.req start];
};

};

};

@end

If anyone knows what I’ve done wrong and can correct me I’d really appreciate it! I’d really hate to lose points when I’ve been doing so well this course.

Did you install the Distance Getter Cocoa Pod?

Hi - I’m not sure if this will solve your problem - but it looks like you’ve got too many (unnecessary) open / close curly brackets. I think you could OMIT the 2nd and 6th open bracket, and OMIT the 4th and 5th close bracket.

Also - at the bottom of your code - your attempt to send the “start” message to the request object - seems wrong. I think it should be:
[self.req start] NOT [strongSelf.req start]

If that suggestion is correct, I think that’s the real problem - your request object is never getting created.

If you aren’t using your debugger - I recommend you try to set some breakpoints and run with the debugger. That would allow you to see if certain sections of your code are ever executing.

Hope this helps.

I can’t seem to send an entire copy of your code back to you -
but to try to be more clear - these are the open / close brackets I think you should OMIT - indicated by comments in the various segments of code below:

(IBAction)calculateButtonTapped:(id)sender {

{ // OMIT 2nd open bracket - right after declaring your calculateButtonTapped method

		self.calculateButton.enabled = NO;

               {    //  OMIT 6th open bracket that is in your code just before setting strongSelf.req to nil
		strongSelf.req =nil;

		[strongSelf.req start];   // this should be [self.req start]

}; // OMIT 4th close bracket

}; // OMIT 5th close bracket

};

@end

1 Like