Search code examples
iosswiftswift4sirikit

How to fix the error "Use of Unresolved identifier" when actually I can access that identifier


I have a class which is defined as a Singleton and I try to access 2 functions from that class but I get an error which is saying that class can't be found but when I press Cmd + Click I'm able to navigate to that class. I restarted xCode so many times, also I tried with xCode 10 and xCode 9...the same error. I don't know how to fix it.

Here is my code:

// First Class
class BankAccount {

    private init() {}

    static let bankAccountKey = "Bank Account"
    static let suiteName = "group.com.YourName"

    // Function to set the balance for ShoppingLand Bank
    static func setBalance(toAmount amount: Double) {
        guard let defaults = UserDefaults(suiteName: suiteName) else { return }
        defaults.set(amount, forKey: bankAccountKey)
        defaults.synchronize()
    }

    // Function to check new updates about the balance of ShoppingLand Bank
    static func checkBalance() -> Double? {
        guard let defaults = UserDefaults(suiteName: suiteName) else { return nil }
        defaults.synchronize()
        let balance = defaults.double(forKey: bankAccountKey)
        return balance
    }

    @discardableResult
    static func withdraw(amount: Double) -> Double? {
        guard let defaults = UserDefaults(suiteName: suiteName) else { return nil }
        let balance = defaults.double(forKey: bankAccountKey)
        let newBalance = balance - amount
        setBalance(toAmount: newBalance)
        return newBalance
    }

    @discardableResult
    static func deposit(amount: Double) -> Double? {
        guard let defaults = UserDefaults(suiteName: suiteName) else { return nil }
        let balance = defaults.double(forKey: bankAccountKey)
        let newBalance = balance + amount
        setBalance(toAmount: newBalance)
        return newBalance
    }
}

// Second Class

import Intents

class IntentHandler: INExtension {}

extension IntentHandler: INSendPaymentIntentHandling {
    func handle(intent: INSendPaymentIntent, completion: @escaping (INSendPaymentIntentResponse) -> Void) {

        guard let amount = intent.currencyAmount?.amount?.doubleValue else {
            completion(INSendPaymentIntentResponse(code: .failure, userActivity: nil))
            return
        }

        BankAccount.withdraw(amount: amount)
        completion(INSendPaymentIntentResponse(code: .success, userActivity: nil))
    }
}


extension IntentHandler: INRequestPaymentIntentHandling {
    func handle(intent: INRequestPaymentIntent, completion: @escaping (INRequestPaymentIntentResponse) -> Void) {

        guard let amount = intent.currencyAmount?.amount?.doubleValue else {
            completion(INRequestPaymentIntentResponse(code: .failure, userActivity: nil))
            return
        }
        BankAccount.deposit(amount: amount)
        completion(INRequestPaymentIntentResponse(code: .success, userActivity: nil))

    }
}

Here is a demo:

http://recordit.co/NoXKlT3dw1

Thank you for your time !


Solution

  • Make sure your class file of BankAccount is available for your other target -> ShoppingLandSiri. you can check it from the file inspector view.