seen from United States
seen from Malaysia

seen from United States

seen from Türkiye
seen from China

seen from Argentina
seen from T1

seen from Yemen

seen from Russia
seen from China

seen from Malaysia
seen from China

seen from Maldives

seen from Singapore

seen from Malaysia
seen from China
seen from Malaysia

seen from Brazil

seen from Malaysia
seen from China
NSPredicate + Objective-C
[Objective-C] How to search object in NSArray
NSArray에 Person이라는 class의 오브젝트들이 담겨있다고 해보자. 이들 중, name이라는 property의 값이 darren인 object는 어떻게 찾을까?
For문을 이용해 찾아보자.
NSMutableArray *result = [NSMutableArray new]; for (Person *personObject in array) { if ([personObject.name isEqualToString:@”darren”]) { [result addObject:personObject]; } }
이런 더러운 코드는 정말 어쩔 수 없을 때가 아니라면 사용하지 말자. 가끔 Stackoverflow의 고수들도 이런 실수를 하기도 한다.
NSPredicate를 이용해 찾아보자.
Cocoa의 Foundation framework에는 NSPredicate라는 멋진 클래스가 존재한다. 그리고, NSArray에는 predicate를 이용하여 필터처리를 해주는 멋진filteredArrayUsingPredicate: 메소드도 존재한다.
NSPredicate를 위의 더러운 7줄 짜리 코드를 아래와 같이 2줄로 줄일 수 있다.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", arron-shin"darren"]; NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
끝! 맺으며
참고 자료
Stack overflow: NSArray find object or objects - best practices
Stack overflow: Search NSArray for value matching value
NSPredicate Cheatsheet
Download
Steps in part 4: 1. Create Hobby Entity - change Core Data model 2. Create and Save hobbies to user. 3. SortDescriptor and NSPredicate Sorry for the end of the video, i finally did product -clean, and then it worked fine. But i just present the way of using filteredArray with Predicate. The correct, best solution at this App to using the simple setPredicate in AppDelegate FetchRequest.! Full tutorial here! Xcode template file on Github here