我試圖很簡單地從appDelegate中設置一個名爲'Bank'的類的屬性,但它並不像我想的那樣工作。不知何故,這些屬性在appDelegate類中設置得很好,所以只要我從appDel中調用它的屬性,它就可以工作,但只要我從任何其他類調用相同的屬性,它們就會返回null。這裏是所有相關的代碼。從appDelegate設置另一個類的屬性
應用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等。
以下是包含正在設置的屬性的類。 Bank.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調用的。請解釋。
你如何獲得銀行的實例中,失敗的案例? – Wain
@Wain - (Bank *)daBank { if(!daBank){daBank = [[Bank alloc] init]; } \t \t return daBank; } – luca590