我正在使用RealmSwift为正在构建的iOS应用程序创建个人识别码对象。 我创建了一个构造函数和一些基本功能来检查引脚,输入新引脚等。 我可以使用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的函数。
如何告诉我我正在尝试将其指向功能?
答案 0 :(得分:1)
已为checkPin
类声明了pinCodeManager
函数,但是您试图为pinCode
对象调用该函数。您需要创建一个pinCodeManager
实例来调用checkPin
。