iPhone アプリケーション デリゲート (AppDelegate) で変数を取得/保持するという非常に奇妙な問題が発生しています。最初に、値が logfile (問題の NSString 変数) に渡されていることを確認できますが、logfile が別のクラスから取得されると (以下のコードを参照)、エラーが発生します。
ここに私の AppDelegate.h ファイルがあります:
#import < UIKit/UIKit.h >
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *_window;
MainViewController *_mainViewController;
NSString *logFile;
}
@property (nonatomic, retain) NSString *logFile;
@property (nonatomic, retain) ProductClass *item;
@property (nonatomic, retain) UIWindow *window;
-(void)appendToLog:(NSString *)textToLog;
@end
ここに私の AppDelegate.m があります:
#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize logFile;
- (void) applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_mainViewController = [[MainViewController alloc] init];
UINavigationController *_navigationController = [[UINavigationController alloc] initWithRootViewController:_mainViewController];
//Initialize the product class
[self appendToLog:@"Application loaded"];
[_window addSubview:_navigationController.view];
[_window makeKeyAndVisible];
}
-(void)appendToLog:(NSString *)textToLog {
//Append the log string
if(logFile!=nil) {
NSString *tmp = [[logFile stringByAppendingString:textToLog] stringByAppendingString:@"\n"];
logFile = tmp;
}
else {
NSString *tmp = [textToLog stringByAppendingString:@"\n"];
logFile = tmp;
}
}
@end
(別のクラスから)呼び出しを使用する場合:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *s = [appDelegate logFile];
「logfile」は「スコープ外」として返されるため、ローカル変数「s」はマッシュです。
ここで何が間違っていますか?それは私には意味がありません。