Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-19-2008, 03:57 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default Access array in appDelegate from another class

Hey everyone,

I have created an NSArray in my appDelegate.

Code:
calendarData = [[NSArray arrayWithObjects: @"A", @"B" , nil] retain];
I have create a new NSObject subclass called CalendarTab and I want to access the calendarData array.

I have tried this:

Code:
myArrayAppDelegate *appDelegate = (myArrayAppDelegate *)[[UIApplication sharedApplication] delegate];   
	return [appDelegate.calendarData count];
but I get the error request for member 'calendarData' is something not a union or structure.

I have imported the appdelegate into my calendartab class file also.

Any ideas?
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-19-2008, 04:01 PM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Is 'calendarData' declared as a property in your myArrayAppDelegate.h file?
RickMaddy is offline   Reply With Quote
Old 12-19-2008, 04:05 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Smile

Sorry I forgot to make it a property and synthsize it. Thanks for your help.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 07-27-2009, 07:24 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default appDelegate values Blank

I have created an array in my appDelegate, it's a property (nonatomic, retain) and synthesized in the .m in which it is populated from a sqlite database. The debugger shows that all this is working properly.

However, in another class (a view controller) I am attempting to access the array using the [UIApplication sharedApplication] method shown in this thread. Compiles fine but right after the execution of that line, the debugger shows that all the values in the appDelegate variable are blank as if it's a new uninitialized object.

Much sorrow abounds. What am I missing?
Chetmun is offline   Reply With Quote
Old 07-27-2009, 07:47 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Post the code you use to populate the array. And the code you use to read it later.
eddietr is offline   Reply With Quote
Old 07-27-2009, 10:21 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

Here's the code. Like I said, the populating works. I put in a breakpoint and checked in the debugger. I checked again after the sharedApplication line and the values are blank. Sigh.




// app delegate .H

NSMutableArray *pois;
@property (nonatomic, retain) NSMutableArray *pois;

// app delegate .M

@synthesize pois;

-(void) readPOIsFromDatabase
{
sqlite3 *database;
pois = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "select name, type from pois";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *aName = [NSString stringWithUTF8Stringchar *)sqlite3_column_text(compiledStatement, 0)];
NSString *aType = [NSString stringWithUTF8Stringchar *)sqlite3_column_text(compiledStatement, 1)];
POI *poi = [[POI alloc] initWithName:aName tp:aType];
[pois addObjectoi];
[poi release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

// OTHER CLASS .M


MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
POI *poi = [appDelegate.pois objectAtIndex:0];
Chetmun is offline   Reply With Quote
Old 07-27-2009, 10:36 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

OK, so in readPOIsFromDatabase, you are not using the pois property setter. So pois is not being retained as you might think from your property declaration.

But, you're also not releasing pois either in that method, so the net result is OK.

So when you read it in the other class, what do you get for pois.count?

Now when you say that the "values are blank", do you mean that you get a poi from the array, but name and type are blank for that poi?

If that is the case, then did you check that -[POI initWithName:tp:] is actually retaining the name and type strings?
eddietr is offline   Reply With Quote
Old 07-27-2009, 11:13 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

Yeah, I didn't think there even was a property setter for pois since it's just an array I created right in the first class. I populate it and don't release it - not even in dealloc.

When I get to the other class and run the sharedApplication line, I check the appDelegate variable and it says 0 objects and other variable in the appDelegate are blank or 0 like it's a brand new uninitialized object.
Chetmun is offline   Reply With Quote
Old 07-27-2009, 11:18 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Hmmm...

So can you tell us more about what happens between the time you load the pois and the time you try to access them from the other object?

You're sure you don't have two app delegates running around, right? (seems unlikely).

Maybe you could post more of the project? Or PM me and you can email me the project and I can take a look.
eddietr is offline   Reply With Quote
Old 07-27-2009, 11:23 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

BTW, you're sure you are checking the value of appDelegate after that line has executed right? i.e. when you have advanced the program counter to the next line?
eddietr is offline   Reply With Quote
Old 07-27-2009, 11:24 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

I'm embarrassed to say I don't know what PM-ing is. But I can certainly send you more of the project tomorrow. I have to shutdown now (in more ways than one). I'll check back in about 8 hours. Thanks!
Chetmun is offline   Reply With Quote
Old 07-27-2009, 11:25 PM   #12 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

PM is private message.

NVM, seems you dont' have private messages enabled?

In any case, maybe you'll figure it out tomorrow or you can post more of the project. I don't see anything (yet) from what you've posted.

Last edited by eddietr; 07-27-2009 at 11:28 PM.
eddietr is offline   Reply With Quote
Old 07-28-2009, 09:37 AM   #13 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

Quote:
Originally Posted by eddietr View Post
PM is private message.

NVM, seems you dont' have private messages enabled?

In any case, maybe you'll figure it out tomorrow or you can post more of the project. I don't see anything (yet) from what you've posted.
Obviously new to the forum. Can't seem to do anything on my CP. So I'll just paste into here. I've stripped out the irrelevant code so it's clearer and smaller. What's left should work on its own. The POI class is just a data structure, could be anything - a couple of strings in this example. I'm a Java programmer if you can't tell and this .m, .h stuff and the property and synthesizing is driving me nuts. OK, here's the code. Hope it makes sense.


OtherClass.h

// nothing in here related to this problem


OtherClass.m

#import "OtherClass.h"
#import "MyAppDelegateAppDelegate.h"
#import "POI.h"

@class MyAppDelegateAppDelegate;

@implementation OtherClass

- (void) viewDidLoad
{
[super viewDidLoad];

MyAppDelegateAppDelegate *appDelegate = (MyAppDelegateAppDelegate *)[[UIApplication sharedApplication] delegate];
// breakpoint here, appDelegate appears to be uninitialized (the pois array has 0 elements)
POI *poi = [appDelegate.pois objectAtIndex:0];
}


MyAppDelegate.h

#import <UIKit/UIKit.h>

@class OtherClass;

@interface MyAppDelegateAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
NSFileManager *FileManager;
NSString *databaseName;
NSString *databasePath;
NSMutableArray *pois;
}

