0

Updated:

I am trying to call a method from a different class and put it in an if statement saying "if the uploadPhoto method is true then display success for testing purposes" here is my updated code:

PhotoScreen *script = [[PhotoScreen alloc] init];

if ([script uploadPhoto])
{
    NSLog(@"Sucess!");
}

It isn't giving me the error anymore but when I summit the photo from the uploadPhoto method, it does't log the "Success" And here is my uploadPhoto method:

- (BOOL)uploadPhoto
{
    //upload the image and the title to the web service
    [[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:
      @"upload",@"command",
      UIImageJPEGRepresentation(photo.image,70),@"file",
      fldTitle.text, @"title",nil]
      onCompletion:^(NSDictionary *json)
      {
          //completion
         if (![json objectForKey:@"error"])
         {
             //success
             [[[UIAlertView alloc]initWithTitle:@"Success!"
                message:@"Your photo is uploaded"
                delegate:nil cancelButtonTitle:@"Yay!"
                otherButtonTitles: nil] show];
         }
         else
         {
             //error, check for expired session and if so - authorize the user
             NSString* errorMsg = [json objectForKey:@"error"];
             [UIAlertView error:errorMsg];

             if ([@"Authorization required" compare:errorMsg]==NSOrderedSame) 
             {
                 [self performSegueWithIdentifier:@"ShowLogin" sender:nil];
             }
         }
     }];
     return YES;
}

What am I doing wrong?

klcjr89
  • 5,862
  • 10
  • 58
  • 91
  • 2
    Does your method return a `BOOL`? Also why are you calling it twice? Also you can just put `[script uploadPhoto]` in the if statement. – carloabelli Jul 16 '14 at 03:31
  • I just changed the method to return a BOOL and it isn't giving me an error anymore but it's not diplaying the "Sucess" when I try to upload a photo. Is there a way I can make it where: once the method is called then display the text? – user3807138 Jul 16 '14 at 03:37
  • 2
    It's only going to log "success" if your `uploadPhoto` method returns `YES` (BTW - only use `YES` or `NO` for `BOOL` values). Perhaps you should post your `uploadPhoto` method in your question. – rmaddy Jul 16 '14 at 03:41
  • This method `uploadPhoto` doesn't seem to return anything. Then you will get an undefined value in comparison expression. – eonil Jul 16 '14 at 04:09
  • Have you used "return YES;" on success and "return NO;" on failure ? – Yogendra Jul 16 '14 at 04:10
  • 1
    @Student This is an asynchronous operation, and we cannot return the result immediately as a return value of this method. – eonil Jul 16 '14 at 04:10
  • Yeah I just put RETURN YES: at the end of the method and it's still not working :/ – user3807138 Jul 16 '14 at 04:12
  • @user3807138 Try to print "failure!" by adding an `else` block. And tell us what happens. – eonil Jul 16 '14 at 04:20
  • 1
    See also similar questions: [Return result of completion block](http://stackoverflow.com/q/12095784), [Why can't I return an object from my block?](http://stackoverflow.com/q/8436750), [Completion handlers and return values](http://stackoverflow.com/q/16254756) – jscs Jul 16 '14 at 04:22

1 Answers1

1

You method needs to return a BOOL. In your method you should have the statement return YES; somewhere if the method succeeds and a return NO; if it fails. You can modify your code to the following:

PhotoScreen *script = [[PhotoScreen alloc] init];

if ([script uploadPhoto])
{
    NSLog(@"Sucess!");
}

The repetitive method calls were unnecessary along with the comparison.

carloabelli
  • 4,289
  • 3
  • 43
  • 70