如何从 NSDictionary 中获取属性?

发布于 2024-11-18 00:09:43 字数 554 浏览 4 评论 0原文

我有一个 Web 服务,它以 JSON 格式将数据返回到我的客户端应用程序。我正在使用 TouchJson 将此数据反序列化为 NSDictionary。伟大的。字典包含一个键“结果”,结果包含一个 NSArray 对象。

这些对象在打印到日志时看起来像这样,即

NSLog(@"Results Contents: %@",[resultsArray objectAtIndex:0 ]);

输出:

Results Contents: {
    creationdate = "2011-06-29 22:03:24";        
    id = 1;
    notes = "This is a test item";
    title = "Test Item";
    "users_id" = 1;
}

我无法确定该对象是什么类型。如何从该对象获取属性和值?

谢谢!

I have a webservice that returns data to my client application in JSON. I am using TouchJson to then deserialize this data into a NSDictionary. Great. The dictionary contains a key, "results" and results contains an NSArray of objects.

These objects look like this when printed to log i.e

NSLog(@"Results Contents: %@",[resultsArray objectAtIndex:0 ]);

Outputs:

Results Contents: {
    creationdate = "2011-06-29 22:03:24";        
    id = 1;
    notes = "This is a test item";
    title = "Test Item";
    "users_id" = 1;
}

I can't determine what type this object is. How do I get the properties and values from this object?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2024-11-25 00:09:43

要获取 NSDictionary 的内容,您必须提供要检索的值的键。 IE:

NSString *notes = [[resultsArray objectAtIndex:0] valueForKey:@"notes"];

To get the content of a NSDictionary you have to supply the key for the value that you want to retrieve. I.e:

NSString *notes = [[resultsArray objectAtIndex:0] valueForKey:@"notes"];
执笏见 2024-11-25 00:09:43

要测试对象是否是类 a 的实例,请使用以下之一:

[yourObject isKindOfClass:[a class]]

[yourObject isMemberOfClass:[a class]]

要获取对象的类名,您可以使用以下之一:

const char* className = class_getName([yourObject class]);

NSString *className = NSStringFromClass([yourObject class]); 

对于 NSDictionary,您可以使用 < code>-allKeys 返回字典键的 NSArray。这还会让您知道有多少个(通过获取数组的计数)。一旦知道类型,您就可以调用

[[resultsArray objectAtIndex:0] objectForKey:keyString];

其中 keyString@"creationdate"@"notes" 等之一。但是,如果该类不是 NSObject 的子类,然后使用:

[[resultsArray objectAtIndex:0] valueForKey:keyString];

例如,您可能需要对等于 @"id"keystring 执行此操作>。

To test if object is an instance of class a, use one of these:

[yourObject isKindOfClass:[a class]]

[yourObject isMemberOfClass:[a class]]

To get object's class name you can use one of these:

const char* className = class_getName([yourObject class]);

NSString *className = NSStringFromClass([yourObject class]); 

For an NSDictionary, you can use -allKeys to return an NSArray of dictionary keys. This will also let you know how many there are (by taking the count of the array). Once you know the type, you can call

[[resultsArray objectAtIndex:0] objectForKey:keyString];

where keyString is one of @"creationdate", @"notes", etc. However, if the class is not a subclass of NSObject, then instead use:

[[resultsArray objectAtIndex:0] valueForKey:keyString];

for example, you probably need to do this for keystring equal to @"id".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文