当我实际上可以访问该标识符时如何修复错误 "Use of Unresolved identifier"
How to fix the error "Use of Unresolved identifier" when actually I can access that identifier
我有一个 class 被定义为单例,我尝试从那个 class 访问 2 个函数,但我得到一个错误,说 class 不能可以找到,但是当我按下 Cmd + Click 时,我可以导航到那个 class。
我重新启动 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))
}
}
这是一个演示:
感谢您的宝贵时间!
您没有正确设置单例。 BankAccount
是一个 class,不是实例。单例是 class 的一个版本,其中只会访问一个实例,但您仍在访问一个实例。您需要添加:
class BankAccount {
static let shared = BankAccount()
...
到您的银行帐户 class。 shared
属性 是实际的单例实例。稍后当您尝试访问单例时,而不是
BankAccount.withdraw(amount: amount)
您想使用该实例:
BankAccount.shared.withdraw(amount: amount)
确保您的 class BankAccount 文件可用于您的其他目标 -> ShoppingLandSiri。您可以从文件检查器视图中查看它。
我有一个 class 被定义为单例,我尝试从那个 class 访问 2 个函数,但我得到一个错误,说 class 不能可以找到,但是当我按下 Cmd + Click 时,我可以导航到那个 class。 我重新启动 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))
}
}
这是一个演示:
感谢您的宝贵时间!
您没有正确设置单例。 BankAccount
是一个 class,不是实例。单例是 class 的一个版本,其中只会访问一个实例,但您仍在访问一个实例。您需要添加:
class BankAccount {
static let shared = BankAccount()
...
到您的银行帐户 class。 shared
属性 是实际的单例实例。稍后当您尝试访问单例时,而不是
BankAccount.withdraw(amount: amount)
您想使用该实例:
BankAccount.shared.withdraw(amount: amount)
确保您的 class BankAccount 文件可用于您的其他目标 -> ShoppingLandSiri。您可以从文件检查器视图中查看它。