我试图从 appDelegate 中简单地设置一个名为“Bank”的类的属性,但它并没有像我想象的那样工作。不知何故,appDelegate 类中的属性设置得很好,所以只要我从 appDel 调用属性它就可以工作,但是一旦我从任何其他类调用这些相同的属性,它们就会返回 null。这是所有相关代码。
应用程序 Del.m
#import "MusikerAppDelegate.h"
#import "MusikerViewController.h"
@implementation MusikerAppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize tbc;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
AudioTableViewController *ATVC = [[AudioTableViewController alloc] init];
MusikerViewController *MusicVC = [[MusikerViewController alloc] init];
tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects: MusicVC, ATVC, nil];
[ATVC release];
[MusicVC release];
Bank *daBank = [[Bank alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
daBank.managedObjectContext = context;
daBank.myDate = [NSDate date];
if (!daBank.myDate) {
NSLog(@"no date at point A");
} else {
NSLog(@"Date at appDel : %@",daBank.myDate);
}
if (!daBank.managedObjectContext) {
NSLog(@"PROBLEM. NO MANAGED OBJECT CONTEXT!! -MUSIKER APP DELEGATE at point A");
}
else {
NSLog(@"ManagedObjectContext created at point A");
NSLog(@"Context: %@",daBank.managedObjectContext);
NSLog(@"PS Coord : %@",daBank.managedObjectContext.persistentStoreCoordinator);
NSLog(@"MOM : %@", daBank.managedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",[[daBank.managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"name"]);
}
self.window.rootViewController = self.viewController;
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
return YES;
}
...(更多方法)不,当我调用 NSLog 打印日期、NSManagedObject 等时,它会打印所有已初始化并正常工作的内容。
这是包含正在设置的属性的类。银行.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "AudioFile.h"
#import "AudioTableViewController.h"
#import "MusikerAppDelegate.h"
@interface Bank : NSObject {
NSManagedObjectContext *managedObjectContext;
NSDate *myDate;
NSString *recPath;
}
@property (retain, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSDate *myDate;
@property (retain, nonatomic) NSString *recPath;
//@property (copy) NSString *docPath;
- (id)init;
+ (NSString *)getDataPath:(NSString *)fileExtDate;
-(BOOL)addAudioFileEntityToModel;
-(BOOL)removeAudioFromModel:(id)audio;
-(NSMutableArray *)getFetchArray;
@end
和实现文件Bank.m
//
// Bank.m
// Musiker
//
// Created by Lucas Gaylord on 7/7/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "Bank.h"
//#import "AudioFile.h"
@implementation Bank
@synthesize managedObjectContext;
@synthesize myDate;
@synthesize recPath;
//@synthesize docPath = _docPath;
- (NSMutableArray *)getFetchArray {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
if(!managedObjectContext) {
//handel error
}
NSLog(@"AT PLACE A");
NSLog(@"Context: %@",managedObjectContext);
NSLog(@"PS Coord : %@",managedObjectContext.persistentStoreCoordinator);
NSLog(@"MOM : %@", managedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",[[managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"name"]);
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AudioFile" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[request release];
return mutableFetchResults;
}
...(更多方法)现在,当我调用 NSLog 来打印我在 appDel 中设置的相同属性时,它们都返回 null。这发生在我在 appDel 之外获得这些属性的任何地方。
我不知道问题是什么。我试过调用 self.managedObjectContext。在执行 Bank.m 之前,我已经尝试过 @class MusikAppDelegate。我已经在我的应用程序的每个类中将属性打印到控制台,但它们始终返回 null,除非从 appDel 调用它们。请,请解释。