Probably everyone to -(ID) Valueforkeypath: (NSString *) KeyPath method is not very understanding.
In fact, this method is very powerful, for example:
Nsarray *array = @[@"name"@ "w"@ "aa" @ "jimsa"]; NSLog (@ "%@", [array Valueforkeypath:@ "uppercasestring "]); output ( NAME, W, AA, Jimsa)
View Code
The equivalent of each member of the array uppercaseString
executes the method and then returns the returned object as a new array.
Since the method uppercaseString
can be used, then other methods of NSString , such as
[Array Valueforkeypath:@ "length"]
View Code
Returns an array of the components of each string length. As long as you can think of the member instance method can be so used.
If you think this method is a bit of a function, it's wrong. Let's take a concrete example and keep looking down.
RightNSNumberAn array calculates the sum, mean, maximum, minimum of an array quickly
Nsarray *array = @[@1, @2, @3, @4, @Ten]; NSNumber*sum = [Array Valueforkeypath:@"@sum. Self"]; NSNumber*avg = [Array Valueforkeypath:@"@avg. Self"]; NSNumber*max = [Array Valueforkeypath:@"@max. Self"]; NSNumber*min = [Array Valueforkeypath:@"@min. Self"];
View Code
Or specify the output type
1 nsnumber *sum = [array valueforkeypath:@ " @sum. Floatvalue " ]; 2 nsnumber *avg = [array valueforkeypath:@ " @avg. Floatvalue " ]; 3 nsnumber *max = [array valueforkeypath:@ " @max. Floatvalue " ]; 4 nsnumber *min = [array valueforkeypath:@ " @min. Floatvalue "];
View CodeEliminate duplicate data
1Nsarray *array = @[@"name",@"W",@"AA",@"Jimsa",@"AA"];2NSLog (@"%@", [array Valueforkeypath:@"@distinctUnionOfObjects. Self"]);
View Code
1 Print 2 3 (4name,5W,6Jimsa,7AA 8 )
View CodeRight
nsdictionaryArray to quickly find the corresponding
KeyThe value of the pair
1Nsarray *array = @[@{@"name":@"cookeee",@"Code": @1},2@{@"name":@"Jim",@"Code": @2},3@{@"name":@"Jim",@"Code": @1},4@{@"name":@"Jbos",@"Code": @1}];5NSLog (@"%@", [array Valueforkeypath:@"name"]);
View Code
An array of values directly from the name
key in the dictionary, which is obviously easier to add to the new array than the loop value
1 (2 cookeee,3 Jim,4 Jim, 5 jbos6 )
View Code
Also can be nested use, first eliminate name
the corresponding value of the duplicate data and then take the value
1Nsarray *array = @[@{@"name":@"cookeee",@"Code": @1},2@{@"name":@"Jim",@"Code": @2},3@{@"name":@"Jim",@"Code": @1},4@{@"name":@"Jbos",@"Code": @1}];5 6NSLog (@"%@", [array Valueforkeypath:@"@distinctUnionOfObjects. Name"]);7 8 Print9 (Ten Cookeee, One Jim, A Jbos -)
View CodeChangeUitextfiedlOf
placeholderThe color
1 [Searchfield Setvalue:[uicolor Whitecolor] Forkeypath:@ "_placeholderlabel.textcolor" ];
View Code
It's a - (void)drawPlaceholderInRect:(CGRect)rect;
lot easier than rewriting.
Go to: http://segmentfault.com/a/1190000000526660
The Magical magic of Valueforkeypath (turn)