2017-04-10 50 views
1

問題很簡單。我有一個在Objective-C中正常工作的代碼,但我需要相同的代碼才能在swift中工作。 。下面的代碼,我上傳的文檔文件夾命名爲「photo.png」我的谷歌雲端硬盤文件上的iPhone(我刪除了與設置的文檔目錄文件夾,以節省一些空間的部分) 這裏是原代碼:在將Objective-C代碼轉換爲swift 3.0.2後,出現錯誤Google Drive Rest API

-(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); 
     } 

    }]; 

} 

我大部分都是從Google開發者那裏得到的。目標 - 它的工作正常! 以下是在迅速得到:

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)") 
     } 
    }) 



} 

我的錯誤是在該行與self.service.exequteQuery

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

請幫我解決這個錯誤!已嘗試不同的變體!

回答

0

請參考SO thread。你可以嘗試改變閉包中參數的展開。也許這需要與功能的Objective-C實現所期望的一致。

你也可以檢查這個附加參考:

相關問題