-(void) readPOIsFromDatabase;
-(void) checkAndCreateDatabase;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) NSMutableArray *pois;

@end


MyAppDelegate.m

#import "MyAppDelegateAppDelegate.h"
#import "sqlite3.h"
#import "POI.h"

@implementation MyAppDelegateAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize pois;

- (void) applicationDidFinishLaunchingUIApplication *)application
{
databaseName = @"MyAppDelegate.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readPOIsFromDatabase];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}

-(void) readPOIsFromDatabase
{
sqlite3 *database;
pois = [[NSMutableArray alloc] init];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "select name, type from pois";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *aName = [NSString stringWithUTF8Stringchar *)sqlite3_column_text(compiledStatement, 0)];
NSString *aType = [NSString stringWithUTF8Stringchar *)sqlite3_column_text(compiledStatement, 1)];
POI *poi = [[POI alloc] initWithName:aName tp:aType];
[pois addObjectoi];
[poi release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

-(void) checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if (success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}

- (void) dealloc
{
[tabBarController release];
[window release];
[super dealloc];
}

@end
Chetmun is offline   Reply With Quote
Old 07-28-2009, 10:20 AM   #14 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

OK, well, it's possible in simplifying the code you have actually removed the source of the bug.

From what I see here, I would make sure you are advancing beyond this line before you check the pois array:

Code:
POI *poi = [appDelegate.pois objectAtIndex:0];
If the program counter is stopped at that line, it means that line has not yet been executed.

Also is POI initWithName actually retaining those two strings (name and type)?
eddietr is offline   Reply With Quote
Old 07-28-2009, 10:59 AM   #15 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

I'm fairly certain anything I removed had nothing to do with the problem. And I did indeed stop at the right point. I stepped a few steps beyond even but the appDelegate variable is just blank completely. I've attached a screen grab of the debugger. I was assuming it had something to do with what I import when and the synthesize/property/retain things. Thanks for trying. Any tips for where else to post?
Attached Images
File Type: jpg debug.jpg (20.1 KB, 1 views)
Chetmun is offline   Reply With Quote
Old 07-28-2009, 11:26 AM   #16 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

I'm not able to see the screenshot?
eddietr is offline   Reply With Quote
Old 07-28-2009, 01:13 PM   #17 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

Me either! Trying again ...
Attached Images
File Type: jpg debug.jpg (15.7 KB, 1 views)
Chetmun is offline   Reply With Quote
Old 07-28-2009, 02:38 PM   #18 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

So still doesn't work.

In terms of next steps, I'm not convinced that the problem is in the code you have posted. If that were the case, you probably would have solved it by now.

The best thing would be to find a way to share the whole project and let someone step through it and help you find the bug.
eddietr is offline   Reply With Quote
Old 07-28-2009, 03:55 PM   #19 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Cleveland
Posts: 21
Send a message via AIM to Chetmun
Default

Quote:
Originally Posted by eddietr View Post
So still doesn't work.

In terms of next steps, I'm not convinced that the problem is in the code you have posted. If that were the case, you probably would have solved it by now.

The best thing would be to find a way to share the whole project and let someone step through it and help you find the bug.
I would be willing to do that if I could PM you. Do you know why I can't change my user settings on this forum?
Chetmun is offline   Reply With Quote
Old 07-28-2009, 04:34 PM   #20 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Quote:
Originally Posted by Chetmun View Post
I would be willing to do that if I could PM you. Do you know why I can't change my user settings on this forum?
Just send me a PM with your email address or if you're on AIM, we can do that. Click on my name next to my post and select "send private message" from the drop down.
eddietr is offline   Reply With Quote
Old 10-10-2009, 10:28 AM   #21 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default

Hi guys,

Lately, I've been googling for solutions for the same problem as Chetnum's and really glad there's this discussion . I was just wondering if anyone here can share the solution to this?

Help is much appreciated!

Regards,
oonoo
oonoo is offline   Reply With Quote
Reply

Bookmarks

Tags
appdelegate, array, table

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 232
16 members and 216 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0