CoreData,瞬态属性和EXC_BAD_ACCESS。

时间:2012-12-13 00:09:51

标签: objective-c core-data

我正在尝试构建简单的文件浏览器而且我被卡住了。 我定义了类,构建窗口,添加控制器,视图..一切都有效,但只有一次。 再次选择NSTableView中的文件夹或尝试从Folder.files获取数据,从main导致静默EXC_BAD_ACCESS(代码= 13,地址0x0)。

关于我保留在CoreData之外的文件的信息,在简单的类中,我不想保存它们:

#import <Foundation/Foundation.h>

@interface TPDrawersFileInfo : NSObject
@property (nonatomic, retain) NSString * filename;
@property (nonatomic, retain) NSString * extension;
@property (nonatomic, retain) NSDate * creation;
@property (nonatomic, retain) NSDate * modified;
@property (nonatomic, retain) NSNumber * isFile;
@property (nonatomic, retain) NSNumber * size;
@property (nonatomic, retain) NSNumber * label;

+(TPDrawersFileInfo *) initWithURL: (NSURL *) url;
@end

@implementation TPDrawersFileInfo
+(TPDrawersFileInfo *) initWithURL: (NSURL *) url {
    TPDrawersFileInfo * new = [[TPDrawersFileInfo alloc] init];
    if (new!=nil) {
        NSFileManager * fileManager = [NSFileManager defaultManager];

        NSError * error;
        NSDictionary * infoDict = [fileManager attributesOfItemAtPath: [url path]  error:&error];

        id labelValue = nil;
        [url getResourceValue:&labelValue forKey:NSURLLabelNumberKey error:&error];
        new.label = labelValue;

        new.size = [infoDict objectForKey: @"NSFileSize"];
        new.modified = [infoDict objectForKey: @"NSFileModificationDate"];
        new.creation = [infoDict objectForKey: @"NSFileCreationDate"];
        new.isFile = [NSNumber numberWithBool:[[infoDict objectForKey:@"NSFileType"] isEqualToString:@"NSFileTypeRegular"]];
        new.extension = [url pathExtension];
        new.filename = [[url lastPathComponent] stringByDeletingPathExtension];

    }
    return  new;
}

接下来我有类Folder,它是NSManagesObject子类

// Managed Object class to keep info about folder content

@interface Folder : NSManagedObject {
    NSArray * _files;
}
@property (nonatomic, retain) NSArray * files; // Array with TPDrawersFileInfo objects
@property (nonatomic, retain) NSString * url;  // url of folder

-(void) reload; //if url changed, load file info again.

@end
@implementation Folder

@synthesize  files = _files;
@dynamic url;

-(void)awakeFromInsert {
    [self addObserver:self forKeyPath:@"url" options:NSKeyValueObservingOptionNew context:@"url"];
}

-(void)awakeFromFetch {
    [self addObserver:self forKeyPath:@"url" options:NSKeyValueObservingOptionNew context:@"url"];
}
-(void)prepareForDeletion {
    [self removeObserver:self forKeyPath:@"url"];
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == @"url") {
        [self reload];
    }
}

-(void) reload {
    NSMutableArray * result = [NSMutableArray array];
    NSError * error = nil;
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString * percented = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSArray * listDir = [fileManager contentsOfDirectoryAtURL: [NSURL URLWithString: percented]
                                   includingPropertiesForKeys: [NSArray arrayWithObject: NSURLCreationDateKey ]
                                                      options:NSDirectoryEnumerationSkipsHiddenFiles
                                                        error:&error];

    if (error!=nil) {NSLog(@"Error <%@> reading <%@> content", error, self.url);}

    for (id fileURL in listDir) {
        TPDrawersFileInfo * fi = [TPDrawersFileInfo initWithURL:fileURL];
        [result addObject: fi];
    }

    _files = [NSArray arrayWithArray:result];

}


@end

在我定义的app delegate中

@interface TPAppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet NSArrayController * foldersController;
    Folder * currentFolder;
}
- (IBAction)chooseDirectory:(id)sender; // choose folder

- (Folder * ) getFolderObjectForPath: path {
     //gives Folder object if already exist or nil if not
.....
}

- (IBAction)chooseDirectory:(id)sender {
    //Opens panel, asking for url
    NSOpenPanel * panel = [NSOpenPanel openPanel];
    [panel setCanChooseDirectories:YES];
    [panel setCanChooseFiles:NO];
    [panel setMessage:@"Choose folder to show:"];
    NSURL * currentDirectory;

    if ([panel runModal] == NSOKButton)
    {
        currentDirectory = [[panel URLs] objectAtIndex:0];
    }

    Folder * folderObject = [self getFolderObjectForPath:[currentDirectory path]];

    if (folderObject) {
        //if exist:
        currentFolder = folderObject;
    } else {
        // create new one
        Folder * newFolder = [NSEntityDescription
                              insertNewObjectForEntityForName:@"Folder"
                              inManagedObjectContext:self.managedObjectContext];
        [newFolder setValue:[currentDirectory path] forKey:@"url"];
        [foldersController addObject:newFolder];
        currentFolder = newFolder;
    }
    [foldersController setSelectedObjects:[NSArray arrayWithObject:currentFolder]];

}

请帮助;)

1 个答案:

答案 0 :(得分:0)

哈!

_files = [NSArray arrayWithArray:result];

应该是:

_files = [[NSArray arrayWithArray:result] retain];