关闭

valueForkeyPath方法使用

74人阅读 评论(0) 收藏 举报
分类:

可能大家对- (id)valueForKeyPath:(NSString *)keyPath方法不是很了解。
其实这个方法非常的强大,举个例子:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-built_in" style="color: rgb(38, 139, 210);">NSArray</span> *array = @[<span class="hljs-string" style="color: rgb(42, 161, 152);">@"name"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"w"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"aa"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"jimsa"</span>];
<span class="hljs-built_in" style="color: rgb(38, 139, 210);">NSLog</span>(<span class="hljs-string" style="color: rgb(42, 161, 152);">@"%@"</span>, [array valueForKeyPath:<span class="hljs-string" style="color: rgb(42, 161, 152);">@"uppercaseString"</span>]);
</code>

输出

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">(
    NAME,
    W,
    AA,
    JIMSA
)
</code>

相当于数组中的每个成员执行了uppercaseString方法,然后把返回的对象组成一个新数组返回。既然可以用uppercaseString方法,那么NSString的其他方法也可以,比如

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">[array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"length"</span>]
</code>

返回每个字符串长度的组成的数组。只要你能想到的成员实例方法都可以这么用。


如果你觉得这个方法就这么点功能,那就错了。还是举具体的例子

对NSNumber数组快速计算数组求和、平均数、最大值、最小值

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSArray</span> *array = @[@<span class="hljs-number" style="color: rgb(42, 161, 152);">1</span>, @<span class="hljs-number" style="color: rgb(42, 161, 152);">2</span>, @<span class="hljs-number" style="color: rgb(42, 161, 152);">3</span>, @<span class="hljs-number" style="color: rgb(42, 161, 152);">4</span>, @<span class="hljs-number" style="color: rgb(42, 161, 152);">10</span>];

    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *sum = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@sum</span>.self"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *avg = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@avg</span>.self"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *max = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@max</span>.self"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *min = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@min</span>.self"</span>];
</code>

或者指定输出类型

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *sum = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@sum</span>.floatValue"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *avg = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@avg</span>.floatValue"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *max = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@max</span>.floatValue"</span>];
    <span class="hljs-title" style="color: rgb(133, 153, 0);">NSNumber</span> *min = [array valueForKeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"<span class="hljs-variable" style="color: rgb(181, 137, 0);">@min</span>.floatValue"</span>];
</code>

剔除重复数据

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">    <span class="hljs-built_in" style="color: rgb(38, 139, 210);">NSArray</span> *array = @[<span class="hljs-string" style="color: rgb(42, 161, 152);">@"name"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"w"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"aa"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"jimsa"</span>, <span class="hljs-string" style="color: rgb(42, 161, 152);">@"aa"</span>];
    <span class="hljs-built_in" style="color: rgb(38, 139, 210);">NSLog</span>(<span class="hljs-string" style="color: rgb(42, 161, 152);">@"%@"</span>, [array valueForKeyPath:<span class="hljs-string" style="color: rgb(42, 161, 152);">@"@distinctUnionOfObjects.self"</span>]);
</code>

打印

(
name,
w,
jimsa,
aa
)

对NSDictionary数组快速找出相应key对的值

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-constant" style="color: rgb(181, 137, 0);">NSArray</span> *array = @[@{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"cookeee"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jim"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@2</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jim"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jbos"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>}];
<span class="hljs-constant" style="color: rgb(181, 137, 0);">NSLog</span>(@<span class="hljs-string" style="color: rgb(42, 161, 152);">"%@"</span>, [array <span class="hljs-symbol" style="color: rgb(203, 75, 22);">valueForKeyPath:</span>@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span>]);
</code>

直接得到字典中namekey对应的值组成的数组,显然比循环取值再加入到新数组中方便快捷

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">(
    cookeee,
    jim,
    jim,
    jbos
)
</code>

同样可以嵌套使用,先剔除name对应值的重复数据再取值

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"> <span class="hljs-constant" style="color: rgb(181, 137, 0);">NSArray</span> *array = @[@{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"cookeee"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jim"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@2</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jim"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>},
                           @{@<span class="hljs-string" style="color: rgb(42, 161, 152);">"name"</span><span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> @<span class="hljs-string" style="color: rgb(42, 161, 152);">"jbos"</span>,@<span class="hljs-string" style="color: rgb(42, 161, 152);">"code"</span> <span class="hljs-symbol" style="color: rgb(203, 75, 22);">:</span> <span class="hljs-variable" style="color: rgb(181, 137, 0);">@1</span>}];

    <span class="hljs-constant" style="color: rgb(181, 137, 0);">NSLog</span>(@<span class="hljs-string" style="color: rgb(42, 161, 152);">"%@"</span>, [array <span class="hljs-symbol" style="color: rgb(203, 75, 22);">valueForKeyPath:</span>@<span class="hljs-string" style="color: rgb(42, 161, 152);">"@distinctUnionOfObjects.name"</span>]);
</code>

打印
(
cookeee,
jim,
jbos
)

改变UITextfiedl的placeholder的颜色

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">    [searchField <span class="hljs-built_in" style="color: rgb(38, 139, 210);">set</span>Value:[UIColor whiteColor] <span class="hljs-keyword" style="color: rgb(133, 153, 0);">for</span>KeyPath:@<span class="hljs-string" style="color: rgb(42, 161, 152);">"_placeholderLabel.textColor"</span>];
</code>

比起重写- (void)drawPlaceholderInRect:(CGRect)rect;要方便很多

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16828次
    • 积分:282
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:69篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论