gpt4 book ai didi

swift - 使用 RealmSwift 创建密码,尝试调用 RealmSwift 函数

转载 作者:行者123 更新时间:2023-11-28 13:48:47 26 4
gpt4 key购买 nike

我正在使用 RealmSwift 为我正在构建的 iOS 应用程序创建个人识别码对象。我已经创建了一个构造函数和一些基本函数来检查 pin、输入新 pin 等。我可以使用在 RealmSwift 中创建的 pin 对象设置一个新的 pin,但我在检查它时遇到了问题。这是 RealmSwift 部分:

import Foundation
import RealmSwift

class pinCode: Object {
@objc dynamic var pin = ""
}

protocol pinCodeManager {
func checkForExistingPin() -> Bool
func enterNewPin(newPin:String)
func checkPin(pin:String) -> Bool
}

class manager:pinCodeManager {
let realm = try! Realm()

func checkForExistingPin() -> Bool {

let existingCode = realm.objects(pinCode.self)
if existingCode.count == 0 {
return false
}
else {
return true
}
}

func enterNewPin(newPin:String) {
if checkForExistingPin() {
let oldCode = realm.objects(pinCode.self).first
try! realm.write {
oldCode!.pin = newPin
}
}
let newPinObject = pinCode()
newPinObject.pin = newPin
realm.add(newPinObject)
}

func checkPin(pin:String) -> Bool {
if checkForExistingPin() {
if pin == realm.objects(pinCode.self).first?.pin {
return true
}
else {
return false
}
}
return false
}
}

这是 ViewController 部分

import UIKit

class InitialViewController: UIViewController {
var currentPinCode = ""
var pinEntered = ""
var firstPinEntered = ""
var secondPinEntered = ""
let myPin = pinCode()

@IBOutlet weak var enterPinCodeField: UITextField!

@IBAction func GoButton(_ sender: Any) {
let enteredPin = enterPinCodeField?.text
if self.myPin.checkPin(pin: enteredPin) {
print ("Correct Pin")
}
else {
print ("Incorrect Pin")
}
}

@IBAction func NewUserButton(_ sender: Any) {
print ("New user selected!")

let pinCodeAlert = UIAlertController(title: "Enter New PIN", message: "", preferredStyle: .alert)
pinCodeAlert.addTextField (configurationHandler:{textField1 in

textField1.keyboardType = .numberPad
textField1.placeholder = "Enter new PIN"
textField1.isSecureTextEntry = true
})

let okAction = UIAlertAction(title: "OK", style: .cancel) {(action) in
let firstPinEntry = pinCodeAlert.textFields?.first
print ("First PIN entered: " , firstPinEntry!.text)
self.confirmPin(firstPin: firstPinEntry!.text!)
}

pinCodeAlert.addAction(okAction)
self.present(pinCodeAlert, animated: true, completion: nil)
}


func confirmPin(firstPin: String) {
let pinCodeAlert2 = UIAlertController(title: "Re-enter New PIN", message: "", preferredStyle: .alert)
pinCodeAlert2.addTextField (configurationHandler:{textField1 in

textField1.keyboardType = .numberPad
textField1.placeholder = "Re-enter new PIN"
textField1.isSecureTextEntry = true


})
let okAction2 = UIAlertAction(title: "OK", style: .cancel) {(action) in
let secondPinEntered = pinCodeAlert2.textFields?.first
print ("2nd PIN entered: " , secondPinEntered?.text! as Any)

if firstPin != secondPinEntered?.text! {
print("PINs dont match!")
let pinCodesDontMatch = UIAlertController(title: "PINs don't match!", message: "", preferredStyle: .alert)


let okAction3 = UIAlertAction(title: "OK", style: .cancel) {(action) in
}
pinCodesDontMatch.addAction(okAction3)
self.present(pinCodesDontMatch, animated: true, completion: nil)
}
else {
let newPinSet = UIAlertController(title: "New PIN Set", message: "", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel) {(action) in
}
newPinSet.addAction(okAction)
self.present(newPinSet, animated: true, completion: nil)
self.myPin.pin = String((secondPinEntered?.text)!)
}
}


pinCodeAlert2.addAction(okAction2)
self.present(pinCodeAlert2, animated: true, completion: nil)
}

@IBOutlet weak var PinCodeField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

我遇到的问题是:

 if self.myPin.checkPin(pin: enteredPin)

我尝试了一些变体,但没有成功。

我得到的错误是“‘pinCode’类型的值没有成员‘checkPin’”所以我得到的印象是它正在寻找一个成员而不是一个名为 checkPin 的函数。

我如何告诉它我正在尝试将它指向一个函数?

最佳答案

您的 checkPin 函数已为 pinCodeManager 类声明,但您正试图为 pinCode 对象调用该函数。您需要创建一个 pinCodeManager 实例来调用 checkPin

关于swift - 使用 RealmSwift 创建密码,尝试调用 RealmSwift 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55034563/

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