避免在swift 4中重复代码

问题描述 投票:0回答:3

我想创建一个通用函数,以避免在使用条件时重复。有没有可能的想法来实现这一目标?谢谢

struct ObjectDataItem {
var name: String
var value: String
}

static func arrayFields(arrayObject: ArrayObject) -> Array<ObjectDataItem> {
    var objectFields = [ObjectDataItem]()

    if let objectCategoryValue = arrayObject.objectCategory {
        let data = [ObjectDataItem(name: ObjectCategoryConstant.objectCategoryKey, value: objectCategory)]
        objectFields.append(contentsOf: data)
    }

    if let objectTypeValue = arrayObject.objectType {
        let data = [ObjectDataItem(name: ObjectTypeConstant.objectTypeKey, value: objectTypeValue)]
        objectFields.append(contentsOf: data)
    }

    if let objectName = arrayObject.objectName {
        let data = [ObjectDataItem(name: ObjectNameConstant.objectNameKey, value: objectName)]
        objectFields.append(contentsOf: data)
    }

    if let countryObjectValue = arrayObject.countryObjectCode {
        let data = [ObjectDataItem(name: countryObjectConstant.countryObjectCodeKey, value: countryObjectValue)]
        objectFields.append(contentsOf: data)
    }

    return objectFields
}
swift generic-function
3个回答
1
投票

你可以使用keypaths

func arrayFields(arrayObject: ArrayObject) -> Array<ObjectDataItem> {
    var objectFields = [ObjectDataItem]()

    func appendField(key: String, valuePath: KeyPath<ArrayObject, String?>) {
        if let value = arrayObject[keyPath: valuePath] {
            let data = [ObjectDataItem(name: key, value: value)]
            objectFields.append(contentsOf: data)
        }
    }

    appendField(key: ObjectCategoryConstant.objectCategoryKey, valuePath: \ArrayObject.objectCategory)
    appendField(key: ObjectCategoryConstant.objectTypeKey, valuePath: \ArrayObject.objectType)

    return objectFields
}

你可以更进一步,使用字典来查找键,所以最后你只需要传入keypath。


0
投票

对我来说唯一有意义的是先创建一个字典:

var dataDictionary: [String: String] = [:]
dataDictionary[ObjectCategoryConstant.objectCategoryKey] = arrayObject.objectCategory
dataDictionary[ObjectCategoryConstant.objectTypeKey] = arrayObject.objectType
dataDictionary[ObjectCategoryConstant.objectNameKey] = arrayObject.objectName
dataDictionary[countryObjectConstant.countryObjectCodeKey] = arrayObject.countryObjectValue

let objectFields = dataDictionary.map { (name, value) in
    ObjectDataItem(name: name, value: countryObjectValue)
}

字典不包含nil的值。但是,您丢失了值的排序(如果它对您很重要)。简化也不是很大。


0
投票

如果你不介意你的密钥是你的属性名称,你也可以使用反射这样的东西:

func arrayFields(arrayObject: ArrayObject) -> Array<ObjectDataItem> {
    var objectFields = [ObjectDataItem]()
    let objectMirror = Mirror(reflecting: arrayObject)
    for child in objectMirror.children {
        let (propertyName, propertyValue) = child
        objectFields.append(ObjectDataItem(name:propertyName!, value: propertyValue as! String))
    }
    return objectFields
}
© www.soinside.com 2019 - 2024. All rights reserved.