我正在为我的工作学习职位开发一个应用程序。我一般都是iOS编程的新手,之前从未使用过API。我不知道我在做什么,谷歌的快速启动对我来说也没有用。
我完全跟着Quickstart,现在我在这里不知所措。我到处都有错误,没有任何信息可以修复它们。 You can see the code here和
这是我在Xcode中的代码。
import GoogleAPIClientForREST
import GTMOAuth2
import UIKit
class ViewController: UIViewController {
private let kKeychainItemName = "Google Sheets API"
private let kClientID = "<my key>"
// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
private let scopes = [kGTLRAuthScopeSheetsSpreadsheetsReadonly]
private let service = GTLRSheetsService()
let output = UITextView()
// When the view loads, create necessary subviews
// and initialize the Google Sheets API service
override func viewDidLoad() {
super.viewDidLoad()
output.frame = view.bounds
output.editable = false
output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
output.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
view.addSubview(output);
if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName(
kKeychainItemName,
clientID: kClientID,
clientSecret: nil) {
service.authorizer = auth
}
}
// When the view appears, ensure that the Google Sheets API service is authorized
// and perform API calls
override func viewDidAppear(animated: Bool) {
if let authorizer = service.authorizer,
canAuth = authorizer.canAuthorize where canAuth {
listMajors()
} else {
presentViewController(
createAuthController(),
animated: true,
completion: nil
)
}
}
// Display (in the UITextView) the names and majors of students in a sample
// spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
func listMajors() {
output.text = "Getting sheet data..."
let spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
let range = "Class Data!A2:E"
let query = GTLRSheetsQuery_SpreadsheetsValuesGet
.queryWithSpreadsheetId(spreadsheetId, range:range)
service.executeQuery(query,
delegate: self,
didFinishSelector: "displayResultWithTicket:finishedWithObject:error:"
)
}
// Process the response and display output
func displayResultWithTicket(ticket: GTLRServiceTicket,
finishedWithObject result : GTLRSheets_ValueRange,
error : NSError?) {
if let error = error {
showAlert("Error", message: error.localizedDescription)
return
}
var majorsString = ""
let rows = result.values!
if rows.isEmpty {
output.text = "No data found."
return
}
majorsString += "Name, Major:\n"
for row in rows {
let name = row[0]
let major = row[4]
majorsString += "\(name), \(major)\n"
}
output.text = majorsString
}
// Creates the auth controller for authorizing access to Google Sheets API
private func createAuthController() -> GTMOAuth2ViewControllerTouch {
let scopeString = scopes.joinWithSeparator(" ")
return GTMOAuth2ViewControllerTouch(
scope: scopeString,
clientID: kClientID,
clientSecret: nil,
keychainItemName: kKeychainItemName,
delegate: self,
finishedSelector: "viewController:finishedWithAuth:error:"
)
}
// Handle completion of the authorization process, and update the Google Sheets API
// with the new credentials.
func viewController(vc : UIViewController,
finishedWithAuth authResult : GTMOAuth2Authentication, error : NSError?) {
if let error = error {
service.authorizer = nil
showAlert("Authentication Error", message: error.localizedDescription)
return
}
service.authorizer = authResult
dismissViewControllerAnimated(true, completion: nil)
}
// Helper for showing an alert
func showAlert(title : String, message: String) {
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert
)
let ok = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.Default,
handler: nil
)
alert.addAction(ok)
presentViewController(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我在这里错过了一个图书馆吗?
答案 0 :(得分:0)
您复制/编写的代码似乎是Swift 2.x,但您正在使用Swift 3进行编译。
在Xcode中,编辑 - &gt;转换 - &gt;当前的Swift语法...... 可能会在某种程度上解决。