簡體   English   中英

在Obj-C中解析RSS?

[英]Parsing RSS in Obj-C?

按照本教程 ,我嘗試解析RSS日期以在我的應用中查看。 我有一個JSON文件,可從屏幕中檢索信息並顯示在屏幕上。

這是代碼

//
//  ViewController.m
//  JSONyt
//
//  Created by yo_291 on 3/16/15.
//  Copyright (c) 2015 yo_291. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

{
    NSMutableData *webData;
    NSURLConnection *connection;
    NSMutableArray *array;
}

@end

@implementation ViewController



- (void)viewDidLoad {

    [[self myView]setDelegate:self];

    [[self myView]setDataSource:self];

    array = [[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.
}


-(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(@"fail with error");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSDictionary *rd = [allDataDictionary objectForKey:@"responseData"];
    NSDictionary *feed = [rd objectForKey:@"feed"];
    NSArray *aoe = [feed objectForKey:@"entries"];

    for(NSDictionary *diction in aoe)
    {
        NSDictionary *numb = [diction objectForKey:@"0"];
        NSString *title = [numb objectForKey:@"title"];


        [array addObject:title];

        }

    [[self myView] reloadData];


}


- (IBAction)getFeed:(id)sender {

    NSURL *url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/services/feed/load?v=2.0&q=http://www.britannica.com/feeds/tdih.rss&num="];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if(connection)
    {
        webData = [[NSMutableData alloc]init];

    }


}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellI = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellI];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    return cell;
}


@end

應用程序在行[array addObject:title];上崩潰[array addObject:title];

錯誤堆棧跟蹤:

2015-03-16 22:32:51.650 JSONyt[8195:547408] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ba28a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b6c1bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010b8f70ca -[__NSArrayM insertObject:atIndex:] + 954
    3   JSONyt                              0x000000010b18da5f -[ViewController connectionDidFinishLoading:] + 735
    4   CFNetwork                           0x000000010d85998c __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 69
    5   CFNetwork                           0x000000010d859930 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199
    6   CFNetwork                           0x000000010d859a97 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48
    7   CFNetwork                           0x000000010d729937 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
    8   CFNetwork                           0x000000010d7f6a31 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 273
    9   CFNetwork                           0x000000010d714d16 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72
    10  CoreFoundation                      0x000000010b930b44 CFArrayApplyFunction + 68
    11  CFNetwork                           0x000000010d714bd7 _ZN19RunloopBlockContext7performEv + 133
    12  CFNetwork                           0x000000010d714a16 _ZN17MultiplexerSource7performEv + 256
    13  CFNetwork                           0x000000010d71482c _ZN17MultiplexerSource8_performEPv + 72
    14  CoreFoundation                      0x000000010b95dc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x000000010b953b5d __CFRunLoopDoSources0 + 269
    16  CoreFoundation                      0x000000010b953194 __CFRunLoopRun + 868
    17  CoreFoundation                      0x000000010b952bc6 CFRunLoopRunSpecific + 470
    18  GraphicsServices                    0x000000010f005a58 GSEventRunModal + 161
    19  UIKit                               0x000000010be18580 UIApplicationMain + 1282
    20  JSONyt                              0x000000010b18e483 main + 115
    21  libdyld.dylib                       0x000000010dfd6145 start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

但我不知道如何解決它。 在我看來,我正在正確搜索JSON文件,但是我可能錯了。 JSON文件也是RSS提要,我已經使用Google工具將其“轉換”為JSON數據庫,因此我不確定是否存在復雜性。

讓我知道需要的任何/所有信息,這是我的第一篇文章,所以我不知道我是否提供了足夠的上下文。

該錯誤表明要插入的對象為nil 問題在於以下幾行:

NSDictionary *numb = [diction objectForKey:@"0"];
NSString *title = [numb objectForKey:@"title"];
[array addObject:title];

在將對象添加到數組之前,您永遠不會檢查title是否為nil 您始終可以使用NSLog()調試值,並使用類似以下技術來檢查nil值:

if([numb objectForKey:@"title"] != nil) {
  [array addObject:[numb objectForKey:@"title"]];
}

ps為title分配值是沒有用的,因為title僅使用了一次(浪費內存)。

另外,使用AFNetworking庫將使您的代碼更簡單,並提供額外的錯誤處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM