如何检查 iPhone 中已解析元素的条件

发布于 2024-11-02 08:47:53 字数 389 浏览 1 评论 0原文

从我项目中的 Json 解析中我得到这些元素...

执行指令:-

NSArray *feed3 = (NSArray *)[feed valueForKey:@"type"];  
NSLog(@" %@",feed3);

在控制台中我得到这个

( 状态,
照片,
链接,
视频)

现在我想检查这些元素的条件..

比如

if(type==staus){  
//do some thing  
} 

如何在 xcode 中执行此操作?

from the Json parsing in my project I am getting these elements...

executing instruction:-

NSArray *feed3 = (NSArray *)[feed valueForKey:@"type"];  
NSLog(@" %@",feed3);

in console I am getting this

(
status,
photo,
link,
video )

Now I want to check condition for these elements..

like

if(type==staus){  
//do some thing  
} 

how to do this in xcode?

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

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

发布评论

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

评论(2

昨迟人 2024-11-09 08:47:53

我假设 feed3 是 JSON 解析器在解析您在另一个问题中列出的 JSON 数据后返回的对象。在这种情况下:

* the top level object is an array
* every element in the array is an object/dictionary representing news
* this object/dictionary contains the following keys:
  * application (object/dictionary with two keys: id, name)
    * id (number)
    * name (string)
  * created_time (string)
  * from (object/dictionary with two keys: id, name)
    * id (number)
    * name (string)
  * icon (string)
  * id (string)
  * likes (object/dictionary with two keys: count, data)
    * count (number)
    * data (array)
      * every element in the array is an object/dictionary
      * this object/dictionary has two keys (id, name)
        * id (number)
        * name (string)
  * link (string)
  * name (string)
  * picture (string)
  * properties (array of objects/dictionaries)
  * type (string)
  * updated_time (string)

解析 JSON 数据时,了解数据的组织方式至关重要。我建议您在需要解析 JSON 时始终执行上述操作。

由于您对“类型”感兴趣,因此您需要遵循以下路径:

  • 遍历新闻的顶级数组
    • 数组中的每个元素都是一个对象/字典
      • 这个对象/字典有一个名为“type”的键

下面的代码应该可以解决这个问题:

for (NSDictionary *news in feed3) {
    NSString *type = [news objectForKey:@"type"];

    if ([type isEqualToString:@"status"]) {
       …
    }
    else if ([type isEqualToString:@"photo"]) { 
       …
    }
    else if ([type isEqualToString:@"link"]) { 
       …
    }
    else if ([type isEqualToString:@"video"]) { 
       …
    }
}

请注意,一般来说,您应该使用 -objectForKey: 而不是 -valueForKey::

  • -objectForKey: 是在NSDictionary 中声明的方法,用于获取存储在给定相应键的字典中的对象。
  • -valueForKey: 是一种 KVC 方法,有另一个用途。特别是,当您不期望时,它可以返回一个数组!

I’m assuming that feed3 is the object returned by the JSON parser after having parsed the JSON data you listed in another question. In that case:

* the top level object is an array
* every element in the array is an object/dictionary representing news
* this object/dictionary contains the following keys:
  * application (object/dictionary with two keys: id, name)
    * id (number)
    * name (string)
  * created_time (string)
  * from (object/dictionary with two keys: id, name)
    * id (number)
    * name (string)
  * icon (string)
  * id (string)
  * likes (object/dictionary with two keys: count, data)
    * count (number)
    * data (array)
      * every element in the array is an object/dictionary
      * this object/dictionary has two keys (id, name)
        * id (number)
        * name (string)
  * link (string)
  * name (string)
  * picture (string)
  * properties (array of objects/dictionaries)
  * type (string)
  * updated_time (string)

When parsing JSON data, it’s vital to understand how data is organised. I suggest you always do the above whenever you have to parse JSON.

Since you’re interested in ‘type’, you need to follow this path:

  • traverse the top level array of news
    • every element in the array is an object/Dictionary
      • this object/dictionary has a key called ‘type’

The following code should do the trick:

for (NSDictionary *news in feed3) {
    NSString *type = [news objectForKey:@"type"];

    if ([type isEqualToString:@"status"]) {
       …
    }
    else if ([type isEqualToString:@"photo"]) { 
       …
    }
    else if ([type isEqualToString:@"link"]) { 
       …
    }
    else if ([type isEqualToString:@"video"]) { 
       …
    }
}

Note that, in general, you should use -objectForKey: instead of -valueForKey::

  • -objectForKey: is a method declared in NSDictionary and it is used to obtain an object stored in the dictionary given the corresponding key.
  • -valueForKey: is a KVC method and serves another purpose. In particular, it can return an array when you’re not expecting one!
独享拥抱 2024-11-09 08:47:53

检查下面

for(int index = 0 ; index < [feed3 count] ; index++)
{
    NSString* tempString = [feed3 objectAtIndex:index];

    if([tempString isEqualToString:@"status"]) 
    {
      //Get value for status from value array
    }
    else if([tempString isEqualToString:@"photo"]) 
    {
      //Get value for photo from value array
    }
    else if([tempString isEqualToString:@"link"]) 
    {
      //Get value for link from value array
    }
    else if([tempString isEqualToString:@"video"]) 
    {
      //Get value for video from value array
    }
}

Check below

for(int index = 0 ; index < [feed3 count] ; index++)
{
    NSString* tempString = [feed3 objectAtIndex:index];

    if([tempString isEqualToString:@"status"]) 
    {
      //Get value for status from value array
    }
    else if([tempString isEqualToString:@"photo"]) 
    {
      //Get value for photo from value array
    }
    else if([tempString isEqualToString:@"link"]) 
    {
      //Get value for link from value array
    }
    else if([tempString isEqualToString:@"video"]) 
    {
      //Get value for video from value array
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文