如何修复错误"Use of Unresolved identifier"实际上何时可以访问该标识符



我有一个定义为单例的类,我尝试从该类访问 2 个函数,但我收到一个错误,说找不到该类,但是当我按 Cmd + 单击时,我能够导航到该类。 我重新启动了xCode很多次,我也尝试了xCode 10和xCode 9......同样的错误。我不知道如何解决它。

这是我的代码:

// 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))
}
}

这是一个演示:

http://recordit.co/NoXKlT3dw1

谢谢你的时间!

确保您的银行帐户类文件可用于您的其他目标 ->购物乐园Siri。 您可以从文件检查器视图中检查它。

您尚未正确设置单例。BankAccount是一个类,而不是一个实例。单一实例是类的一个版本,其中将只访问一个实例,但您仍在访问一个实例。您需要添加:

class BankAccount {
static let shared = BankAccount()
...

到您的银行账户类。shared属性是实际的单一实例。稍后当您尝试访问单例时,而不是

BankAccount.withdraw(amount: amount)

您希望使用该实例:

BankAccount.shared.withdraw(amount: amount)

相关内容

最新更新