我希望有人可以幫助我解決我在Swift項目中遇到的這些錯誤。Swift - 我無法解決的錯誤
我很新的應用程序編碼,所以如果我聽起來像一個noob提前抱歉。
到目前爲止,我已經創建了一個應用程序,它包含一個連接在一起的登錄和註冊頁面。現在我正在執行將所有這些連接在一起的代碼部分。
我正在使用名爲StormPath的用戶管理系統,這就是編碼完成的方式。
LoginViewController.Swift
import UIKit
import Stormpath
class LoginViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBAction func login(_ sender: AnyObject) {
// Code when someone presses the login button
Stormpath.sharedSession.login(emailTextField.text!, password: passwordTextField.text!, completionHandler: openNotes)
}
@IBAction func loginWithFacebook(_ sender: AnyObject) {
// Code when someone presses the login with Facebook button
}
@IBAction func loginWithGoogle(_ sender: AnyObject) {
// Code when someone presses the login with Google button
}
@IBAction func resetPassword(_ sender: AnyObject) {
// Code when someone presses the reset password button
Stormpath.sharedSession.resetPassword(emailTextField.text!) { (success, error) -> Void in
if let error = error {
**self.showAlert(withTitle: "Error", message: error.localizedDescription)**
} else {
**self.showAlert(withTitle: "Success", message: "Password reset email sent!")**
}
}
}
func openNotes(success: Bool, error: NSError?) {
if let error = error {
**showAlert(withTitle: "Error", message: error.localizedDescription)**
}else {
performSegue(withIdentifier: "login", sender: self)
}
}
// Helper extension to display alerts easily.
**extension UIViewController {**
func showAlert(withTitle title: String, message: String?) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
RegisterViewController.Swift
import UIKit
import Stormpath
class RegisterViewController: UIViewController {
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: .exit)
}
func exit() {
dismiss(animated: true, completion: nil)
}
@IBAction func register(_ sender: AnyObject) {
// Code for registering the user
let newUser = RegistrationModel(email: emailTextField.text!, password: passwordTextField.text!)
newUser.givenName = firstNameTextField.text!
newUser.surname = lastNameTextField.text!
// Register the new user
Stormpath.sharedSession.register(newUser) { (account, error) -> Void in
if let error = error {
**self.showAlert(withTitle: "Error", message: error.localizedDescription)**
} else {
self.exit()
}
}
}
}
private extension Selector {
static let exit = #selector(RegisterViewController.exit)
}
NotesViewController.swift(還沒完成編碼)
import UIKit
import Stormpath
class NotesViewController: UIViewController {
@IBOutlet weak var helloLabel: UILabel!
@IBOutlet weak var notesTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: .keyboardWasShown, name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: .keyboardWillBeHidden, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Place code to load data here
Stormpath.sharedSession.me { (account, error) -> Void in
if let account = account {
self.helloLabel.text = "Hello \(account.fullName)!"
}
}
var request = URLRequest(url: notesEndpoint)
request.setValue("Bearer \(Stormpath.sharedSession.accessToken ?? "")", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
guard let data = data, let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any], let notes = json["notes"] as? String else {
return
}
DispatchQueue.main.async(execute: {
self.notesTextView.text = notes
})
})
task.resume()
}
@IBAction func logout(_ sender: AnyObject) {
// Code when someone presses the logout button
Stormpath.sharedSession.logout()
dismiss(animated: false, completion: nil)
}
func keyboardWasShown(_ notification: Notification) {
if let keyboardRect = ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue {
notesTextView.contentInset = UIEdgeInsetsMake(0, 0, keyboardRect.size.height, 0)
notesTextView.scrollIndicatorInsets = notesTextView.contentInset
}
}
func keyboardWillBeHidden(_ notification: Notification) {
notesTextView.contentInset = UIEdgeInsets.zero
notesTextView.scrollIndicatorInsets = UIEdgeInsets.zero
}
}
extension NotesViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
// Add a "Save" button to the navigation bar when we start editing the
// text field.
**let postBody = ["notes": notesTextView.text]
var request = URLRequest(url: notesEndpoint)**
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: postBody, options: [])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(Stormpath.sharedSession.accessToken ?? "")", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request)
task.resume()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: .stopEditing)
}
func stopEditing() {
// Remove the "Save" button, and close the keyboard.
navigationItem.rightBarButtonItem = nil
notesTextView.resignFirstResponder()
}
func textViewDidEndEditing(_ textView: UITextView) {
// Code when someone exits out of the text field
}
}
private extension Selector {
static let keyboardWasShown = #selector(NotesViewController.keyboardWasShown(_:))
static let keyboardWillBeHidden = #selector(NotesViewController.keyboardWillBeHidden(_:))
static let stopEditing = #selector(NotesViewController.stopEditing)
}
現在我具有錯誤是:
- 聲明只在文件範圍內
- 預計聲明
- 使用的未解決的認定有效「指出端點」
我一直在就如何解決在谷歌,但我這些錯誤尋找小時不明白代碼有什麼問題。如果有人能指導我解決什麼問題,我們將非常感激。
錯誤 - NotesViewController.Swift - 錯誤=
var request = URLRequest(url: notesEndpoint) - Use of unresolved identifier 'notesEndPoint'
LoginViewController.Swift - 錯誤=
self.showAlert(withTitle: "Error", message: error.localizedDescription) - Value of type 'LoginViewController' has no member 'showAlert'
performSegue(withIdentifier: "login", sender: self) - Implicit use of 'self' in closure; use 'self'. to make capture semantics explicit
showAlert(withTitle: "Error", message: error.localizedDescription) - Use of unresolved identifier 'showAlert'
extension UIViewController { -Declaration is only valid at file scope
RegisterViewController.swift -
self.showAlert(withTitle: "Error", message: error.localizedDescription) - Value of type 'RegisterViewController' has no member 'showAlert'
這些是I」的誤差m得到
'懸而未決的標識符「notesEndpoint''是因爲你捆綁使用,你有沒有在任何地方聲明的變量。 (你在哪裏創建'notesEndpoint'?) –
其他錯誤可能是在某處丟失了一個支架。 –
您需要指出哪些線路正準備給出錯誤,並確保每條線路都標有完整且準確的錯誤。 – rmaddy