gpt4 book ai didi

swift - 'if (let keychain = KeychainService.loadToken())' 给我一个预期的声明错误

转载 作者:行者123 更新时间:2023-11-30 10:21:45 24 4
gpt4 key购买 nike

我的应用程序崩溃了,因为它在展开时发现 nil:

var keyChain:String = KeychainService.loadToken()!

所以我尝试这样做:

if (let keyChain = KeychainService.loadToken()){

}

但它给了我错误:预期声明

这是正确的方法吗?如果不是我做错了什么?

如果您不想看到我的 KeychainService 类:

// Identifiers
let serviceIdentifier = "serviceIndentifier"
let userAccount = "userAccount"
let accessGroup = "accessGroup"

// Arguments for the keychain queries
let kSecClassValue = kSecClass as NSString
let kSecAttrAccountValue = kSecAttrAccount as NSString
let kSecValueDataValue = kSecValueData as NSString
let kSecClassGenericPasswordValue = kSecClassGenericPassword as NSString
let kSecAttrServiceValue = kSecAttrService as NSString
let kSecMatchLimitValue = kSecMatchLimit as NSString
let kSecReturnDataValue = kSecReturnData as NSString
let kSecMatchLimitOneValue = kSecMatchLimitOne as NSString

class KeychainService: NSObject {

/**
* Exposed methods to perform queries.
* Note: feel free to play around with the arguments
* for these if you want to be able to customise the
* service identifier, user accounts, access groups, etc.
*/
internal class func saveToken(token: NSString) {
self.save(serviceIdentifier, data: token)
}

internal class func loadToken() -> NSString? {
var token = self.load(serviceIdentifier)

return token
}

/**
* Internal methods for querying the keychain.
*/
private class func save(service: NSString, data: NSString) {
var dataFromString: NSData = data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

// Instantiate a new default keychain query
var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, userAccount, dataFromString], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecValueDataValue])

// Delete any existing items
SecItemDelete(keychainQuery as CFDictionaryRef)

// Add the new keychain item
var status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil)
}

private class func load(service: NSString) -> NSString? {
// Instantiate a new default keychain query
// Tell the query to return a result
// Limit our results to one item
var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, userAccount, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecReturnDataValue, kSecMatchLimitValue])

var dataTypeRef :Unmanaged<AnyObject>?

// Search for the keychain items
let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)

let opaque = dataTypeRef?.toOpaque()

var contentsOfKeychain: NSString?

if let op = opaque? {
let retrievedData = Unmanaged<NSData>.fromOpaque(op).takeUnretainedValue()

// Convert the data retrieved from the keychain into a string
contentsOfKeychain = NSString(data: retrievedData, encoding: NSUTF8StringEncoding)
} else {
println("Nothing was retrieved from the keychain. Status code \(status)")
}

return contentsOfKeychain
}
}

最佳答案

省略大括号:

if let keyChain = KeychainService.loadToken() {
...
}

关于swift - 'if (let keychain = KeychainService.loadToken())' 给我一个预期的声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com