Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iPad Application Development
This is the forum to discuss the Wrox book Beginning iPad Application Development by Wei-Meng Lee; ISBN: 9780470641651
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iPad Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 19th, 2011, 12:33 AM
Registered User
 
Join Date: Feb 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Totally baffled Obj-C noob

I'm totally baffled. Does any one have any idea how this can happen? In the many languages I've worked with, when you add an object to an array, the array ACTUALLY CONTAINS THAT OBJECT. Object-C doesn't seem to work that way. Ok-- that felt better. I'm doing Chapter 3 UsingViews2. Most of the code from below can be found on pp. 59-61. I'm trying to do things in a more elegant way-- I hate just copying stuff from a book.

BTW, it behaves the same way even it I don't declare it as a property.I'm baffled. I never initialize imageArray anywhere, as in:
NSMutableArray *array = [NSMutableArray initWithCapacity:16];

However, I never get a runtime error when I try to add objects to it or query its size, which is always 0. I've worked with Obj-C before and I've always had to initialize object before I used them or I would get an error. I'm not getting what has changed in this SDK.

my header file:
#import <UIKit/UIKit.h>
#import <stdlib.h>

@interface UsingViews2ViewController : UIViewController {

IBOutlet UIPageControl *pageControl;
IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
NSMutableArray *imageArray;
UIImageView *tempImageView, *bgImageView;
}

@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIImageView *imageView1;
@property (nonatomic, retain) UIImageView *imageView2;
@property (nonatomic, retain) NSMutableArray *imageArray;

@end

my implementation:

#import "UsingViews2ViewController.h"

@implementation UsingViews2ViewController

@synthesize pageControl;
@synthesize imageView1, imageView2;
@synthesize imageArray;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[imageArray addObject:@"P1010579.jpg"];
[imageArray addObject:@"P1010580.jpg"];
[imageArray addObject:@"P1010581.jpg"];
[imageArray addObject:@"P1010582.jpg"];
[imageArray addObject:@"P1010583.jpg"];

// I just added 5 objects to imageArray, unless my eyes deceive me...
NSLog(@"number of objects: %d", [imageArray count]);

//[imageView1 setImage:[UIImage imageNamed:[imageArray objectAtIndex:arc4random()%[imageArray count] ] ] ];
[imageView1 setImage:[UIImage imageNamed:[imageArray objectAtIndex:0 ] ] ];

tempImageView = imageView2;

//[pageControl numberOfPages:[imageArray count] ];
// -- make the first imageView visible and hide the second
[imageView1 setHidden:NO];
[imageView2 setHidden:YES];
[pageControl addTarget:self
action:@selector(pageTurning:)
forControlEvents:UIControlEventValueChanged];
[super viewDidLoad];
}

-(void) pageTurning: (UIPageControl *) pageController {
NSInteger nextPage = [pageController currentPage];
// at least my PageController works...
NSLog(@"turning page: %d", nextPage);
NSLog(@"number of objects: %d", [imageArray count]);
//NSEnumerator *imageArrayEnum = [imageArray objectEnumerator];
NSObject *object;
for ( NSString *object in imageArray ) {
// never executes these statements...
NSLog(@"selecting image %s",(NSString *)[imageArray objectAtIndex:nextPage]);
if ( [imageArray indexOfObject:object] == nextPage ) {
[tempImageView setImage:[UIImage imageNamed:(NSString *)object]];
break;
}
}

}


output of log file:

[Session started at 2011-02-18 23:10:14 -0500.]
2011-02-18 23:10:24.644 UsingViews2[41954:207] number of objects: 0
2011-02-18 23:10:24.646 UsingViews2[41954:207] turning page: 1
2011-02-18 23:10:24.646 UsingViews2[41954:207] number of objects: 0
2011-02-18 23:10:24.854 UsingViews2[41954:207] number of objects: 0
2011-02-18 23:10:24.854 UsingViews2[41954:207] turning page: 2
2011-02-18 23:10:24.855 UsingViews2[41954:207] number of objects: 0



