gpt4 book ai didi

ios - Swift - 我无法解决的错误

转载 作者:行者123 更新时间:2023-11-28 12:39:35 24 4
gpt4 key购买 nike

我希望有人可以帮助我解决我在 Swift 项目中遇到的这些错误。

我是应用程序编码的新手,所以如果我听起来像菜鸟,请提前道歉。

到目前为止,我已经创建了一个带有登录和注册页面的应用程序,它们全部连接在一起。现在我正在做将所有这些连接在一起的代码部分。

我正在使用名为 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)
}

现在我遇到的错误是:

  • 声明仅在文件范围内有效
  • 预计申报
  • 使用 Unresolved 已识别“notes 端点”

我花了几个小时在 Google 上搜索如何解决这些错误,但我不明白代码有什么问题。如果有人可以指导我解决什么问题,将不胜感激。

错误 -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'

这些是我遇到的错误

最佳答案

您的 openNotes 函数缺少括号,这可能是前两个错误的原因。你应该关闭 else:

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

您的第三个错误 “使用未解析的标识符 'notesEndpoint'” 是因为您正试图使用​​尚未在任何地方声明的变量。如果您正在学习教程,我猜您在某处错过了这样的一行:

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

关于ios - Swift - 我无法解决的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39793308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com