cocoa用nspredicate描述查詢的方式,原理類似於在資料庫中進行查詢
計算謂詞:
//基本的查詢
nspredicate *predicate;
predicate = [nspredicate predicatewithformat: @"name == 'herbie'"
];
bool
match = [predicate evaluatewithobject: car];
nslog (@"%s"
, (match) ?
"yes"
: "no"
);
//在整個cars裡面迴圈比較
predicate = [nspredicate predicatewithformat: @"engine.horsepower > 150"
];
nsarray *cars = [garage cars];
for(car *car in [garage cars])
} //輸出完整的資訊
predicate = [nspredicate predicatewithformat: @"engine.horsepower > 150"
];
nsarray *results;
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
//含有變數的謂詞
nspredicate *predicatetemplate = [nspredicate predicatewithformat:@"name == $name"
];
nsdictionary *vardict;
vardict = [nsdictionary dictionarywithobjectsandkeys:
@"herbie"
, @"name"
, nil];
predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict];
nslog(@"snorgle: %@"
, predicate);
match = [predicate evaluatewithobject: car];
nslog (@"%s"
, (match) ?
"yes"
: "no"
);
//注意不能使用$variable作為路徑名,因為它值代表值
//謂詞字元竄還支援c語言中一些常用的運算子
predicate = [nspredicate predicatewithformat:
@"(engine.horsepower > 50) and (engine.horsepower < 200)"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"oop %@"
, results);
predicate = [nspredicate predicatewithformat: @"name < 'newton'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, [results valueforkey: @
"name"
]);
//強大的陣列運算子
predicate = [nspredicate predicatewithformat:
@"engine.horsepower between "
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
nsarray *betweens = [nsarray arraywithobjects:
[nsnumber numberwithint: 50], [nsnumber numberwithint: 200], nil];
predicate = [nspredicate predicatewithformat: @"engine.horsepower between %@"
, betweens];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
predicatetemplate = [nspredicate predicatewithformat: @"engine.horsepower between $powers"
];
vardict = [nsdictionary dictionarywithobjectsandkeys: betweens, @"powers"
, nil];
predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
//in運算子
predicate = [nspredicate predicatewithformat: @"name in "
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, [results valueforkey: @
"name"
]);
predicate = [nspredicate predicatewithformat: @"self.name in "
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, [results valueforkey: @
"name"
]);
names = [cars valueforkey: @"name"
];
predicate = [nspredicate predicatewithformat: @"self in "
];
results = [names filteredarrayusingpredicate: predicate];//這裡限制了self的範圍
nslog (@"%@"
, results);
//beginswith,endswith,contains
//附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字元,cd表示什麼都不區分
predicate = [nspredicate predicatewithformat: @"name beginswith 'bad'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
predicate = [nspredicate predicatewithformat: @"name beginswith 'herb'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
predicate = [nspredicate predicatewithformat: @"name beginswith[cd] 'herb'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
//like運算子(萬用字元)
predicate = [nspredicate predicatewithformat: @"name like[cd] '*er*'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
predicate = [nspredicate predicatewithformat: @"name like[cd] '???er*'"
];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@"
, results);
Cocoa過濾器NSPredicate的完全用法
從下面的各個例子中,可以體會到nspredicate的強大的能力,作為正規表示式的核心類,確實優化了很多的字串及其正則相關的操作的流程。使 簡潔,而強大!cpp view plain copy print cocoa用nspredicate描述查詢的方式,原理類似於在資料庫中進行查詢 計算謂詞 基本...
Vue 過濾器案例(全域性過濾器和區域性過濾器)
doctype html en utf 8 viewport content width device width,initial scale 1.0 js vue 2.4.0 js script 過濾器 title head 兩個過濾器的名稱都為msgformat,但是控制不同作用,乙個是全域性的...
過濾器(6) 過濾器的攔截
本系列部落格彙總在這裡 過濾器彙總 我們來做個測試,寫乙個過濾器,指定過濾的資源為 index.jsp,然後我們在瀏覽器中直接訪問 index.jsp,你會發現過濾器執行了!但是,當我們在 helloservlet 中使用伺服器端的跳轉request.getrequestdispathcer ind...