As always-- thanks for any ideas.

-prm
Reply With Quote
  #2 (permalink)  
Old February 19th, 2011, 01:39 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

You have never allocated the array, and that is the problem.
imageArray = [[NSMutableArray alloc] init];
or
imageArray = [NSMutableArray array];
etc.
will resolve your issue.
Bob
Reply With Quote
  #3 (permalink)  
Old February 19th, 2011, 02:53 AM
Registered User
 
Join Date: Feb 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for your response. Unfortunately, that doesn't seem to do anything:
[imageArray initWithCapacity:32];
[imageArray addObject:@"P1010579.jpg"];
[imageArray addObject:@"P1010580.jpg"];
[imageArray addObject:@"P1010581.jpg"];
[imageArray addObject:@"P1010582.jpg"];
[imageArray addObject:@"P1010583.jpg"];
NSLog(@"number of objects: %d", [imageArray count]);

What baffles me is that the imageArray object never complains about being sent the 'addObject' and 'count' messages. My previous experience with XCode tells me that I should get some sort of error sending a specific message to an uninitialized object. Anyway, thanks for you help.
Reply With Quote
  #4 (permalink)  
Old February 19th, 2011, 09:24 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Since imageArray is not allocated it is set to nil. Sending messages to nil is perfectly valid in Objective C.

Insert
Code:
NSLog(@"imageArray is at %p",imageArray);
and you will see it points to 0x0 without an alloc.
Don't follow your response, about doesn't seem to do anything. You can't -initWithCapacity until you allocate first.
Code:
imageArray = [NSMutableArray arrayWithCapacity:16];
will allocate and initialize the array. Calling
Code:
[imageArray initWithCapacity:32];
without an alloc first does not work. It must be used in the nested
imageArray = [[NSMutableArray alloc] initWithCapacity:32];
Attempting to call it separately throws an exception, and causes a crash. The -initWithCapacity method is not and cannot be called directly by an instance of NSMutableArray. During the allocation and initialization process it is called by one of the class cluster's abstract private classes, whose public face is the NSMutableArray.


Acceptable allocation and initialization would be
Code:
imageArray = [NSMutableArray arrayWithCapacity:16];
[imageArray addObject:@"P1010579.jpg"];
[imageArray addObject:@"P1010580.jpg"];
[imageArray addObject:@"P1010581.jpg"];
[imageArray addObject:@"P1010582.jpg"];
[imageArray addObject:@"P1010583.jpg"];
NSLog(@"number of objects: %d", [imageArray count]);
or
Code:
imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:@"P1010579.jpg"];
[imageArray addObject:@"P1010580.jpg"];
[imageArray addObject:@"P1010581.jpg"];
[imageArray addObject:@"P1010582.jpg"];
[imageArray addObject:@"P1010583.jpg"];
NSLog(@"number of objects: %d", [imageArray count]);
or
Code:
imageArray = [[NSMutableArray alloc] initWithCapacity:16];
[imageArray addObject:@"P1010579.jpg"];
[imageArray addObject:@"P1010580.jpg"];
[imageArray addObject:@"P1010581.jpg"];
[imageArray addObject:@"P1010582.jpg"];
[imageArray addObject:@"P1010583.jpg"];
NSLog(@"number of objects: %d", [imageArray count]);
Bob

Last edited by thepianoguy; February 19th, 2011 at 03:21 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help a noob wilbo BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 April 22nd, 2008 12:54 PM
Noob Needs Help -XM- BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 3 June 28th, 2007 05:53 PM
totally weird problem with a script tsavalia PHP Databases 0 August 7th, 2006 07:12 PM
css: fixed box totally centered   cat CSS Cascading Style Sheets 2 April 6th, 2005 09:19 PM



All times are GMT -4. The time now is 01:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.