Swift 泛型函数问题.减少重复代码



我想了解如何减少重复代码的数量。我有两个几乎相同的功能。区别如下:

FIRS 函数返回[ExerciseEntity]数组,第二个函数返回[WorkoutEntity]数组

func replaceExercisesIdentifiers(from jsonObjects: [[String: Any]], transaction: BaseDataTransaction) -> [ExerciseEntity] {
for jsonObject in jsonObjects {
if let mobileLocalId = jsonObject["mobileLocalId"] as? String {
if mobileLocalId.contains("<x-coredata://") {
if let managedObject = try? transaction.fetchOne(From<ExerciseEntity>()
.where(
format: "%K == %@",
#keyPath(BaseMO.id),
mobileLocalId)
) {
let editObject = transaction.edit(managedObject)
if let identifier = jsonObject["id"] as? String {
editObject?.id = identifier
}
}
}
}
}
let managedObjects = try! transaction.importUniqueObjects(
Into<ExerciseEntity>(),
sourceArray: jsonObjects)
return managedObjects
}
func replaceWorkoutsIdentifiers(from jsonObjects: [[String: Any]], transaction: BaseDataTransaction) -> [WorkoutEntity] {
for jsonObject in jsonObjects {
if let mobileLocalId = jsonObject["mobileLocalId"] as? String {
if mobileLocalId.contains("<x-coredata://") {
if let managedObject = try? transaction.fetchOne(From<WorkoutEntity>()
.where(
format: "%K == %@",
#keyPath(BaseMO.id),
mobileLocalId)
) {
let editObject = transaction.edit(managedObject)
if let identifier = jsonObject["id"] as? String {
editObject?.id = identifier
}
}
}
}
}
let managedObjects = try! transaction.importUniqueObjects(
Into<WorkoutEntity>(),
sourceArray: jsonObjects)
return managedObjects
}

这是一个与我之前问过的如何使用泛型函数相关的类似问题。

我在代码中实现了这个,但是:

func importArray<T: ImportableUniqueObject>(from exercisesDict: [[String: Any]], transaction: BaseDataTransaction) -> [T] where T.ImportSource == [String: Any] {
let managedObjects = try? transaction.importUniqueObjects(Into<T>(), sourceArray: jsonObjects)
}

但这里有一些东西,T型

首先 - 我无法添加此代码:editObject?.id = identifier

因为T type没有id.

其次,当我每次崩溃时调试这些通用函数调试器时:

Message from debugger: The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log.

如果有趣,这里有一个带有日志的文件。我还没有提交。

当然,我可以添加很多打印来跟踪行为,尽管这很烦人(但主要任务是摆脱重复。

试试这个(我还没有测试过(:

protocol MyProtocol {
var id: Int { get set }
}
struct ExerciseEntity {
var id: Int
}    
struct WorkoutEntity {
var id: Int
} 
func replaceWorkoutsIdentifiers<T: MyProtocol>(from jsonObjects: [[String: Any]], transaction: BaseDataTransaction) -> [T] {
for jsonObject in jsonObjects {
if let mobileLocalId = jsonObject["mobileLocalId"] as? String {
if mobileLocalId.contains("<x-coredata://") {
if let managedObject = try? transaction.fetchOne(From<T>()
.where(
format: "%K == %@",
#keyPath(BaseMO.id),
mobileLocalId)
) {
let editObject = transaction.edit(managedObject)
if let identifier = jsonObject["id"] as? String {
editObject?.id = identifier
}
}
}
}
}
let managedObjects = try! transaction.importUniqueObjects(
Into<T>(),
sourceArray: jsonObjects)
return managedObjects as! T
}

用:

let array: [ExerciseEntity] = replaceWorkoutsIdentifiers(from jsonObjects: ..., transaction: ...)

最新更新