单键和键路径有什么区别? [英] what's the difference between single key and keypath?

查看:82
本文介绍了单键和键路径有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面中所示

[self setValue:[NSNumber numberWithInt:intValue] forKey:@"myObject.value"];

答案是当然,这是一个密钥路径而不是单个密钥",这是什么意思?

The answer was that "of course, it's a key path not a single key", What does that mean?

推荐答案

键是标识对象属性的字符串.键 path 是由点分隔的键的列表,用于标识嵌套的属性.

A key is a string that identifies a property of an object. A key path is a list of keys separated by dots, used to identify a nested property.

这是一个例子.如果对象person具有属性address,其本身具有属性town,则可以使用键分两步获取镇值:

Here's an example. If an object person has a property address, which itself has a property town you could get the town value in two steps using keys:

id address = [person valueForKey:@"address"];
id town = [address valueForKey:@"town"];

或一步一步使用keyPath:

or in one step using a keyPath:

id town = [person valueForKeyPath:@"address.town"];

Have a look at Apple's docs on Key-Value Coding for further details.

这篇关于单键和键路径有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