Я использую RealmSwift для создания объекта пин-кода для приложения iOS, которое я создаю.
Я создал конструктор и несколько основных функций для проверки вывода, ввода нового вывода и т. Д.
Я могу установить новый вывод, используя объект вывода, созданный в RealmSwift, но у меня возникают проблемы с его проверкой.
Вот часть 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.
Как мне сказать, что я пытаюсь указать на функцию?