_NSCFString allKeys,无法识别的选择器。我以为这是一个 NSDictionary ?

发布于 2024-12-26 11:48:22 字数 2920 浏览 0 评论 0原文

我正在疯狂地试图弄清楚如何实现tableView numberofRowsinSection,并希望我能得到一些提示或建议。我觉得我把整个事情过于复杂化了。让我首先发布转换后的 xml 数据,该数据现在是 NSDictionary。

{
    "@body" = "";
    "@class_id" = "";
    "@title" = Discussions;
    "@type" = "Discussion Block";
},
{
    "@body" = "";
    "@class_id" = "";
    "@col" = 1;
    "@title" = YouTube;
    "@type" = "Youtube Block";
    item =     {
        "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
        "@title" = "Gavin DeGraw - Not Over You";
        "@type" = link;
    };
},
{
    "@body" = "";
    "@class_id" = "";
    "@title" = Calendar;
    "@type" = "Calendar Block";
},
{
    "@body" = "<p>  homework test</p> ";
    "@class_id" = "";
    "@title" = Homework;
    "@type" = "File Block";
    item =     (
                {
            "@body" = "https://MYDOMAIN.com/securelink;
            "@title" = "crashreport.rtf";
            "@type" = file;
        },
                {
            "@body" = "mydomain.com/securelink";
            "@title" = "Chem notes sep 26.docx";
            "@type" = file;
    }
);
},
{
    "@body" = "<p>  Link testing</p> ";
    "@class_id" = "";
    "@title" = Links;
    "@type" = "Link Block";
    item =     (
                {
            "@body" = "http://google.com";
            "@title" = Link1;
            "@type" = link;
        },
                {
            "@body" = "http://yahoo.com";
            "@title" = link2;
            "@type" = link;
        },
                {
            "@body" = "http://reddit.com";
            "@title" = link3;
            "@type" = link;
        }
    );
}
)

以上数据为响应。这样做的目标是使第一个标题键成为表标题,使第二个“标题”键(在“项目”内)成为各自部分的行。在 tableView TitleforHeaderinSection 方法中,我只需使用此代码

for (NSDictionary *roster in response) {
     [blockName addObject:[roster objectForKey@"@title"]];
 }
     return [blockName objectAtIndex:section];

numberofRowsinSection 方法是事情变得棘手的地方。这就是我尝试过的

for (NSDictionary *myDict in [[response objectAtIndex:section] valueForKeyPath:@"item"]) {
    NSArray *keys = [myDict allKeys];
    if ([keys containsObject:@"@title"]) {
        array3 = [[NSMutableArray alloc] init];
        [array3 addObject:[myDict objectForKey:@"@title"]];
    }
}
 return array3;

我得到的错误是 myDict 是 NSString 并且 allKeys 不能在其上使用。我不确定为什么会发生这种情况,因为当我将其更改为 objectAtIndex:1 并记录它时, allKeys 方法有效。我也很确定这不是查找每个部分所需行数的最佳方法,因此如果有更好的方法请分享。

只是为了澄清,这就是我在解析上述数据后在 tableView 中寻找的内容:

 Header (Discussions)
   -No rows under this header
 Header (YouTube)
   -Row (Gavin DeGraw - Not Over You)
 Header (Calendar)
   -No rows
 Header (Homework)
   -Row (crashreport)
   -Row (chem notes)
 Header (links)
   -Row (link1)
   -Row (link2)
   -Row (link3)

感谢您的帮助。我对 NSDictionary 和 NSArray 方法做了很多研究,但我不确定如何解决这个问题或以其他方式编码。

I'm driving myself crazy trying to figure out how to implement tableView numberofRowsinSection and was hoping I could get some tips or suggestions. I feel like I've over-complicated the entire thing. Let me start by posting the converted xml data that is now an NSDictionary.

{
    "@body" = "";
    "@class_id" = "";
    "@title" = Discussions;
    "@type" = "Discussion Block";
},
{
    "@body" = "";
    "@class_id" = "";
    "@col" = 1;
    "@title" = YouTube;
    "@type" = "Youtube Block";
    item =     {
        "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
        "@title" = "Gavin DeGraw - Not Over You";
        "@type" = link;
    };
},
{
    "@body" = "";
    "@class_id" = "";
    "@title" = Calendar;
    "@type" = "Calendar Block";
},
{
    "@body" = "<p>  homework test</p> ";
    "@class_id" = "";
    "@title" = Homework;
    "@type" = "File Block";
    item =     (
                {
            "@body" = "https://MYDOMAIN.com/securelink;
            "@title" = "crashreport.rtf";
            "@type" = file;
        },
                {
            "@body" = "mydomain.com/securelink";
            "@title" = "Chem notes sep 26.docx";
            "@type" = file;
    }
);
},
{
    "@body" = "<p>  Link testing</p> ";
    "@class_id" = "";
    "@title" = Links;
    "@type" = "Link Block";
    item =     (
                {
            "@body" = "http://google.com";
            "@title" = Link1;
            "@type" = link;
        },
                {
            "@body" = "http://yahoo.com";
            "@title" = link2;
            "@type" = link;
        },
                {
            "@body" = "http://reddit.com";
            "@title" = link3;
            "@type" = link;
        }
    );
}
)

The above data is the response. The goal for this is to have the first title key be the table headers, and for the second "title" key (inside "item") to be the rows for their respective sections. In the tableView TitleforHeaderinSection method I simply use this code

