【问题标题】:Using RealmSwift to create a pin code, trying to call RealmSwift function使用 RealmSwift 创建密码,尝试调用 RealmSwift 函数
【发布时间】:2019-03-07 01:07:57
【问题描述】:

我正在使用 RealmSwift 为我正在构建的 iOS 应用程序创建一个 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 的函数。

我如何告诉它我试图将它指向一个函数?

【问题讨论】:

    标签: swift xcode realm


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我现在在 checkForExistingPin() 中遇到了一个问题,它的行 let existingCode = realm.objects(pinCode.self) 产生了一个对象,但我正在尝试获取与该对象关联的 pin。
    • 您应该将此标记为已回答并提出一个新问题。不过这次我会在这里回答:realm.objects() 会给你所有的 pinCode 实例。如果您确定您在领域中只有一个,您可以使用.first? 给您一个可选结果,并使用.pin 取消引用它。即realm.objects(pinCode.self).first?.pin
    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多