我正在尝试以下代码,但它抛出异常

时间:2015-02-19 06:12:02

标签: ios

It is showing exception (signal sigabert) at  return UIApplicationMain(argc, argv, nil, NSStringFromClass([xyzAppDelegate class]));

例外细节是:  2015-02-19 13:09:02.825 try11feb2 [377:11303] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [__ NSArrayM insertObject:atIndex:]:对象不能是零'     ***第一次抛出调用堆栈:     (0x1c92012 0x10cfe7e 0x1c45b6a 0x1c45a20 0x26ab 0xbd5589 0xbd3652 0xbd489a 0xbd360d 0xbd3785 0xb20a68 0x46156dc 0x4613bb3 0x4651cda 0x1c348fd 0x465235c 0x46522d5 0x453c250 0x1c15f3f 0x1c1596f 0x1c38734 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x1c3d 0x1b65为0x1)     libc ++ abi.dylib:terminate调用抛出异常     (lldb)

import“xyzViewController.h”

    @interface xyzViewController ()

    @end

    @implementation xyzViewController
    @synthesize mytv;

    - (void)viewDidLoad
    {
        tabledata=[[NSMutableArray alloc]init];
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (IBAction)btnpressed:(id)sender {

        NSString *str=@"http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/json";
        NSURL *url=[[NSURL alloc]initWithString:str];
        NSURLRequest *req=[[NSURLRequest alloc]initWithURL:url];
        conn=[[NSURLConnection alloc]initWithRequest:req delegate:self];
        if (conn) {
            webdata=[[NSMutableData alloc]init];
        }

    }
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [webdata setLength:0];
    }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [webdata appendData:data];
    }
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"failed");
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {

        NSDictionary *ald=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:Nil];
        NSDictionary *feed=[ald objectForKey:@"feed"];
        NSArray *arrayofentry=[feed objectForKey:@"entry"];
        for (NSDictionary *dic in arrayofentry)
        {  NSDictionary *zero=[dic objectForKey:0];
            NSDictionary *title=[zero objectForKey:@"title"];
            NSString *label=[title objectForKey:@"label"];

            [tabledata addObject:label];
             // NSLog(@"%@",label);}
          //  tabledata =[[NSArray alloc]initWithObjects:label, nil];
            [[self mytv]reloadData];
        }
[[self mytv]reloadData];
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [tabledata count];
    }
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        }
cell.textLabel.text=[tabledata objectAtIndex:indexPath.row];
        return cell;
    }
    @end

1 个答案:

答案 0 :(得分:0)

您的解析代码完全错误。我不知道您需要在表视图中加载什么值...例如我已经解析了标签键值。它可以帮助您,只需更改此代码。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSDictionary *ald=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:Nil];
    NSDictionary *feed=[ald objectForKey:@"feed"];
    NSArray *arrayofentry=[feed objectForKey:@"entry"];
    NSLog(@"%@",arrayofentry);
    for (NSDictionary *dic in arrayofentry)
    {
        NSDictionary *zero=[dic objectForKey:@"category"];
        NSString *label=[[zero objectForKey:@"attributes"]valueForKey:@"label"];

        [tabledata addObject:label];

    }
    [[self mtv]reloadData];
}

修改

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSDictionary *ald=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:Nil];
    NSDictionary *feed=[ald objectForKey:@"feed"];
    NSLog(@"%@",[feed allKeys]);
    NSArray *arrayofentry=[feed objectForKey:@"entry"];
    NSLog(@"%@",arrayofentry);
    for (NSDictionary *dic in arrayofentry)
    {
        //THIS LINE TELL YOU THAT ALL THE AVAILABLE KEYS IN DICTIONARY
        NSLog(@"%@",[dic allKeys]);

//        NSDictionary *zero=[dic objectForKey:@"category"];
//        NSString *label=[[zero objectForKey:@"attributes"]valueForKey:@"label"];


        // NSDictionary *zero=[dic objectForKey:@"title"];
        NSString *label=[[dic objectForKey:@"title"]valueForKey:@"label"];
        [tabledata addObject:label];

    }
    [[self mtv]reloadData];
}

如果你想要解析另一个值,请告诉我。