I had a result that i wanted (getting max value.) with using keypath feature by someone’s help.
The code : getting a max value
NSNumber *number1 = [[NSNumber alloc]initWithInt:10];
NSNumber *number2 = [[NSNumber alloc]initWithInt:20];
NSNumber *number3 = [[NSNumber alloc]initWithInt:30];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:number1, number2, number3, nil];
NSNumber *result = [array valueForKeyPath:@"@min.intValue"];
I wonder about various usage of keypath.
Below are questions.
Q1) How to get an instance of having a max value of int_test property?
Q2) How to get a max value of int_test property in Just instances?
Just *test1 = [[Just alloc]init];
test1.int_test = 10;
Just *test2 = [[Just alloc]init];
test2.int_test = 20;
Just *test3 = [[Just alloc]init];
test3.int_test = 30;
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:test1, test2, test3, nil];
NSLog(@"%@",[array valueForKeyPath:@"?????"]); <= Q1)
NSLog(@"%d",[array valueForKeyPath:@"?????"]); <= Q2)
Note: Untested code ahead. Ideally, I’d test this before posting as an answer, but I don’t have time to do that just now. I believe the code is correct conceptually, but you may need to tweak it to get it to work. If you find any errors, please let me know so that I can make the necessary corrections.
Use this:
Use the NSArray method
-indexOfObjectPassingTest:like this: