Alex Rivera | Logout

Can you access application delegate from within a UITableViewDataSource function?

Asked 2009-05-03T14:55:55.060
32

I'm trying to figure out the SQLite functionality for the iPhone, and I'm having some problems reading my database file from an overridden UITableViewDataSource function. I am storing the database file location in an ivar (as an NSString) in my application delegate, however when I try to access that string from an overridden UITableViewDataSource function, it returns some other object. If I access the string from any of the classes own instance methods, it works fine.

Is there any way to access the application delegate from within overridden UITableViewDataSource functions?

Edit
Report

2 Answers

22

Roger is correct, but I personally find it extremely confusing to mix dot syntax and bracket syntax in the same statement.

If this confuses you as well, the equivalent syntax, using only bracket notation, is:

[[UIApplication sharedApplication] delegate];

and yes, you may need to cast the result to be your application delegate class, rather than the generic UIApplicationDelegate class, or you will get a number of compiler warnings, most likely.

answered 2009-05-03T21:42:13.193
1

The problem turned out to be really simple. I had created the NSString to hold the path to my database file using stringByAppendingPathComponent: . I failed to realize that this was going to be autoreleased, and so I didn't bother to explicitly retain it. That reason it was returning a different type of object was because that memory had been reused once the string had been autoreleased.

Explicitly retaining the string holding the path to the database file solved the problem.

answered 2009-05-03T21:34:04.180

Your Answer