1

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

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
}
4

3 回答 3

1

你可以使用键路径

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
}

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

于 2018-03-06T09:31:54.640 回答
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. 但是,您会丢失值的顺序(如果它对您很重要)。简化也不是很大。

于 2018-03-06T07:16:30.723 回答
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
}
于 2018-03-06T07:35:27.880 回答