-
[Swift] NSPredicate format placeholdersSwift 2023. 10. 25. 16:59
format placeholders 정리에 대한 GPT 의 답변
In Objective-C, when creating an NSPredicate with a format string, you can use several different placeholders to represent values of different data types. Here are some common placeholders for format strings in NSPredicate:
1. %@: Used for representing object values, such as NSString, NSNumber, NSDate, and other NSObject-derived classes.
2. %K: Used for specifying the name of a key path. It allows you to use a variable to specify the attribute or key you want to compare.
3. %d, %i, %u, %f, %ld, %lu, %lld, %llu, %s, %c: These are placeholders for primitive data types, like integers, floats, longs, doubles, characters, etc. You should choose the appropriate specifier based on the data type you want to compare.
4. %C: Used for representing a Unicode character value.
5. %@K: This placeholder is used for specifying key paths dynamically. It allows you to use a variable as a key path.
6. %SUBQUERY: Used for subquery predicates. You would use this placeholder to define a subquery within your predicate format.
7. %SELF: Represents the object being evaluated in the predicate.
8. %<comparison operator>: You can use placeholders for specific comparison operators, such as %<, %>, %==, %!=, %<=, and %>=.
9. %BEGINSWITH, %CONTAINS, %ENDSWITH: These placeholders are used for string operations like checking if a string begins with, contains, or ends with a specific substring.
These are some of the common placeholders used in NSPredicate format strings. The choice of placeholder depends on the data type you want to compare and the specific operation you want to perform in your predicate.
실제 프로젝트 케이스struct ItemDTO: SelfReturnable { var id: Int16 var name: String? } func edit(_ item: ItemDTO) { let request: NSFetchRequest<Item> = Item.fetchRequest() request.predicate = NSPredicate(format: "id == %d", item.id) // 여기서 앱이 크래쉬 남 // 왜냐하면 id 는 int 이기 때문에 "%d" 를 사용해야함 // request.predicate = NSPredicate(format: "id == %@", item.id) let object = try? db.context.fetch(request).first object?.name = item.name db.save() }
사용 예시
✔️ "%d" and "%i": Used for integers (Int): // Example: Filtering for items with a quantity greater than 10 let predicate = NSPredicate(format: "quantity > %d", 10) ✔️ "%u": Used for unsigned integers (UInt): // Example: Filtering for items with a positive ID let predicate = NSPredicate(format: "id > %u", 0) ✔️ "%f": Used for floating-point numbers (Float or Double): // Example: Filtering for items with a price less than $20.0 let predicate = NSPredicate(format: "price < %f", 20.0) ✔️ "%ld" and "%lu": Used for long integers (Int) and unsigned long integers (UInt): // Example: Filtering for items with a timestamp greater than a specific date (represented as a Unix timestamp) let predicate = NSPredicate(format: "timestamp > %ld", 1635172800) // Example: Filtering for items with a positive identifier let predicate = NSPredicate(format: "identifier > %lu", 0) ✔️ "%lld" and "%llu": Used for long long integers (Int64) and unsigned long long integers (UInt64): // Example: Filtering for items with a very large quantity let predicate = NSPredicate(format: "quantity > %lld", 1000000000) // Example: Filtering for items with a large positive identifier let predicate = NSPredicate(format: "identifier > %llu", 0) ✔️ "%s": Used for C-style strings (null-terminated character arrays): // Example: Filtering for items with a specific name let predicate = NSPredicate(format: "name LIKE[cd] %s", "Widget") ✔️ "%c": Used for characters (Character): // Example: Filtering for items with a specific initial letter let predicate = NSPredicate(format: "initialLetter == %c", "A")
'Swift' 카테고리의 다른 글
[Swift] @dynamicMemberLookup 써보기 (0) 2023.10.26 [Swift] KeyPath, WritableKeyPath, ReferenceWritableKeyPath 차이와 구분해서 사용하기 (0) 2023.10.25 [Combine] AsyncPublisher 란? (0) 2023.10.24 [Swift] ThreadSafe 한 Class 만들기 (1) 2023.10.24 [Swift] DispatchQueue.global() 과 Task.detatched(priority: .background) 의 차이점 (0) 2023.10.24