for (NSDictionary *roster in response) {
     [blockName addObject:[roster objectForKey@"@title"]];
 }
     return [blockName objectAtIndex:section];

The numberofRowsinSection method is where things get tricky. This is what I tried

for (NSDictionary *myDict in [[response objectAtIndex:section] valueForKeyPath:@"item"]) {
    NSArray *keys = [myDict allKeys];
    if ([keys containsObject:@"@title"]) {
        array3 = [[NSMutableArray alloc] init];
        [array3 addObject:[myDict objectForKey:@"@title"]];
    }
}
 return array3;

The error that I'm getting is that myDict is an NSString and allKeys can't be used on it. I'm not sure why this is happening because when I change it to objectAtIndex:1 and log it the allKeys method works. I'm also pretty sure this isn't the best way to find the number of rows needed in each section, so if there is a better way please share.

Just to clarify, this is what I'm looking for in my tableView after parsing the above data:

 Header (Discussions)
   -No rows under this header
 Header (YouTube)
   -Row (Gavin DeGraw - Not Over You)
 Header (Calendar)
   -No rows
 Header (Homework)
   -Row (crashreport)
   -Row (chem notes)
 Header (links)
   -Row (link1)
   -Row (link2)
   -Row (link3)

Thanks for your help. I've done so much research on NSDictionary and NSArray methods, but I'm not sure how to fix this problem or code this any other way.

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

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

发布评论

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

评论(2

天冷不及心凉 2025-01-02 11:48:22

问题就在这里:

for (NSDictionary *myDict in [[响应 objectAtIndex:section]
valueForKeyPath:@"item"]) {

这对于下面的字典来说很好,因为它循环遍历 NSDictionary 对象的 NSArray。因此,当调用 [myDict allKeys] 时,您实际上是在 NSDictionary 上调用它。

{
    "@body" = "<p>  homework test</p> ";
    "@class_id" = "";
    "@title" = Homework;
    "@type" = "File Block";
    item =     (
                {
            "@body" = "https://MYDOMAIN.com/securelink;
            "@title" = "crashreport.rtf";
            "@type" = file;
        },
                {
            "@body" = "mydomain.com/securelink";
            "@title" = "Chem notes sep 26.docx";
            "@type" = file;
    }
);

但对于下一个字典来说并不好,因为您正在循环字典而不是数组,因此 for..in 循环正在循环字典的键。然后,您在 NSString(也称为字典中的键)上调用 allKeys

{
    "@body" = "";
    "@class_id" = "";
    "@col" = 1;
    "@title" = YouTube;
    "@type" = "Youtube Block";
    item =     {
        "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
        "@title" = "Gavin DeGraw - Not Over You";
        "@type" = link;
    };
},

你可以这样做:

id item = [[response objectAtIndex:section] valueForKeyPath:@"item"];
if(item) {
    if([item isKindOfClass:[NSDictionary class]]) {
        NSArray *keys = [(NSDictionary *)item allKeys];
    } else if([item isKindOfClass:[NSArray class]]) {
        for (NSDictionary *myDict in (NSArray *)item) {
            NSArray *keys = [myDict allKeys];
        }
    }
}
else {
  return 0;
}

The problem is here:

for (NSDictionary *myDict in [[response objectAtIndex:section]
valueForKeyPath:@"item"]) {

This is fine for the dictionary below, because it loops through an NSArray of NSDictionary objects. So when [myDict allKeys] is called, you are actually calling it on an NSDictionary.

{
    "@body" = "<p>  homework test</p> ";
    "@class_id" = "";
    "@title" = Homework;
    "@type" = "File Block";
    item =     (
                {
            "@body" = "https://MYDOMAIN.com/securelink;
            "@title" = "crashreport.rtf";
            "@type" = file;
        },
                {
            "@body" = "mydomain.com/securelink";
            "@title" = "Chem notes sep 26.docx";
            "@type" = file;
    }
);

But it is not fine for the next dictionary because you are looping through a dictionary instead of an array, so the for..in loop is looping the keys of the dictionary. Then, you are calling allKeys on an NSString (aka on a key in the dictionary).

{
    "@body" = "";
    "@class_id" = "";
    "@col" = 1;
    "@title" = YouTube;
    "@type" = "Youtube Block";
    item =     {
        "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
        "@title" = "Gavin DeGraw - Not Over You";
        "@type" = link;
    };
},

You could do something like this instead:

id item = [[response objectAtIndex:section] valueForKeyPath:@"item"];
if(item) {
    if([item isKindOfClass:[NSDictionary class]]) {
        NSArray *keys = [(NSDictionary *)item allKeys];
    } else if([item isKindOfClass:[NSArray class]]) {
        for (NSDictionary *myDict in (NSArray *)item) {
            NSArray *keys = [myDict allKeys];
        }
    }
}
else {
  return 0;
}
所谓喜欢 2025-01-02 11:48:22

假设如果“item”键存在,那么对应的对象是一个 NSArray 的项目。

尝试:

NSArray *items = [[response objectAtIndex:section] valueForKey:@"item"];

if (items!=nil) {
    return [items count];
} else {
    return 0;
}

Let's assume that if the "item" key exists then the corresponding object is an NSArray of the items.

Try:

NSArray *items = [[response objectAtIndex:section] valueForKey:@"item"];

if (items!=nil) {
    return [items count];
} else {
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文