Objective C property passed reset to null after use - ios

In my root view controller (named rootViewController.h) I have a property
#property (nonatomic, strong) NSDictionary *contactList;
Im trying to store value in this dictionary. Im passing the property to my function
AddContact *addContact = [[AddContact alloc]init];
NSString *result = #"";
result = [addContact addContact:user :[self contactList]];
When I go to my AddContact Function and breakpoint, the contactList is null even though I added 5x on the contactList using this code.
User *newUser = [[User alloc]init];
newUser.name = user.name;
newUser.phoneNumber = user.phoneNumber;
newUser.companyName = user.companyName;
[contactList setValue:newUser forKey:newUser.name];
Help

Try to use NSMutableDictionary, if you want to change it. NSDictionary can't be edited after initialization.

If you want to change your dictionary you need to create is as mutable like
#property (nonatomic, strong) NSMutableDictionary *contactList;
as per rule if you are using Mutable objects you need to alloc ,init it
so in view's didLoad method
_contactList = [[NSMutableDictionary alloc] init] ;

You have to initialise contactList. You have not initialise contactList and so the value is not set. And you can use mutable dictionary if you want to change values later on. Please make sure the values being added to contactList are not null else they will not be added to contactList and so dictionary will not have any objects in it.

Related

Access and change values of an NSMutableDictionary from one class to another iOS

I am dealing with NSMutableDictionary in one class, But I want to change it's value in an another class.
Here is the code where it is declared.
#interface MyClass0 : NSObject
{
#public
NSMutableDictionary *valuee;
}
#property (nonatomic, copy) NSMutableDictionary *valuee;
#end
and in the implementation of myClass0 I do
#synthesize valuee;
and I also declare value as
valuee = #{#"name" : #"Aryan"};
Now I want to access and change the value of this dictionary in an another class.
Use #property (nonatomic, strong) NSMutableDictionary *valuee;
Now assign the value as
self.valuee = [[NSMutableDictionary alloc] init];
You can do this in the init method of your MyClass0.
Now you can access the value from another class like
MyClass0 *myClassInstance = [[MyClass0 alloc] init];
NSMutableDictionary *dict = myClassInstance.valuee;
You can write the getter by hand --
- valuee {
return valuee;
}
so the code instance.valuee will give you the original object not the copy.

Create NSArray with specific property

