2016-09-30 110 views
0

我希望有人可以帮助我解决我在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得到

+1

'悬而未决的标识符“notesEndpoint''是因为你捆绑使用,你有没有在任何地方声明的变量。 (你在哪里创建'notesEndpoint'?) –

+0

其他错误可能是在某处丢失了一个支架。 –

+2

您需要指出哪些线路正准备给出错误,并确保每条线路都标有完整且准确的错误。 – rmaddy

回答

0

swift3

let notesEndpoint = "http......." 
    let request = URLRequest(url: URL(string: notesEndpoint)!) 
+0

感谢您的答案,但我不认为它的工作。 – Sebbie

2

openNotes功能缺少一个支架,这可能是第2个错误的原因。你应该关闭其他:

func openNotes(success: Bool, error: NSError?) { 
    if let error = error { 
     showAlert(withTitle: "Error", message: error.localizedDescription) 
    } else { 
     performSegue(withIdentifier: "login", sender: self) 
    } 
} 

你的第三个错误"Use of unresolved identifier 'notesEndpoint'"是因为你试图使用尚未在任何地方声明的变量。如果你的下一个教程,我猜你已经错过了这样的地方行:

let notesEndpoint = URL(string: "https://stormpathnotes.herokuapp.com/notes")!

+0

谢谢你:D--这就解决了你说的两个错误。我会回头看看这个教程,看看我能不能弄清楚我做错了什么。这是我正在使用的教程 - https://stormpath.com/blog/build-note-taking-app-swift-ios – Sebbie

+1

Hey Sebbie,这是本教程的作者。当我修改Swift 3的时候,我想我把它遗漏了 - 终点是在'/ notes',所以整行是'let notesEndpoint = URL(string:“https://stormpathnotes.herokuapp.com/notes “)!'。希望这可以帮助! –

+0

@EdwardJiang - 感谢您为我工作:D 现在我试图创建一个后端,所以当用户注册它将保存StormPath数据库中的/ notes。 我有点挣扎在如何做到这一点..我已经检查了教程,但它对我没有什么帮助。 – Sebbie