如何使用NSMutableDictionary来存储和检索数据

时间:2010-04-10 10:08:34

标签: iphone uitabbarcontroller nsmutabledictionary

我已经创建了基于Window的应用程序和标签栏控制器作为根控制器。我的目标是将文本字段数据值存储在一个标签栏VC中,并且可由其他VC访问和编辑,并且在应用程序启动时也可以检索。

我希望在AppDelegate中使用NSMutableDictionary类,以便我可以使用键访问存储的数据值。

//TestAppDelegate.h

extern NSString *kNamekey ;
extern NSString *kUserIDkey ;
extern NSString *kPasswordkey ;

@interface TestAppDelegate :NSObject<UIApplicationDelegate>{
 UIWindow *window;
 IBOutlet UITabBarController *rootController;
 NSMutableDictionary *outlineData ;

}
@property(nonatomic,retain)IBOutlet UIWindow *window;
@property(nonatomic,retain)IBOutlet UITabBarController *rootController;
@property(nonatomic,retain) NSMutableDictionary *outlineData ;
@end

//TestAppDelegate.m
 #import "TestAppDelegate.h"

NSString *kNamekey =@"Namekey";
NSString *kUserIDkey =@"UserIDkey";
NSString *kPasswordkey =@"Passwordkey";

@implemetation TestAppDelegate

@synthesize outlineData ;

-(void)applicationDidFinishLaunching:(UIApplication)application
{


  NSMutableDictionary *tempMutableCopy = [[[NSUserDefaults standardUserDefaults] objectForKey:kRestoreLocationKey] mutableCopy];
 self.outlineData = tempMutableCopy;
 [tempMutableCopy release];
 if(outlineData == nil){
 NSString *NameDefault   = NULL;
 NSString *UserIDdefault= NULL;
 NSString *Passworddefault= NULL;


 NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObjectsAndKeys:
      NameDefault, kNamekey ,
      UserIDdefault, kUserIDkey ,      
      Passworddefault, kPasswordkey ,      
      nil];
 self.outlineData = appDefaults;
  [appDefaults release];
  }

 [window addSubview:rootController.view];
 [window makeKeyAndVisible];

 NSMutableDictionary *savedLocationDict = [NSMutableDictionary dictionaryWithObject:outlineData forKey:kRestoreLocationKey];
 [[NSUserDefaults standardUserDefaults] registerDefaults:savedLocationDict];
 [[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)applicationWillTerminate:(UIApplication *)application
{

 [[NSUserDefaults standardUserDefaults] setObject:outlineData forKey:kRestoreLocationKey];
}
@end
Here ViewController is ViewController of Navigation Controller which is attached with one tab bar.. I have attached xib file with ViewController

//ViewController.h
@interface
   IBOutlet UITextField *Name;
   IBOutlet UITextField *UserId;
   IBOutlet UITextField *Password;
}
@property(retain,nonatomic) IBOutlet UITextField *Name
@property(retain,nonatomic) IBOutlet UITextField *UserId;
@property(retain,nonatomic) IBOutlet UITextField *Password;
-(IBAction)Save:(id)sender;
@end

Here in ViewController.m, I am storing object values with keys.
/ViewController.m
-(IBAction)Save:(id)sender{

  TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
   [appDelegate.outlineData setObject:Name.text forKey:kNamekey ];
   [appDelegate.outlineData setObject:UserId.text forKey:kUserIDkey ];
   [appDelegate.outlineData setObject:Password.text forKey:kPasswordkey];
   [[NSUserDefaults standardUserDefaults] synchronize];   
}

I am accessing stored object using following method.
-(void)loadData
{

 TabBarAppDelegate *appDelegate = (TabBarAppDelegate *)[[UIApplication sharedApplication] delegate];
 Name = [appDelegate.outlineData  objectForKey:kNamekey ];
 UserId = [appDelegate.outlineData  objectForKey:kUserIDkey ];
 Password = [appDelegate.outlineData  objectForKey:kPasswordkey];


 [Name release];
 [UserId release]; 
 [Password release];

}

我在应用程序中获得了EXEC_BAD_ACCESS。我在哪里弄错了? 谢谢,

1 个答案:

答案 0 :(得分:1)

您正在创建自动释放的对象,然后在它们上调用release(例如在loadData中)。您需要阅读“内存管理指南”。您应该只对使用alloc或copy创建的对象调用release。

相关问题