This article was accepted into the corpus but its outbound wikilinks were never NER-processed — typical at the deepest BFS hop or when the run's entity cap was reached. No expansion funnel to show.
| NSPredicate | |
|---|---|
| Name | NSPredicate |
| Developer | Apple Inc. |
| Initial release | 2001 |
| Latest release | macOS/iOS updates |
| Programming language | Objective‑C, Swift |
| Platform | macOS, iOS, tvOS, watchOS |
| License | Apple Software License |
NSPredicate
NSPredicate is a Foundation framework class used for defining logical conditions that can be evaluated against objects, collections, or data stores in Apple's platforms. It provides a textual and programmatic query language enabling filtering, matching, and predicate logic for Objective‑C and Swift code. NSPredicate interoperates with Core Data, Foundation collections, and Cocoa APIs to express queries in a compact, composable form.
NSPredicate represents a boolean condition expressed as a predicate object that can be evaluated against instances or used to filter collections. It complements classes such as NSArray, NSDictionary, and NSSortDescriptor and integrates tightly with technologies including Core Data, NSExpression, and NSPredicateEditor. NSPredicate enables applications on macOS, iOS, tvOS, and watchOS to perform queries that map to in‑memory evaluation or backend fetch requests.
The textual syntax supports key‑value coding paths, comparison operators, logical connectors, and aggregate operations. Literal forms include format strings with %@, %K, and format specifiers parsed similarly to NSString formatting. Supported operators include ==, !=, <, >, <=, >=, BEGINSWITH, ENDSWITH, CONTAINS, LIKE, MATCHES, IN, BETWEEN, and EXISTS. Format parsing leverages NSExpression nodes to represent constants, key paths, functions, and subqueries, enabling use of functions such as sum:, count:, min:, max:, and custom functions accessible via NSExpression(forFunction:arguments:). Predicates can be combined using NSCompoundPredicate with types AND, OR, and NOT to create complex boolean logic.
Common usage patterns include filtering arrays with filtered(using:), constructing NSFetchRequest predicates for Core Data fetches, and driving UI query editors. Example patterns: creating predicates from format strings, using NSComparisonPredicate for programmatic construction, and employing SUBQUERY to relate nested collections. In practice, developers use NSPredicate to filter model arrays in MVC or MVVM architectures, to limit results in search features, and to implement dynamic filters in apps that use technologies like UITableView, UICollectionView, and NSSortDescriptor for ordering.
Evaluation semantics differ between in‑memory and store‑backed contexts: in‑memory evaluation uses object property access and KVC compliance, while store translation (e.g., SQLite) requires predicate constructs translatable to the backing store’s query language. Performance considerations include predicate complexity, use of functions or regex MATCHES, and use of key paths that trigger faulting in Core Data. Optimizations involve reducing use of non‑sargable constructs, preferring indexed attributes in persistent stores, and avoiding excessive SUBQUERY or SELF evaluations on large collections. Instruments such as Time Profiler, Core Data SQL Debugging, and Energy Diagnostics can help locate bottlenecks.
NSPredicate is a first‑class citizen in Foundation and is the canonical mechanism for expressing fetch criteria in Core Data’s NSFetchRequest. When used with NSPersistentStoreCoordinator and NSPersistentContainer, predicates are translated into SQL, binary, or other store queries depending on the NSPersistentStore type. Integration points include NSSortDescriptor, NSFetchRequest, NSPredicateEditor in AppKit, and NSExpression descriptions for transformable properties. Care must be taken when using predicates with transient properties, relationships, and faulted objects to ensure correct and efficient fetch semantics.
Because NSPredicate format strings can be constructed from external input, applications must avoid direct interpolation of untrusted strings into predicate formats to prevent injection‑like issues or unintended behavior when predicates are parsed or compiled. Use parameterized format arguments (%@, %K) or programmatic NSComparisonPredicate construction to mitigate risk. In sandboxed environments and App Store contexts, ensure predicates do not attempt to access private APIs or introspect types beyond allowed entitlements. When predicates are serialized (e.g., archived) between processes, validate contents carefully to avoid deserialization vulnerabilities.
Introduced as part of the OpenStep/Foundation lineage, predicate objects trace roots to predicate logic and earlier query abstractions in Objective‑C frameworks. Over successive OS X and iOS releases, NSPredicate gained richer operator support, enhanced NSExpression functions, SUBQUERY and block‑based evaluation, and tighter Core Data translation. Platform updates continued to refine performance and compatibility with SQLite, binary stores, and CloudKit‑backed stores as Apple expanded persistence and synchronization technologies.
Apple Inc. Objective‑C Swift (programming language) Foundation (Apple) Core Data NSFetchRequest NSSortDescriptor NSExpression NSPredicateEditor NSCompoundPredicate NSArray NSDictionary UITableView UICollectionView NSPersistentStoreCoordinator NSPersistentContainer SQLite CloudKit Instruments (software) Time Profiler App Store App Sandbox KVC NSComparisonPredicate SUBQUERY NSExpression(forFunction:arguments:) macOS iOS tvOS watchOS OpenStep Objective‑C runtime SQLite database serialization deserialization predicate logic MVC MVVM NSSortDescriptor NSPredicateEditor NSCoding NSKeyedArchiver Security (magazine) Software security Performance engineering Apple Developer Xcode Instruments Debugging Best practice App Sandbox Entitlement (computing) CloudKit NSPersistentStore Binary property list Binary plist Regex Regular expression KVC compliance Fault (computing) Transient property Relationship (database) Predicate pushdown Query optimization SQL Binary store SQLite cache Fetch limit Predicate composition NSPredicateVisualizer PredicateEditor NSPredicate validation Predicate serialization Data synchronization Persistent history tracking Concurrency (computer science) Thread safety Dispatch (Apple) Grand Central Dispatch Concurrency programming Foundation Networking CFNetwork NSURLSession Core Data faults Apple platform frameworks API design Design pattern