Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone _NSCFString allKeys,无法识别的选择器。我以为那是一本字典?_Iphone_Arrays_Xcode_Uitableview_Nsdictionary - Fatal编程技术网

Iphone _NSCFString allKeys,无法识别的选择器。我以为那是一本字典?

Iphone _NSCFString allKeys,无法识别的选择器。我以为那是一本字典?,iphone,arrays,xcode,uitableview,nsdictionary,Iphone,Arrays,Xcode,Uitableview,Nsdictionary,我想弄清楚如何实现tableView numberofRowsinSection,这让我发疯,我希望能得到一些提示或建议。我觉得我把整个事情复杂化了。首先,让我将转换后的xml数据发布到NSDictionary中 { "@body" = ""; "@class_id" = ""; "@title" = Discussions; "@type" = "Discussion Block"; }, { "@body" = ""; "@class_id"

我想弄清楚如何实现
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;
        }
    );
}
)
{
    "@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" = "";
    "@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;
    };
},
numberofrowsinssection
方法是事情变得棘手的地方。这就是我试过的

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,不能在上面使用所有键。我不确定为什么会发生这种情况,因为当我将其更改为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方法做了很多研究,但我不确定如何解决此问题或以其他方式编写此代码。

问题在于:

for(NSDictionary*myDict在[[响应对象索引:部分] valueForKeyPath:@“项”]){

这对于下面的字典很好,因为它通过NSDictionary对象的NSArray循环。因此,当调用
[myDict allKeys]
时,实际上是在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;
        }
    );
}
)
{
    "@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" = "";
    "@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;
}
让我们假设,如果“item”键存在,那么相应的对象就是这些项的NSArray

尝试:


好的,这很有意义,谢谢。因此,我使用的方法只有在每个标题有2个或更多项的情况下才有效?您有其他方法来设置此表吗?太棒了,非常感谢。我添加了其他{return 0},以便在没有项值ForkeyPath的情况下不会显示任何行。