I need to take information submitted by a user, store that information in an NSMutableDictionary, then store that NSMutableDictionary inside another NSMutableDictionary which is then encoded inside another class. For whatever reason, I can't seem to store the first NSMutableDictionary inside of the other.
I had to slim down the code that's in here due to work rules, so sorry if it seems to be missing anything. I only posted the parts that I'm having trouble with.
UserInfo.h:
#import <Foundation/Foundation.h>
#interface MyPlanInfo : NSObject <NSCoding>
#property (nonatomic, strong) NSMutableDictionary *emergencyDictionary;
#end
UserInfo.m:
#import <Foundation/Foundation.h>
#import "MyPlanInfo.h"
static NSString *emergencyDictionaryKey = #"emergencyDictionaryKey";
#implementation MyPlanInfo
#synthesize emergencyDictionary;
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
self.emergencyDictionary = [coder decodeObjectForKey:emergencyDictionaryKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.emergencyDictionary forKey:emergencyDictionaryKey];
}
#end
infoView.h
#import <UIKit/UIKit.h>
#import "MyPlanInfo.h"
#interface infoView : UIViewController <NSCoding>
{
NSMutableDictionary *emergencyContactInfo;
NSArray *userInfo;
NSArray *userKeys;
NSMutableArray *tempArray;
}
#property (nonatomic, strong) MyPlanInfo *myPlanInfoObject;
-(void)saveUserInfo;
-(void)loadUserInfo;
#end
infoView.m:
#import "infoView.h"
#interface infoView ()
#end
#implementation infoView
static NSString *userInfoKey = #"userInfoKey";
static NSString *userName;
-(void)viewDidLoad
{
[super viewDidLoad];
if(!self.myPlanInfoObject)
{
self.myPlanInfoObject = [[MyPlanInfo alloc] init];
}
[self loadUserInfo];
}
-(void)addToDictionary
{
emergencyContactInfo = [NSMutableDictionary dictionaryWithObjects:userInfo forKeys:userKeys];
if([userInfo count] != 0 || userInfo == nil)
{
self.myPlanInfoObject.emergencyDictionary = [NSMutableDictionary dictionaryWithObject:emergencyContactInfo forKey:userName];
}
[self saveUserInfo];
}
- (void)saveUserInfo
{
NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:self.myPlanInfoObject];
[[NSUserDefaults standardUserDefaults] setObject:userInfoData forKey:userInfoKey];
}
- (void)loadUserInfo
{
NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:userInfoKey];
if(userInfoData)
{
self.myPlanInfoObject = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
}
}
#end
In infoView.m, in the addToDictionary method, userInfo is an array of user inputted information, and userKey's is an array of key's. The emergencyContactInfo NSMutableDictionary works just fine, everything is in it, but when I try to set that as an object in a new NSMutableDictionary, for a key, it doesn't work. Everything is nil.
Anyone have any ideas on how what I'm doing wrong?
Edit: If you down vote, please leave a reason as to why so that I can avoid doing whatever I did wrong in the future.
In the following line you’re creating an instance of MyPlanInfo using plain alloc/init:
self.myPlanInfoObject = [[MyPlanInfo alloc] init];
However, at least in the code provided, you haven’t overridden init in MyPlanInfo, but instead, initWithCoder::
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
self.emergencyDictionary = [coder decodeObjectForKey:emergencyDictionaryKey];
return self;
}
When you use just plain init, the MyPlanInfo’s emergencyDictionary instance variable will be nil. You should likely add something like the following to MyPlanInfo to override init:
- (id) init
{
if ((self = [super init])) {
emergencyDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
That will assure that the newly created MyPlanInfo instance has a proper NSMutableDictionary that can be manipulated from other classes.
Related
i have an NSMutableDictionary declared in a class but i want to print get access to the contents of it in another class for example
#interface MyClass0 : NSObject
{
}
#property (nonatomic, strong) NSMutableDictionary *valuee;
#end
and in implementation i do
#implementation MyClass0
- (void)viewDidLoad{
[super viewDidLoad];
[valuee setObject:#"name" forKey:#"Aryan"];
}
#end
Now i create a new class called MyClass1 where i want to access these
#interface MyClass1 : NSObject
{
}
#property (nonatomic, strong) NSMutableDictionary *dict;
#end
and the implementation
#implementation MyClass1
#synthesize dict;
- (void)viewDidLoad{
[super viewDidLoad];
self.dict = [[NSMutableDictionary alloc] init];
MyClass0 *c = [[MyClass0 alloc] init];
self.dict = c.valuee;
// dict is not nil but the contents inside is nil so it clearly creates a new instance
}
#end
You are creating the instance of MyClass0 and valuee is declared but not initialized.
The closest solution to your code is
MyClass0 *c = [[MyClass0 alloc] init];
c.valuee = [[NSMutableDictionary alloc] init];
self.dict = c.valuee;
If a value is assigned to a declared property then an explicit initialization is not necessary.
If it's just a simple NSMutableDictionary that has the same contents every time you can create a class method in MyClass0 like so:
+ (NSMutableDictionary *) getDict {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"name" forKey:#"Aryan"];//did you mean [dict setObject:#"Aryan" forKey:#"name"]?
return dict;
}
To access this, declare the method in the MyClass0.h file like so: + (NSMutableDictionary *) getDict; and simply call [MyClass0 getDict]; in your MyClass1.m file.
If it doesn't have the same contents every time, you'll have to pass the dictionary forward to each view controller in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Make sure your segue name in storyboard is the same as this next line
if ([[segue identifier] isEqualToString:#"MySegue"]) {
MyClass1 *mc = [segue destinationViewController];
mc.dict = self.valuee;
}
}
I know one way to share data is segue. But in my application I have multiple tabs which contain number of VCs. For instance userName and address. I want to show in some of the VCs these infos.
Every time I query the cloud is not right way. I am following this answer first part: answer. But as a newbie I am not sure how MyDataModel is defined. Is it a NSObject class? I appreciate if anyone can define this class as example with two NSString fields. And how to access these fields in VC and AppDelegate.
Inside AppDelegate
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
MyDataModel *model;
AViewController *aViewController;
BViewController *bViewController;
...
}
#property (retain) IBOutlet AViewController *aViewController;
#property (retain) IBOutlet BViewController *aViewController;
#end
#implementation MyAppDelegate
...
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
...
aViewController.model = model;
bViewController.model = model;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
Inside VC:
#interface AViewController : UIViewController {
MyDataModel *model;
}
#property (nonatomic, retain) MyDataModel *model;
#end
#interface BViewController : UIViewController {
MyDataModel *model;
}
#property (nonatomic, retain) MyDataModel *model;
#end
The only thing I need is where to define MyDataMode and how to access its fields?
You can use singleton class for that,
----------
SharedManages.h
----------
#import <Foundation/Foundation.h>
#import "Reachability.h"
#import "Reachability.h"
#interface SharedManager : NSObject
{
}
+(SharedManager *)sharedInstance;
// Create property of your object which you want to access from whole over project.
#property (retain, nonatomic) User *loginUser;
#property (assign, readwrite) BOOL isNetAvailable;
#end
----------
----------
SharedManages.m
----------
#import "SharedManager.h"
static SharedManager *objSharedManager;
#implementation SharedManager
#synthesize
isNetAvailable = _isNetAvailable,
loginUser = _ loginUser;
+(SharedManager *)sharedInstance
{
if(objSharedManager == nil)
{
objSharedManager = [[SharedManager alloc] init];
objSharedManager. loginUser = [User alloc]] init];
Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
// Bool
if(internetStatus == NotReachable)
{
NSLog(#"Internet Disconnected");
objSharedManager.isNetAvailable = NO; // Internet not Connected
}
else if (internetStatus == ReachableViaWiFi)
{
NSLog(#"Connected via WIFI");
objSharedManager.isNetAvailable = YES; // Connected via WIFI
}
else if (internetStatus == ReachableViaWWAN)
{
NSLog(#"Connected via WWAN");
objSharedManager.isNetAvailable = YES; // Connected via WWAN
}
}
return objSharedManager;
}
#end
Access from other Class...
[SharedManager sharedInstance].isNetAvailable ;
[SharedManager sharedInstance].loginUser ;
Hope, This will help you..
I don't have "local" copies of the values. I set them in the delegate and fetch them from there. That way you don't have to hard code it for all UIViewController's in the delegate.
Assigning values is best done on the first view, or with default values. I personally use viewDidLoad for those kind of things. Since it is only called once on the first view once and pertains until the app is terminated.
Then I get the delegate from inside the VC, call the instance and from there the values.
Swift
Inside AppDelegate:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var globals : GlobalValueClass?
First VC:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
delegate.globals = GlobalValueClass()
delegate.globals!.numbers = [1,2,3]
}
}
Other VC's:
class ViewControllerTwo: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
print(delegate.globals!.numbers)
// Do any additional setup after loading the view.
}
}
Objective C ( don't have the full method in obj-c, but easy to find)
MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];
how to get the delegate in obj-c
Easiest way i could think of is using NSUserDefaults. Save your name and address string in NSUserDefaults like
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:YourNameString forKey:#"NameString"];
[defaults setValue:YourAddressString forKey:#"AddressString"];
[defaults synchronize];
and access it in any ViewController as
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [plistContent valueForKey:#"NameString"];
NSString *address= [plistContent valueForKey:#"AddressString"];
Hope this helps.
You can create a class and use it in your tab controller.
#import <Foundation/Foundation.h>
#interface UserModel : NSObject <NSCoding>
#property (nonatomic, strong) NSString *lastName;
#property (nonatomic, strong) NSString *firstName;
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict;
- (instancetype)initWithDictionary:(NSDictionary *)dict;
- (NSDictionary *)dictionaryRepresentation;
#end
implementation file
#import "UserModel.h"
NSString *const kUserModelLastName = #"LastName";
NSString *const kUserModelFirstName = #"FirstName";
#interface UserModel ()
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
#end
#implementation UserModel
#synthesize lastName = _lastName;
#synthesize firstName = _firstName;
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
return [[self alloc] initWithDictionary:dict];
}
- (instancetype)initWithDictionary:(NSDictionary *)dict
{
self = [super init];
// This check serves to make sure that a non-NSDictionary object
// passed into the model class doesn't break the parsing.
if(self && [dict isKindOfClass:[NSDictionary class]]) {
self.lastName = [self objectOrNilForKey:kUserModelLastName fromDictionary:dict];
self.firstName = [self objectOrNilForKey:kUserModelFirstName fromDictionary:dict];
}
return self;
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setValue:self.lastName forKey:kUserModelLastName];
[mutableDict setValue:self.firstName forKey:kUserModelFirstName];
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
- (NSString *)description
{
return [NSString stringWithFormat:#"%#", [self dictionaryRepresentation]];
}
#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
id object = [dict objectForKey:aKey];
return [object isEqual:[NSNull null]] ? nil : object;
}
#pragma mark - NSCoding Methods
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
self.lastName = [aDecoder decodeObjectForKey:kUserModelLastName];
self.firstName = [aDecoder decodeObjectForKey:kUserModelFirstName];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_lastName forKey:kUserModelLastName];
[aCoder encodeObject:_firstName forKey:kUserModelFirstName];
}
Import this class where you are setting or initializing data or values. and do below code.
NSDictionary *dicUserModel = [[NSDictionary alloc]initWithObjectsAndKeys:#"Moure",#"LastName",#"Jackson",#"FirstName", nil];
UserModel *userModel = [[UserModel alloc]initWithDictionary:dicUserModel];
//NSUserDefault save your class with all property. and you can simply retrieve your UserModel from NSUserDefault.
//Below code save this model into nsuserdefault.
[[NSUserDefaults standardUserDefaults]setObject:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:#"UserModel"];
[[NSUserDefaults standardUserDefaults]synchronize];
You can retrieve you class object using below code.
UserModel *savedUserModel = (UserModel *)[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:#"UserModel"]];
NSLog(#"%#",savedUserModel.firstName);
NSLog(#"%#",savedUserModel.lastName);
I have a NSMutableaArray of NSString objects. So i'm using NSKeyedArchiever to save it to disk. So when i try to use
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.EventsList forKey:#"Events"];
}
i got an error
Event encodeWithCoder:]: unrecognized selector sent to instance 0x7fd06b542780
Here's my parts of code:
//-------------------Events.h--------------------------
#interface Event : NSObject
#property (strong,nonatomic) NSString *nameOfEvent;
#property (strong,nonatomic) NSString *dateOfEvent;
#property (strong,nonatomic) NSString *placeOfEvent;
#property int priorityOfEvent;
#end
//---------------Singleton.h ----------------
#interface GlobalSingleton : NSObject <NSCoding, NSCopying> {
NSMutableArray *EventsList;
}
#property (nonatomic,retain) NSMutableArray *EventsList;
+(GlobalSingleton *)sharedFavoritesSingleton;
#end
//----------------Singleton.m------------------------
....
#implementation GlobalSingleton
#synthesize EventsList;
....
....
- (void)encodeWithCoder:(NSCoder *)aCoder {
NSLog (#"%#",EventsList); // not nil
[aCoder encodeObject:self.EventsList forKey:#"Events"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super init])) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:[aDecoder decodeObjectForKey:#"Events"]];
self.EventsList = temp;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
GlobalSingleton *copy = [[GlobalSingleton allocWithZone:zone] init];
copy.EventsList = self.EventsList;
return copy;
}
#end
I get textdata from Web-server using ASIFormDataRequest in JSON format, and then i add this object to NSMutableArray, which is also a Singleton, so it looks like this:
NSDictionary *responseDict = [responseString JSONValue];
GlobalSingleton *Singleton = [GlobalSingleton sharedFavoritesSingleton];
for (NSDictionary *str in responseDict) {
Event *newEvent = [[Event alloc] init];
newEvent.nameOfEvent = [str objectForKey:#"EventName"];
newEvent.dateOfEvent = [str objectForKey:#"EventDate"];
newEvent.placeOfEvent = [str objectForKey:#"EventPlace"];
[Singleton.EventsList addObject:newEvent];
}
//------------------Save this data stored in NSMutableArray to disk-------------------------
[NSKeyedArchiver archiveRootObject:Singleton toFile:[self save_path]];
So, again, execution stops on this:
[aCoder encodeObject:self.EventsList forKey:#"Events"];
But when i try to code single NSString object everything goes with no errors.
eventList doesn't contain NSStrings, it contains Event objects.
Your Event class needs to implement encodeWithCoder: - as the exception message says, the Event class doesn't implement this method.
Also you should use a lowercase s for singleton as it is an instance, not a class, and you should probably not use singletons.
It seems if I do something like:
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
Then this needs to be in a function, and I can't modify it later using a different function.
I tried just instantiating it in the .h file,
#interface ViewController:
{
NSMutableArray *Values;
}
But then when I try to append to it during runtime, nothing happens. I try to append to it with this:
int intRSSI = [RSSI intValue];
NSString* myRSSI = [#(intRSSI) stringValue];
[Values addObject:myRSSI];
But the array remains empty when I do this.
How can I fix this?
The recommended way is to create a property;
// ViewController.h
#interface ViewController : UIViewController
{
}
#property (nonatomic, strong) NSMutableArray *values;
#end
Then override the getter for that property, to lazy-initialize it, i.e. the array will be allocated and initialized on first call of the NSMutableArray property's getter:
// ViewController.m
#interface ViewController ()
#end
#implementation ViewController
- (NSMutableArray *)values
{
if (!_values) {
_values = [[NSMutableArray alloc] init];
}
return _values;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//int intRSSI = [RSSI intValue];
//NSString *myRSSI = [#(intRSSI) stringValue];
//[self.values addObject:myRSSI];
// Keep it simple:
[self.values addObject:RSSI];
}
#end
I am using an NSData to store an object for later use. It has quite a few NSStrings and I need to pull it out of an object.
For some reason, only some of the NSStrings are stored and some others get zeroed out!
I was thinking it must be something with my code, that I must have forgotten to initialize some string, but for a very weird reason some of the strings lose the data!
I can't get theImportantString to get it's relevant value because it first seems like the variable got it's value, but after coming back from Unarchive, it's equal to #""!
// CMyData.h
/////////////////////
#interface CMyData : NSObject <NSCoding>
{
NSString *ID;
NSString *DIST;
.
.
}
#property (nonatomic,retain) NSString *ID;
#property (nonatomic,retain) NSString *DIST;
#end
// CMyData.m
//////////////////////
#import "CMyData.h"
#implementation CMyData
#synthesize ID;
#synthesize DIST;
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.ID = [decoder decodeObjectForKey:#"ID"];
self.DIST = [decoder decodeObjectForKey:#"DIST"];
.
.
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:ID forKey:#"ID"];
[encoder encodeObject:DIST forKey:#"DIST"];
.
.
}
- (void)dealloc {
[ID release];
[DIST release];
[super dealloc];
}
#end
MyController.m
-(void) makeObject: (NSDictionary *)dict
{
CMyData* myData=[[CMyData alloc] init];
myData.ID = [[NSString alloc] initWithString:[dict objectForKey:#"NAME"]];
myData.DIST = [[NSString alloc] initWithString:[dict objectForKey:#"DISTRIBUTOR"]];
.
.
myObject = [[[MYObject alloc] init];
myObject.data = [NSKeyedArchiver archivedDataWithRootObject:myData];
}
And then a tap on a button happens:
- (void) tapOnIcon: (MyObject*)theObject
{
CMyData *data = [NSKeyedUnarchiver unarchiveObjectWithData:theObject.data];
[delegate showData:data];
}
in the delegate Controller (Where the value can't be set anymore):
delegateController.m
/////////////////////////////////
-(void) showData:(CMyData*)theData{
self.theImportantString = [[NSString alloc] initWithString:theData.DIST];
.
.
.
}
Seems you have types mismatch:
// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [decoder decodeIntForKey:#"DIST"];
but in declaration you have
// in CMyData.h
NSString *DIST;
This should be:
// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [NSString stringWithFormat:#"%d", [decoder decodeIntForKey:#"DIST"]];