I have an NSArray called carparkArr that contain MyCarpark objects. This class contain:
#property (nonatomic, assign) CLLocationCoordinate locationCoord;
Now I want to create an array that will have NSValue of each of these objects. How to do it quickly? I want something like:
NSArray *carparkArr = // fetching data
NSArray *locationProperty = [self getArrayForPropertyPath:#".locationCoord" forArray:carparkArr];
Yeah that feature already exists:
NSArray *locationProperty = [carparkArr valueForKey:#"locationCoord"];

How can I access an NSMutableArray in a different class?

I declare the following in ViewController.h
#property (nonatomic, strong) NSMutableArray *locations;
and the following in ViewController.m
#implementation GHViewController
#synthesize locations;
...
for (FSFourSquareVenueObject *object in array) {
locations = [[NSMutableArray alloc] init];
[locations addObject:object.locationName];
NSLog(#"%#", locations);
}
This successfully logs all the string locations that have been placed in the locations NSMutableArray. How can I access this NSMutableArray in a different class?
I am trying to access it in my TableViewController class in order to display all the elements in the array. I have tried importing the ViewController.h file into my TableViewController.h file, yet I still cannot access the array from the ViewController class.
Remove the line
locations = [[NSMutableArray alloc] init];
from your for loop and place it somewhere like viewDidLoad or init. You're wiping out your array every time before adding a new object.
To access a single object across classes, you want to look into creating a singleton. There are many tutorials online.
Do as #Stonz2 suggests, but also modify your headers as follows:
In GHViewController.h:
#property (nonatomic, strong) NSArray *locations;
In GHViewController.m
#implementation GHViewController
#synthesize locations;
...
NSMutableArray *array = [[NSMutableArray alloc] init];
for (FSFourSquareVenueObject *object in array) {
[array addObject:object.locationName];
NSLog(#"%#", array);
}
self.locations = [array copy];
You can then access the array from another class using GHViewController -locations. You can edit the locations using the following snippet (or by creating a similar method in GHViewController):
NSMutableArray *array = [gh_viewController.locations mutableCopy];
[array addObject: newLocation];
gh_viewController.locations = [array copy];
Exposing a mutable array allows other class to modify the array without notifying GHViewController and vice versa. This can lead to unpredictable and hard to debug problems, such as if GHViewController removes some elements while TableViewController is iterating through all the objects. Using a non-mutable array prevents these sorts of bugs and ensures everyone has a consistent view of what's inside.

NSMutableArray not printing correct data - iOS/Objective C

I'm struggling to get my array to print the correct data.
I've got it linked to a button, so it gets the textfield data and adds it to a Person class which has a subclass called PhoneBookEntry which contains firstName, and then adds it to an NSMutableArray called entries. here's the button code:
PhonebookEntry *person = [[PhonebookEntry alloc] init];
self.firstName.text = person.firstName;
[self.entries addObject:person];
NSLog(#"%#", self.entries);
Here's the start where I initialise everything:
#interface ViewController ()
#property (nonatomic, strong) PhonebookEntry *person;
#property (nonatomic, strong) NSMutableArray *entries;
#end
in my viewDidLoad, this is the code to create the NSArray.
self.entries = [NSMutableArray arrayWithCapacity:1];
I've tested and it works fine when adding normally and prints etc, just not with the array.
Thanks
The output
test,
test2,
"<PhonebookEntry: 0x8c69770>"
It is printing correctly.
Actually you want something more from your code or Objective-C.
For this you need to override description in PhonebookEntry class to break to the level where NSLog can print. NSLog can't print person object values.
-(NSString *)description{
return [NSString stringWithFormat:#"%# , %#", self.firstName, self.lastName];
}
This self.firstName.text = person.firstName; should be the other way around, so change it to this:
person.firstName = self.firstName.text;

iOS Incompatible Pointer Types (NSMutableDictionary from NSDictionary)

I'm having an issue where I use nothing but NSMutableDictionaries but somewhere in saving/pulling the data from Core Data it's turning into an NSDictionary that I can't edit. Here is the code:
View Controller A
#property NSMutableDictionary *myDictionary;
#synthesize myDictionary;
myDictionary = [NSMutableDictionary new];
//Pull dictionary data in IBAction from somewhere else
APPOtherViewController *source = [segue sourceViewController];
myDictionary = source.otherDictionary; //other dictionary is also NSMutableDictionary
//On a button press, I save this dictionary to a Core Data transformable attribute (using MR_shorthand):
APPMyObject *newObject = [APPMyObject createEntity];
newObject.coreDictionary = myDictionary;
[[NSManagedObjectContext defaultContext]saveToPersistentStoreAndWait];
I then pull the CD entity in a new view controller based on a set attribute...
ViewControllerB
#property NSMutableDictionary *myDictionary;
#synthesize myDictionary;
myDictionary = [NSMutableDictionary new];
APPMyObject *details = [APPMyObject findFirstByAttribute:#"myName" withValue:myName];
myDictionary = details.coreDictionary;
The warning occurs on the very last line, "Incompatible pointer types assigning to 'NSMutableDictionary' from 'NSDictionary'. Everything compiles and runs fine, but the warning is there for a reason.
Forgive me if this is fundamental knowledge, but why does saving the dictionary to Core Data change it from mutable to immutable? If I change everything to NSDictionary it works fine, but then I can't edit the information and rewrite.
Thank you!
In these case, you can use this instead of myDictionary = details.coreDictionary;.
myDictionary = [NSMutableDicationary dictionaryWithDictionary:details.coreDictionary];
Updation:
I saw that myDictionary = [NSMutableDictionary new];, this line totally waste. Even you allocate to NSMutableDictionary, during assign with details.coreDictionary, It will try to assign a NSDictionary object with NSMutableDictionary variable which means, myDictionary try to point NSMutableDictionary memory which leads to compile time error. Also Type casting to NSMutableDictionarymyDictionary = (NSMutableDictionary*)details.coreDictionary;, temporary solve your problem but type casting is not correct one. After assign it, when you try to access NSMutable method, it will get crash.

Resources