Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从使用镜像创建的字典中筛选nil值_Ios_Swift_Dictionary_Filter - Fatal编程技术网

Ios 从使用镜像创建的字典中筛选nil值

Ios 从使用镜像创建的字典中筛选nil值,ios,swift,dictionary,filter,Ios,Swift,Dictionary,Filter,我有一个项目正在进行中,我需要使用核心数据中的相同术语执行搜索,并构建URL。我没有在DataManager类中转储一堆代码,而是尝试创建一个单独的搜索词类,该类将存储查询元素,并在初始化时构造一个NSCompoundPredicate和一个URL 我正在使用Mirror构建一个包含类的键和值的字典。我正试图从字典中筛选出非nil值。我不知道如何在网站上的其他地方应用解决方案来解决我的问题 该类唯一的非可选vars是字典,URL,以及NSCompoundPredicate。因此,当类初始化时,我

我有一个项目正在进行中,我需要使用
核心数据中的相同术语执行搜索,并构建
URL
。我没有在DataManager类中转储一堆代码,而是尝试创建一个单独的搜索词类,该类将存储查询元素,并在初始化时构造一个
NSCompoundPredicate
和一个
URL

我正在使用
Mirror
构建一个包含类的键和值的字典。我正试图从字典中筛选出非nil值。我不知道如何在网站上的其他地方应用解决方案来解决我的问题

该类唯一的非可选
vars
字典
URL
,以及
NSCompoundPredicate
。因此,当类初始化时,我运行一个方法来填充字典,字典反过来用于设置
NSCompoundPredicate
URL

我遇到的困难是从字典中过滤出非零值:

func setNonNilDictionary() {
    // get the keys for the class' properties
    let keys = Mirror(reflecting: self).children.flatMap{$0.label}
    // get the values for the class' properties
    let values = Mirror(reflecting: self).children.flatMap{$0.value}
    // zip them into a dictionary
    var propertyDict = Dictionary(uniqueKeysWithValues: zip(keys, values))
    // remove the nil values
    let filteredDict = propertyDict.filter{$0.value != nil}
    nonNilPropertyDict = filteredDict

    print(nonNilPropertyDict)
}
当我打印
nonNilPropertyDict
时,它仍然有****ing
nil
键。我已经研究了一些不同的解决方案,但是不管我怎么做,我总是遇到同样的问题

我遗漏了什么,如何修复

下面是我的班级的样子:

class LastSearch: NSObject {

  var startDate: Date?
  var endDate: Date?
  var minMagnitude: Double?
  var maxMagnitude: Double?
  var minLongitude: Double?
  var maxLongitude: Double?
  var minLatitude: Double?
  var maxLatitude: Double?
  var minDepth: Double?
  var maxDepth: Double?

  // methods to create predicate and url reference this dictionary
  var nonNilPropertyDict: Dictionary<String, Any>!

  var url: URL!
  var predicate: NSCompoundPredicate!

  init(startDate: Date?, endDate: Date?,
       minMagnitude: Double?, maxMagnitude: Double?,
       minLongitude: Double?, maxLongitude: Double?,
       minLatitude: Double?, maxLatitude: Double?,
       minDepth: Double?, maxDepth: Double?) {

    super.init()

    // Dates
    self.startDate = startDate
    self.endDate = endDate

    // Magnitude Values
    self.minMagnitude = minMagnitude
    self.maxMagnitude = maxMagnitude

    // Geographic Coordinates
    self.minLongitude = minLongitude
    self.maxLongitude = maxLongitude
    self.minLatitude = minLatitude
    self.maxLatitude = maxLatitude

    // Depth Values
    self.minDepth = minDepth
    self.maxDepth = maxDepth

    self.setNonNilDictionary()
    self.setURL()
    self.setPredicate()

  }

  func setNonNilDictionary() {
    let keys = Mirror(reflecting: self).children.flatMap{$0.label}
    let values = Mirror(reflecting: self).children.flatMap{$0.value}
    let nonNilPropertyDict = Dictionary(uniqueKeysWithValues: zip(keys, values))

    print(filtered)
    print(nonNilPropertyDict)
  }
}
class LastSearch:NSObject{
var开始日期:日期?
var endDate:日期?
两倍?
var最大值:两倍?
两倍?
var Max经度:双倍?
纬度:双倍?
var最大纬度:双?
var minDepth:双倍?
var maxDepth:双精度?
//创建谓词和url的方法引用此字典
var nonNilPropertyDict:字典!
var-url:url!
var谓词:NSCompoundPredicate!
初始(开始日期:日期?,结束日期:日期?,
最小震级:双精度,最大震级:双精度?,
最小经度:双精度?,最大经度:双精度?,
minLatitude:Double?,maxLatitude:Double?,
minDepth:Double?,maxDepth:Double?){
super.init()
//日期
self.startDate=startDate
self.endDate=endDate
//震级值
self.minMagnite=最小震级
self.maxMagnitude=maxMagnitude
//地理坐标
self.minLongitude=minLongitude
self.maxLongitude=maxLongitude
self.minLatitude=minLatitude
self.maxLatitude=maxLatitude
//深度值
self.minDepth=minDepth
self.maxDepth=maxDepth
self.setNonNilDictionary()
self.setURL()
self.setPredicate()
}
func setNonNilDictionary(){
让keys=Mirror(反射:self).children.flatMap{$0.label}
让values=Mirror(反射:self).children.flatMap{$0.value}
让nonNilPropertyDict=字典(uniqueKeyWithValues:zip(键,值))
打印(过滤)
打印(非NILPROPERTYDICT)
}
}

您不能直接将子项的
与nil进行比较,因为它的类型为
Any
,但您可以使用模式匹配来确保在迭代子项时它不是nil:

func setNonNilDictionary() {        
    var nonNilProperties = [String: Any]()

    for child in Mirror(reflecting: self).children {
        guard let label = child.label else { return }

        if case Optional<Any>.some(let value) = child.value {
            nonNilProperties[label] = value
        }
    }

    nonNilPropertyDict = nonNilProperties
}
func setNonNilDictionary(){
var nonNilProperties=[String:Any]()
镜子里的孩子(反射:自我)。孩子们{
guard let label=child.label else{return}
如果大小写可选,则.some(let value)=child.value{
非零属性[标签]=值
}
}
nonNilPropertyDict=nonNilProperties
}

它的可能副本最好将其设置为惰性变量,而不是添加一个方法来设置其值谢谢你,丹!我可能会为镜像中的child(反射:self)实现Leo的mod.
lazy-var-nonnildiodictionary:[String:Any]={var-dict:[String:Any]=[:]子对象{guard-let-key=child.label-else{continue}如果大小写可选。some(let-value)=child.value{dict[key]=value}}return dict}()
@Adrian甚至更短
lazy var nonnildiodictionary=Mirror(reflecting:self).children.reduce(成:[:]){guard case可选。some(let value)=$1.value else{return};$0[$1.label!]=value}
@LeoDabus非常感谢您在这方面的投入。我有些东西在工作,但有时会出错。Dan的解决方案和您的
lazy
var实现使其运行非常平稳。