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

标签 swift xcode realm

我正在使用 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/

相关文章:

ios - UICollectionViewCell 中托管的 UITableViewController 的滚动问题

ios - 来自 AVURLAsset 的 URL 的数据 contentsOf 返回 nil

ios - 如何使用 Xcode 7 UI 测试检查表格 View 中的单元格是否可见?

ios - [可能重复]:体系结构i386的 undefined symbol :

ios - 使用 Realm 数据库进行 XCTest 单元测试中的 RLMException

ios - 如何使用 Alamofire 发布复合关键数据参数?

swift - xcode 导航栏按钮状态

XCode - 编辑 xcodeproj 包(特别是 project.pbxproj)

objective-c - 在 Realm 中添加属性,导致项目崩溃

dependencies - RLMLinkingObjects 循环依赖