2017-11-28 7 views
0

私はCore Dataの同じ用語を使用して検索を実行し、URLを構築する必要があるプロジェクトを作成しました。 DataManagerクラスにコードをダンプするのではなく、クエリの要素を格納する別の検索用語クラスを作成して、とURLを初期化するときに作成しようとしています。ミラーで作成した辞書からゼロ値をフィルタリングする

私はMirrorを使用して、クラスのキーと値を持つ辞書を作成しています。私は、辞書から非ゼロの値をフィルタリングしようとしています。サイト上の他の場所でソリューションを私の問題に適用する方法を理解していません。

クラスの唯一の非唯一のvarsは、DictionaryURL、およびNSCompoundPredicateです。だから、クラスが初期化されるときに、私はNSCompoundPredicateURLを設定するために使用される辞書を読み込むメソッドを実行します。

辞書から非nilの値をフィルタリングされて、私は難易度に実行しているよ

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を印刷するとき、それはまだそれにnilキーをINGの****持っています。私はSOのいくつかの異なるソリューションを見てきましたが、私は何を試しても同じ問題に走り続けています。

私は何が欠けていますか、どのように修正しますか?

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) 
    } 
} 
+0

可能な重複し、作る方が良いでしょう(https://stackoverflow.com/questions/47393441/check-for-nil-in-dictionary) – dan

答えて

1

あなたが直接ので、それタイプAnyの子供valueをnilに比較することはできませんが、あなたが反復しながら、それが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 
} 
+0

[辞書にはnilをチェック]それは私を追加するのではなく、怠惰なvarその価値を設定するthod –

+0

ありがとう、ダン!私はレオのモッズを実装するかもしれません。 – Adrian

+1

'lazy var nonNilDictionary:[文字列:任意] = { var dict:[文字列:任意] = [:] ミラー内の子(反映:自己).children { ガードletキー=子。ラベルelse { } ケースオプション .some(値を聞かせ)=} child.value { 辞書[キー] =値 }戻り辞書 }() ' –

関連する問題