我有一个变量 endentity ,带有type etidentity ,我需要存储在viewController1中,并在viewController2中检索,
viewController1
//Variables
var activationCodeFromCore, serialNumberFromCore, entityNameFromCore, deviceIdFromCore, registrationCodeFromCore, entityFromCore: String?
var activationCode, serialNumber, entityName, deviceId, registrationCode, entity: String?
var counter: Int = 0
var storedIdentity: ETIdentity?
以下是我需要保留
let storedIdentity = BridgeSDKUtils.performClassicActivation("26586-05858", withActivationCode: "8998-6857-1357-1870", "entidad0");
GlobalIdentity.identity = storedIdentity;
func softTokenDataService() {
let storedIdentity = BridgeSDKUtils.performClassicActivation("26586-05858", withActivationCode: "8998-6857-1357-1870", "entidad0");
GlobalIdentity.identity = storedIdentity;
self.activationCode = "8998-6857-1357-1870"
self.serialNumber = "26586-05858"
self.entityName = "entityData(counter)"
self.deviceId = "(String(describing: storedIdentity?.deviceId))"
self.registrationCode = "(String(describing: storedIdentity?.registrationCode))"
self.entity = "(storedIdentity!)"
}
...
func getEntityCore()
{
//Variables that are going to be stored
self.activationCodeFromCore = activationCode
self.serialNumberFromCore = serialNumber
self.entityNameFromCore = entityName
self.deviceIdFromCore = deviceId
self.registrationCodeFromCore = registrationCode
self.entityFromCore = storedIdentity
}
...
//SecureStorage Function
func saveEntityToCoreData()-> Bool {
self.softTokenDataService()
var SavedItem:Bool = true
var arr : [[String: Any]] = [[
"activationCode": self.activationCodeFromCore,
"serialNumber": self.serialNumberFromCore,
"entityName": self.entityNameFromCore,
"deviceId": self.deviceIdFromCore,
"registrationCode": self.registrationCodeFromCore,
"entity": self.entityFromCore]]
let jsonData = try! JSONSerialization.data(withJSONObject: arr, options: [.prettyPrinted])
let json = String(data: jsonData, encoding: String.Encoding.utf8)!
if self.saveRutSwitchOn
{
SecureData.save(key: "entityData0)", data: json.data(using: .utf8)!)
}
SavedItem = self.saveRutSwitchOn
return SavedItem
}
ViewController2
struct Person {
var activationCode: String
var serialNumber: String
var entityName: String
var deviceId: String
var registrationCode: String
var entity: String
}
struct EntityModel: Codable {
let activationCode, serialNumber, entityName, deviceId, registrationCode, entity: String?
}
if let loadedData = SecureData.load(key: "entityData0") {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let entityData = try decoder.decode([EntityModel].self, from: loadedData)
entityData.forEach { (EntityModel) in
//Here I Imagine something like this
//var identity: ETIdentity?
//identity = EntityModel.entity
////Here I have the identity, so I can manipulate it like needed, because is from type ETIdentity I can access its methods.
//identity?.getOTP(Date())
}
} catch {
print(error)
}
}
参考数据:
globalidentity.swift
struct GlobalIdentity{
static var identity : ETIdentity?
}
etidentity.h
@interface ETIdentity : NSObject<NSCoding> {
@private
-(NSString*)getOTP:(NSDate*)time;
@end
编辑问题在于变量 entity (我需要在viewController2中调用其参数(,不是字符串,因此它崩溃了,它不起作用。我还尝试将可变标识放置在我需要的var identity: ETIdentity?
的类型中,但是EtIdentity不使用 codable 进行协议(与struct一起使用,以便我可以在viewController2中称呼它们2(
我读了您的问题,发现您所缺少的基本内容是您的实体似乎是词典或其他类型的模型,如果是字典,请执行此操作。当前,您的SecureData.load(key: "entityData0")
包含返回实体模型数组的所有数据,并且您的数据对象loadedData
必须包含entity
对象,为什么不尝试为您的entity
实现Codable
。
struct SortedArryModel: Codable {
var sorterarrkey: String? // if your array contain strings
}
并在您的EntityModel
SortedArryModel
之后,您可以像[key:string]一样获得数组,只需将该字符串转换为数组。建议:在您的情况下,最好使用jsonserialization而不是代码,如果您仍然要应用代号,那么您的元素应符合代码协议。就像我上面提到的那样,将您的实体作为模型,将代码协议。