我有一个定义为Singleton的类,我尝试从该类访问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))
}
}
这是一个演示:
谢谢您的时间!
答案 0 :(得分:2)
确保您的BankAccount类文件可用于另一个目标-> ShoppingLandSiri。您可以从文件检查器视图中对其进行检查。
答案 1 :(得分:-1)
您没有正确设置单身人士。 BankAccount
是一个类,而不是一个实例。单例是类的一个版本,其中仅一个实例将被访问,但是您仍在访问一个实例。您需要添加:
class BankAccount {
static let shared = BankAccount()
...
到您的BankAccount类。 shared
属性是实际的单例实例。稍后,当您尝试访问单例时,而不是
BankAccount.withdraw(amount: amount)
您要使用该实例:
BankAccount.shared.withdraw(amount: amount)