Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
ソース リンク
ソース リンク
OOPer
  • 1.9万
  • 2
  • 16
  • 33

残念ながらNSPredicateの演算子には、剰余を求めるものは無いようです。(ちなみに%はフォーマット用のパラメータとみなされるので、不正な形式のフォーマットを指定するとエラー落ちします。)

BNF Definition of Cocoa Predicates

ただ英語ありで検索をかけるとこんな記事が出てきました。

How to fetch even and odd values from core data using Predicate

どうやらきちんと明記されたドキュメントは見当たりませんが、NSExpressionでの定義済み関数はどれも使えるようです。

(NSExpression.hより)※ちと長いので綺麗に桁合わせできていません。悪しからず。

// Predefined functions are:
// name              parameter array contents             returns
//-------------------------------------------------------------------------------------------------------------------------------------
// sum:              NSExpression instances representing numbers      NSNumber 
// count:            NSExpression instances representing numbers      NSNumber 
// min:              NSExpression instances representing numbers      NSNumber  
// max:              NSExpression instances representing numbers      NSNumber
// average:          NSExpression instances representing numbers      NSNumber
// median:           NSExpression instances representing numbers      NSNumber
// mode:             NSExpression instances representing numbers      NSArray     (returned array will contain all occurrences of the mode)
// stddev:           NSExpression instances representing numbers      NSNumber
// add:to:           NSExpression instances representing numbers      NSNumber
// from:subtract:    two NSExpression instances representing numbers  NSNumber
// multiply:by:      two NSExpression instances representing numbers  NSNumber
// divide:by:        two NSExpression instances representing numbers  NSNumber
// modulus:by:       two NSExpression instances representing numbers  NSNumber
// sqrt:             one NSExpression instance representing numbers       NSNumber
// log:              one NSExpression instance representing a number  NSNumber
// ln:               one NSExpression instance representing a number  NSNumber
// raise:toPower:    one NSExpression instance representing a number  NSNumber
// exp:              one NSExpression instance representing a number  NSNumber
// floor:            one NSExpression instance representing a number  NSNumber
// ceiling:          one NSExpression instance representing a number  NSNumber
// abs:              one NSExpression instance representing a number  NSNumber
// trunc:            one NSExpression instance representing a number  NSNumber
// uppercase:        one NSExpression instance representing a string  NSString
// lowercase:        one NSExpression instance representing a string  NSString
// random            none                         NSNumber (integer) 
// randomn:          one NSExpression instance representing a number  NSNumber (integer) such that 0 <= rand < param
// now               none                         [NSDate now]
// bitwiseAnd:with:  two NSExpression instances representing numbers  NSNumber    (numbers will be treated as NSInteger)
// bitwiseOr:with:   two NSExpression instances representing numbers  NSNumber    (numbers will be treated as NSInteger)
// bitwiseXor:with:  two NSExpression instances representing numbers  NSNumber    (numbers will be treated as NSInteger)
// leftshift:by:     two NSExpression instances representing numbers  NSNumber    (numbers will be treated as NSInteger)
// rightshift:by:    two NSExpression instances representing numbers  NSNumber    (numbers will be treated as NSInteger)
// onesComplement:   one NSExpression instance representing a numbers NSNumber    (numbers will be treated as NSInteger)
// noindex:          an NSExpression                  parameter   (used by CoreData to indicate that an index should be dropped)
// distanceToLocation:fromLocation: two NSExpression instances representing CLLocations    NSNumber
// length:           an NSExpression instance representing a string         NSNumber

※一部はNSArrayには使えてもCore Data等のクエリーには使えない可能性があります。

let array: NSArray = [1,2,3,4,5,6,7,8,9,10]

let predicate = NSPredicate(format: "SELF > 4 AND modulus:by:(SELF,2) == 0")
array.filtered(using: predicate)

またCore Data等に使う必要が無いと言う前提なら、自前のメソッドを呼び出すこともできるようです。

extension NSNumber {
    @objc func isEven() -> NSNumber {
        return (self.intValue % 2 == 0) as NSNumber
    }
}

let predicate2 = NSPredicate(format: "SELF > 4 AND FUNCTION(SELF,'isEven')==1")
array.filtered(using: predicate2)

なお配列からと言うのを本当に大前提として良いのであれば、SwiftのArrayfilterメソッドを使った方が簡単でしょう。

let arr: [Int] = [1,2,3,4,5,6,7,8,9,10]

let result = arr.filter { $0 > 4 && $0 % 2 == 0 }
print(result) //->[6, 8, 10]