iPhone Xcode 4 file bundle problem
I'm developing an app that needs 开发者_JAVA百科to save files and than show the titles on a tableview and it just works fine on xcode 3, now I have xcode 4 and when I load the tableView with the titles of all the files it crashes because of a bad access. The files are stored in the main bundle. I noticed that if I cut out the content of viewDidLoad (where i load the list of the files) the app works fine! This is the content of my viewDidLoad
:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
Anyone can help me out?
I would recommend to check if the file list is empty.
NSError* error;
list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (!list)
{
NSLog(error);
}
It might be that due to some SDK update the directory you are accessing is either not accessible or empty. But since your program crashes it indicates your list
variable is 0x0
.
From the looks of it, list
seems to be a ivar. If that is correct then you are assigning an autoreleased value to list
. If you do access this later, you will be trying to access a deallocated object and hence have an error. If you have an retain
ed property defined for list
, then you can do,
self.list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
If you for some reason, having a property isn't possible then do simply call retain on it like this,
list = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL] retain];
精彩评论