用字典参数预测领域

时间:2017-10-30 11:53:16

标签: swift dictionary realm predicate

我想用字典paramateres创建一个谓词并在域上过滤数据,比如

var parameters = [String: Any]()
                        parameters["Mobile"] = a.Mobile!
                        parameters["CategoryId"] = self.SelectCategryId

                        let existContact = (contactBiz.FetchMulti(parameters: parameters)?.count)! > 0

在fetchMulti func中我创建了一个谓词和过滤数据

func FetchMulti(parameters: [String: Any])-> Results<RealmEntityType>?
{
    do
    {
        let key = T.KeyName()

        var object = realm.objects(RealmEntityType.self)

        let subPredicates = parameters.map {
            NSPredicate(format: "%@ = %@", $0.key, $0.value as! CVarArg)
        }

        let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: subPredicates)


//            var predictionArray = [String]()
//            for p in parameters
//            {
//                predictionArray.append("\(p.key) = \(p.value) ")
//                 
//            }
//            
//            let perdicate = predictionArray.joined(separator: " && ")
//            


        return object.filter(compoundPredicate);
    }
    catch
    {
        print(error.localizedDescription)

    }
    return nil

}

但我收到此错误

 reason: 'Predicate expressions must compare a keypath and another keypath or a constant value'

任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要使用using System; namespace test { public class Base { public int Data { get {return Data;} set {Data = value;} } public int Normal; } public class Program { public static void Main(string[] args) { Base b = new Base(); int x = b.Data; int y = b.Normal; } } } 来表示该值是键的名称而不是常量字符串: node[4][1]=LocalDeclarationStatement,declsymbol=null,symbol=null,type=null node[5][0]=VariableDeclaration,declsymbol=null,symbol=null,type=null node[6][0]=PredefinedType,declsymbol=null,symbol=int,name=Int32,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct node[6][1]=VariableDeclarator,declsymbol=x,symbol=null,type=null node[7][0]=EqualsValueClause,declsymbol=null,symbol=null,type=null node[8][0]=SimpleMemberAccessExpression,declsymbol=null,symbol=test.Base.Data,name=Data,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct node[9][0]=IdentifierName,declsymbol=null,symbol=b,name=b,static=False,type=test.Base,static=False,type.Kind=NamedType,type.TypeKind=Class node[9][1]=IdentifierName,declsymbol=null,symbol=test.Base.Data,name=Data,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct node[4][2]=LocalDeclarationStatement,declsymbol=null,symbol=null,type=null node[5][0]=VariableDeclaration,declsymbol=null,symbol=null,type=null node[6][0]=PredefinedType,declsymbol=null,symbol=int,name=Int32,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct node[6][1]=VariableDeclarator,declsymbol=y,symbol=null,type=null node[7][0]=EqualsValueClause,declsymbol=null,symbol=null,type=null node[8][0]=SimpleMemberAccessExpression,declsymbol=null,symbol=test.Base.Normal,name=Normal,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct node[9][0]=IdentifierName,declsymbol=null,symbol=b,name=b,static=False,type=test.Base,static=False,type.Kind=NamedType,type.TypeKind=Class node[9][1]=IdentifierName,declsymbol=null,symbol=test.Base.Normal,name=Normal,static=False,type=int,static=False,type.Kind=NamedType,type.TypeKind=Struct