【发布时间】:2018-07-04 18:04:23
【问题描述】:
我有一个定义为 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))
}
}
这是一个演示:
感谢您的宝贵时间!
【问题讨论】:
-
请指出哪些行给你错误。
-
请点击Demo看看是什么错误。是说找不到类 BankAccount。
-
我没有点击一些奇怪的链接。您需要更新您的问题以明确指出哪些行导致错误。
-
使用未解析的标识符“BankAccount”。这是错误。但是当我按 Cmd+Click 时,BankAccount 正在导航到班级。