Question

I am creating an rss feed app with a slide out navigation bar as shown here(http://www.appcoda.com/ios-programming-sidebar-navigation-menu/). The app will load and RSS feeds will parse and appear on my main screen. You can click on a feed and it will lead to a webView to show that corresponding website. I will also have a navigation bar button on the top left to toggle the slide out menu. I am not able to proceed working on my app because it keeps crashing.BTW, I'm using a third party library called SWRevealViewController.Here is my MasterViewController:

//
//  JSSMasterViewController.m
//  News App
//
//  Created by Steve on 4/5/14.
//  Copyright (c) 2014 self.edu.steve. All rights reserved.
//

#import "JSSMasterViewController.h"
#import "JSSDetailViewController.h"
#import "SWRevealViewController.h"

@interface JSSMasterViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;
}
@end

@implementation JSSMasterViewController

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

    // Change button color
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];

    // Set the side bar button action. When it's tapped, it'll show up the sidebar.
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);

    // Set the gesture
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];


}

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

#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        link    = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];

        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [link appendString:string];
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
        [[segue destinationViewController] setUrl:string];

    }
}
@end

Here is SideBar View controller that manages the Slide out menu:

    //
//  JSSSidebarViewController.m
//  News App
//
//  Created by Steve on 4/5/14.
//  Copyright (c) 2014 self.edu.steve. All rights reserved.
//

#import "JSSSidebarViewController.h"
#import "SWRevealViewController.h"

@interface JSSSidebarViewController ()

@end

@implementation JSSSidebarViewController{

    NSArray *menuItems;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

     menuItems = @[@"title", @"news", @"comments", @"map", @"calendar", @"wishlist", @"bookmark", @"tag"];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [menuItems count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
    // Set the title of navigation bar by using the menu items
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
    destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];

    // Set the photo if it navigates to the PhotoView

    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
        SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;

        swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

            UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
            [navController setViewControllers: @[dvc] animated: NO ];
            [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
        };

    }

}

@end

I am not able to find out the solution. Here is my logs:

2014-04-05 20:17:43.843 News App[4496:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101968495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016c799e objc_exception_throw + 43
    2   CoreFoundation                      0x000000010191f374 -[__NSArrayM insertObject:atIndex:] + 820
    3   UIKit                               0x00000001002d40da -[UIView(UIViewGestures) addGestureRecognizer:] + 199
    4   News App                            0x0000000100001ecf -[JSSMasterViewController viewDidLoad] + 719
    5   UIKit                               0x000000010036a59e -[UIViewController loadViewIfRequired] + 562
    6   UIKit                               0x000000010036a777 -[UIViewController view] + 29
    7   UIKit                               0x00000001006752e2 -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 390
    8   UIKit                               0x00000001002b0ffa -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1109
    9   UIKit                               0x00000001002b0b9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    10  UIKit                               0x00000001002b0aef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
    11  UIKit                               0x00000001002afdfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
    12  UIKit                               0x000000010036e70a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
    13  UIKit                               0x00000001002aab1b -[UIWindow addRootViewControllerViewIfPossible] + 490
    14  UIKit                               0x00000001002aac70 -[UIWindow _setHidden:forced:] + 282
    15  UIKit                               0x00000001002b3ffa -[UIWindow makeKeyAndVisible] + 51
    16  UIKit                               0x000000010026fc98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
    17  UIKit                               0x0000000100273a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    18  UIKit                               0x0000000100284d4c -[UIApplication handleEvent:withNewEvent:] + 3189
    19  UIKit                               0x0000000100285216 -[UIApplication sendEvent:] + 79
    20  UIKit                               0x0000000100275086 _UIApplicationHandleEvent + 578
    21  GraphicsServices                    0x0000000103ae171a _PurpleEventCallback + 762
    22  GraphicsServices                    0x0000000103ae11e1 PurpleEventCallback + 35
    23  CoreFoundation                      0x00000001018ea679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    24  CoreFoundation                      0x00000001018ea44e __CFRunLoopDoSource1 + 478
    25  CoreFoundation                      0x0000000101913903 __CFRunLoopRun + 1939
    26  CoreFoundation                      0x0000000101912d83 CFRunLoopRunSpecific + 467
    27  UIKit                               0x00000001002732e1 -[UIApplication _run] + 609
    28  UIKit                               0x0000000100274e33 UIApplicationMain + 1010
    29  News App                            0x0000000100002c23 main + 115
    30  libdyld.dylib                       0x00000001020005fd start + 1
    31  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Thank you for any Help!

Was it helpful?

Solution

The error is exactly what your error log says:

'*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

You are trying to insert an object to an array and that object is nil. In this particular case, it's this line that's causing problem:

[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

You will need to alloc self.revealViewController.panGestureRecognizer first before adding it as the view's gesture recognizers.

One way to alloc the gesture recognizer:

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(someMethod:)];
[self.view addGestureRecognizer:gestureRecognizer];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top