objective c - How to create a key per initiated object using NSUserdefaults -


i have custom nsobject , instantiate object using manager.

my nsobjets passed mutable array can access properties(name) via uipickerview once save method initialised.

when close app, objects disappear expected.

i save objects in nsuserdefaults have implemented nsuserdefaults in manager. save objects loaded when application launched in uipickerviewcontroller.

however application crashes when load uipickerviewcontroller. here log after crash: terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[pacsserver encodewithcoder:]: unrecognized selector sent instance 0x7fd0a2e7fa20'

pacsserver.h

#import <foundation/foundation.h>  @interface pacsserver : nsobject @property ( nonatomic) nsstring *pacsname; @property (nonatomic) nsstring*username; @property ( nonatomic) nsstring*password; @property (nonatomic) nsstring *wadourl; @property ( nonatomic) nsstring *apidirectory; @property ( nonatomic) nsstring *dataarrayincludesusernamepasswordwadologinwebserviceandsecurity;  @end 

pacsserver.m

#import "pacsserver.h"  @implementation pacsserver  @end 

manager.h

#import <foundation/foundation.h>  @interface pacsservermanager : nsobject +(instancetype)pacsmanager ;  @property nsmutablearray *pacsservers; @end 

manager.m

#import "pacsservermanager.h" #import "pacsserver.h" @implementation pacsservermanager  +(instancetype)pacsmanager {      static id instance;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         instance = [[self alloc] init];     });     return instance; }  -(id) init {      if(self =[super init]){          pacsserver *server= [[pacsserver alloc]  init];          _pacsservers = [nsmutablearray arraywithobjects:server, nil];         nsuserdefaults *standarduserdefaults = [nsuserdefaults standarduserdefaults];         nsdata *serverdata = [nskeyedarchiver archiveddatawithrootobject:_pacsservers];         [standarduserdefaults setobject:serverdata forkey:@"server"];               [standarduserdefaults setobject:_normalarray forkey:@"servers" ];        [standarduserdefaults synchronize];     }      return self; }  @end 

here in save method below username, password , other fields text fields. able save these keychain.

-i first save these in array pass array nsdata using datawithjsonobject. save array nsstring keychain

add nsobjects via manager

#import "addpacsviewcontroller.h" #import "pacsservermanager.h" #import "pacsserver.h" #import <keychainitemwrapper.h> @interface addpacsviewcontroller ()  @end- (void)saveforaddpacs:(id)sender { . . . .      nsstring *pacsdataarray = [keychain objectforkey:(__bridge id)ksecvaluedata]; pacsserver *newpacsserver = [[pacsserver alloc] init]; newpacsserver.pacsname= pacsnamefromaddpacsview;     newpacsserver.dataarrayincludesusernamepasswordwadologinwebserviceandsecurity = pacsdataarray;     [[pacsservermanager pacsmanager].pacsservers addobject:newpacsserver]; [self performseguewithidentifier:@"save" sender:self]; } 

access objects in uipickerview pickerviewcontroller.m

- (nsstring*)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component{      nslog(@"pacs data in settingsview:%@", [pacsservermanager pacsmanager].pacsservers);       pacsserver *server = [[pacsservermanager pacsmanager].pacsservers objectatindex:row];     nsstring *name = server.pacsname;     nslog(@"servername:%@, , data %@", server.pacsname, server.dataarrayincludesusernamepasswordwadologinwebserviceandsecurity);    nslog(@"%@", [[nsuserdefaults standarduserdefaults] dictionaryrepresentation]);     return name;   } 

any appreciated.

thank you.

you need address couple of things here.

1) exception caused because of line:

nsdata *serverdata = [nskeyedarchiver archiveddatawithrootobject:_pacsservers]; 

when archive object, object needs implement <nscoding> protocol. when archive aggregate objects (like nsarray , nsset) each of objects contained in array or set need implement <nscoding> protocol also. in case pacsserver being sent encodewithcoder: message doesn't implement. see this tutorial on nscoding.

2) idea persisting these objects in nsuserdefaults? nsuserdefaults designed small bits of information whether should show item, or first time user has launched app. perhaps you'd better off saving data in separate .dat file or using different method coredata or sqlite (if have large amount of data).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -