Note that there are some explanatory texts on larger screens.

plurals
  1. PONSArray returning NULL value even though right number of objects is counted and returned
    text
    copied!<p>I have created a NSArray that contains images objects. Based on the swipe direction recognized by UISwipeGestureRecognizerDirection the index is incremented up or down so the right image could be selected.</p> <p>The problem I have is that my NSArray is returning a NULL value instead of the image object. Therefore the UIImageView (named imageView) displays nothing.</p> <p>I am printing an NSLog statement in order to see what is really happening - example below:</p> <p>... [2307:60b] Swiped and the index is: 1, the image is (null) and the total number of objects 2</p> <p>As per NSLog statement above we can see that: - The number of objects is perfectly counted. - NSArray index increments perfectly up and down. - But a NULL value is returned for the image object.</p> <p>My code below (see the handleSwipe method):</p> <pre><code>@synthesize imageView; int imageIndex = 1; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, CGRectGetHeight(self.view.bounds)/2.0f+20); [self.view addSubview:imageView]; UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [self.view addGestureRecognizer:swipeRightGesture]; swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight; UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [self.view addGestureRecognizer:swipeLeftGesture]; swipeRightGesture.direction = UISwipeGestureRecognizerDirectionLeft; UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)]; self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil]; } - (IBAction)handleSwipe:(UIGestureRecognizer *)sender { int theTotal; NSArray *images=[[NSArray alloc] initWithObjects: @"image021.jpg", @"image041.jpg", nil]; UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction]; switch (direction) { case UISwipeGestureRecognizerDirectionLeft: imageIndex++; break; case UISwipeGestureRecognizerDirectionRight: imageIndex--; break; default: break; } imageIndex = (imageIndex &lt; 0) ? ([images count] -1): imageIndex % [images count]; imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]]; theTotal = [images count]; NSLog(@"Swiped and the index is: %d, the image is %@ and the total number of objects %d", imageIndex, imageView.image, theTotal); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload