1

The problem is quite simple.I have a code that works fine in Objective-C, but I need the same code to work in swift.With the code below I am uploading file named"photo.png" to my Google Drive from Documents folder on iPhone.(I deleted the part with setting Documents directory folder to save some space) Here is the original code:

-(void)uploadPhoto{
    metadata.parents = ids;
    GTLRUploadParameters *uploadParameters;
    uploadParameters = [GTLRUploadParameters uploadParametersWithData:photoData MIMEType:@"image/png"];                                                             

    uploadParameters.shouldUploadWithSingleRequest = TRUE;
    GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
                                                                   uploadParameters:uploadParameters];


    query.fields = @"id";

    [self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_File *file,
                                                     NSError *error) {
        if (error == nil) {
            NSLog(@"File photo ID %@", file.identifier);
        } else {
            NSLog(@"An error occurred: %@", error);
        }

    }];

}

Most of it I got form Google developers.IN Objective - Its working fine! Here is what In got in swift:

func uploadPhotos()
{


    var ids : NSArray!
    metadata.parents = ident as! [String]?

    var uploadParameters =  GTLRUploadParameters(data:photoData! as Data,mimeType:"image/png")
    uploadParameters.shouldUploadWithSingleRequest = true
    var query :GTLRDriveQuery_FilesCreate
    query = GTLRDriveQuery_FilesCreate.query(withObject: metadata, uploadParameters: uploadParameters)
    query.fields = "id"

    let vc:DriveListTableViewController


    self.service.executeQuery(query,completionHandler :{(ticket: GTLRServiceTicket!,
                                                      file: GTLRDrive_File!,
                                                     error: Error!)-> Void in
        if error == nil {
            print("File photo ID \(file.identifier)")
        }
        else {
            print("An error occurred: \(error)")
        }
    })



}

The Error I get is in the the line with self.service.exequteQuery

swift:314:59: Cannot convert value of type '(GTLRServiceTicket!, GTLRDrive_File!, Error!) -> Void' to expected argument type 'GTLRServiceCompletionHandler?'

Please help me solve that Error! Have tried different variants!

1 Answers1

0

Refer with this SO thread. You may try changing the unwrapping of the parameters in the closure. Maybe this is required to match what the Objective-C implementation of the function is expecting.

You may also check this additional references:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59