TextField text not being added to mutable array - ios

I am using textfield for the user to enter his username and password to signup and then store it in an array. But when i log my array it shows null. This is my code
User = self.Username.text;
UserPassword = self.UserPass.text;
LoginPageViewController *loginPage;
[loginPage.registeredUsernames addObject:User];
[loginPage.registeredPassword addObject:UserPassword];
NSLog(#"%#", loginPage.registeredUsernames);
NSLog(#"%#", loginPage.registeredPassword);
Please help

loginPage is a variable only declared, but is not instanciated any object. This means that is nil, we you send the message loginPage.registeredUsernames nothing happens because no object exist to handle that message.

Have you initialise your mutable array? If not try initialise the array and save the direct values in array like,
[loginPage.registeredUsernames addObject:self.Username.text];
[loginPage.registeredPassword addObject:self.UserPass.text];

I take it that your LoginPageViewController object's properties registeredUsernames and registeredPassword are NSMutableArrays.
If that is indeed the case, given you code, you will need to instantiate the LoginPageViewController *loginPage and make sure that in the init method for this object you are properly instantiating the registeredUsernames and registeredPassword properties. Something to the effect of:
...
_registeredUsernames = [NSMutableArray new];
_registeredPassword = [NSMutableArray new];
...
Note that the code you posted is not initializing the loginPage object, perhaps that is a typo in the post, otherwise you will need to initialize the object before using it.
Hope this helps.

You have not instantiated your object. Try this code :
User = self.Username.text;
UserPassword = self.UserPass.text;
NSMutableArray *arrayUsernames = [[NSMutableArray alloc] init];
NSMutableArray *arrayPasswords = [[NSMutableArray alloc] init];
LoginPageViewController *loginPage = [[LoginPageViewController alloc] initWithNibName:#"LoginPageViewController" bundle:nil];
[arrayUsernames addObject:User];
[arrayPasswords addObject:UserPassword];
loginPage.registeredUsernames = arrayUsernames;
loginPage.registeredPassword = arrayPasswords;

First initiate LoginPageViewControllers object and registeredUsernames,registeredPassword array then directly pass your textfield value to array. Try this code
LoginPageViewController*loginPage = [[LoginPageViewController alloc] initWithNibName:#"LoginPageViewController" bundle:nil];
loginPage.registeredUsernames=[[NSMutableArray alloc]init];
loginPage.registeredPassword=[[NSMutableArray alloc]init];
[loginPage.registeredUsernames addObject:self.Username.text];
[loginPage.registeredPassword addObject:self.UserPass.text];
NSLog(#"%#", loginPage.registeredUsernames);
NSLog(#"%#", loginPage.registeredPassword);

Related

Why RemovingAllObjects, Assigning nil and then initializing NSMutableArray crashes iOS App?

I have to assign countries, states & cities getting in response from JSON, in an NSMutableArray, Which is initialized in Modal Class.
I will have to remove all objects in order to set new states and cities, doing that crashes with error
incorrect checksum for freed object - object was probably modified
after being freed.
*** set a breakpoint in malloc_error_break to debug
then in answer https://stackoverflow.com/a/12050676/5568169 came to know that assigning nil to mutableArray will work, and it worked, But now User can again select another country, So now allocating memory [myMutableArray alloc] init]], gives me the same error i was getting in starting.
-(void)fetchStates:(NSString*)idString {
[registrationModalContactVC.allStateArray removeAllObjects];
registrationModalContactVC.allStateArray = nil;
[registrationModalContactVC.allStateDict removeAllObjects];
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];
}
Kindly help
You should not be dong this :
[myMutableArray alloc] init]
What you mean to do is :
myMutableArray = [[NSMutableArray alloc] init];
i think your mutablearray is a datasource of a pickview , when you remove all objects, how is your pickview's status.
Removing below code is working, but it may give the same error again
registrationModalContactVC.allStateArray = nil;
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];

Objective C property passed reset to null after use

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.

addObject in NSMutableArray is copied or retained?

When you add object into collection, such as NSMutableArray, the object is copied, that is, value semantics, or is retained, that is, reference semantics?
I am confused in the example:
NSMutableString *testStr = [#"test" mutableCopy];
NSMutableArray *arrayA = [[NSMutableArray alloc] init];
[arrayA addObject:testStr];
NSLog(#"%#", arrayA); // output: test
testStr = [#"world" mutableCopy];
NSLog(#"%#", arrayA); // output: test
// testStr is copied - value semantics
NSMutableArray *testArr = [#[#1, #2] mutableCopy];
NSMutableArray *arrarB = [[NSMutableArray alloc] init];
[arrarB addObject:testArr];
NSLog(#"%#", arrarB); // output: [1, 2]
[testArr addObject:#3];
NSLog(#"%#", arrarB); // output: [1, 2, 3]
// testArr is retained - reference semantics
You can see: if the object is a NSMutableString, it looks like the object is copied - you change the object will not affect the object in the array.
However, if the object is a NSMutableArray, when you change the object, the object in the array also be changed - like you retain the object or pass by reference.
Am I missing something here?
Thanks.
Collections retain, not copy, objects that are added to them.
In your first part, adding an object to the array is not making a copy. Let's look at your code:
Here you created your NSMutableString and your NSMutableArray:
NSMutableString *testStr = [#"test" mutableCopy];
NSMutableArray *arrayA = [[NSMutableArray alloc] init];
Then you add your string to the array:
[arrayA addObject:testStr];
NSLog(#"%#", arrayA); // output: test
Next you're creating a new NSMutableString and assigning that to the variable testStr, which simply makes testStr point to the new object. It has no effect on the first string you created:
testStr = [#"world" mutableCopy];
NSLog(#"%#", arrayA); // output: test
So that's why your first block of code works how it does.
In your second block of code, it seems that you already understand why that works how it does, but just to make it clear, testArr always points to the same array, which you also added to arrarB, hence why printing out arrarB reflects the change you make to testArr.
String is retained too, in line:
testStr = [#"world" mutableCopy];
you create a new string and you assign its copy to the testStr local variable, so old retained string is still in the table
Instead of:
testStr = [#"world" mutableCopy];
you could try:
[testStr setString:#"world"];
This would modify 'old' string instead of making testStr to point on a new object
NSMutableString *testStr = [#"test" mutableCopy];
NSMutableArray *arrayA = [[NSMutableArray alloc] init];
[arrayA addObject:testStr];
NSLog(#"%#", arrayA); // output: test
testStr = [#"world" mutableCopy];
NSLog(#"%#", arrayA); // output: test
Here testStr is not copied to arrayA. After adding testStr object to array, testStr pointer itself changed to reference to some other object (lets say "World").
In the same was as you did for array, you can also append another string to testStr (instead of pointing to different string ) you can see the same results as testArr.
[testStr appendString:#" Test 2"];
`NSLog(#"%#", arrayA);` // output: test Test 2

NSMutableDictionary giving all together same values instead of new fresh values

i am constructing All data to look like a response data dictionary from a server.
Now, newsFeedsDict1 which should Dictionary for both Bolly and Global is not only showing all data inside Global dictionary only. While my for loop is running its showing correct data for Bolly. but for 2nd time its showing Bolly's data also in Global dictionary.
if(internetStatus == NotReachable)
{
NSMutableArray *titleArr = [[NSMutableArray alloc] init];
NSMutableArray *wholeFeeds = [[[NSMutableArray alloc] init] autorelease];
[titleArr addObject:#"Bolly"];
[titleArr addObject:#"Global"];
for (NSString *title in titleArr) {
//titleArr = [[NSUserDefaults standardUserDefaults] objectForKey:#"TitleArray"];
NSLog(#"TITle arr %#",titleArr);
NSLog(#"No internet");
OrderedDictionary *newsFeedsDict1 = [[[OrderedDictionary alloc] init] autorelease];
NSMutableDictionary *newsFeedsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
NSMutableDictionary *articleDict = [[[NSMutableDictionary alloc] init] autorelease];
[articleDict setObject:myLocalArray forKey:#"article"];
[newsFeedsDict setObject:articleDict forKey:#"Articles"];
[newsFeedsDict setObject:title forKey:#"#name"];
[newsFeedsDict1 setObject:newsFeedsDict forKey:title];
[wholeFeeds addObject:newsFeedsDict1];
NSLog(#"news feed dict %#",newsFeedsDict1);
NSMutableDictionary *temparticleDictionary = [[NSMutableDictionary alloc] init];
self.articleDictionary = temparticleDictionary;
self.categoriesDictionary = [[NSMutableDictionary alloc] init];
self.categoriesDictionary =newsFeedsDict1;
[self createArticleDictionaryForCategory:newsFeedsDict];
}
}
It's difficult to tell what your code is supposed to do, and how you can tell that one dictionary has the same content as another. A couple problems:
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
The first line is entirely unnecessary. You're creating a new array, assigning it to myLocalArray, and then assigning something else to myLocalArray. You do the same later with self.categoriesDictionary. This leads me to believe that you have some misunderstanding with respect to object pointers.
So, the array that you get from your shared database ends up at myLocalArray, and you then add that array to the dictionary at articleDict, and then add articleDict to newsFeedDict and in turn add that to newsFeedDict1. And then you log newsFeedDict1. You do exactly the same for both your titles, #"Bolly" and #"Global", so it's not at all surprising that you see the same output in both cases.
I'm having a hard time seeing why you expect the output to be different, and I have to guess that again it's due to a misunderstanding of what happens when you assign one object pointer to another. Or, perhaps you're expecting the array that you get from [[Database sharedDatabase] getArticleData:title]; to be different because you're passing in different titles. Maybe you really should be getting different arrays there; it would be a good idea to look at what happens in that -getArticleData: method and whether it really is giving you the right results for each title.
If that doesn't help, take some time to clean up your code so that it's easier for us, and more importantly, for you to really see what's going on. Also, please edit your question to give a better description of what you're seeing and how that's different from what you expect to see.
Can you write the snippet for *getArticleData()* method

NSMutableArray initializing from viewDidLoad

arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
and
arrayOfElements = [[NSMutableArray alloc] init];
arrayOfElements = [someObj getArray];
What's the difference?
The first arrayOfElements does not seem to lose its objects when it returns count in numberOfRowsInSection:(NSInteger)section, but the second one does. I get EXC_BAD_ACCESS when I do it the second way.
EDIT:
Can I suppose now that this is the best way,
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];
because I am initializing an array with the contents of whatever will be autorelease'd, and I now have a fully independent array in the current class, that is viewDidLoad, oops sorry, ViewController.
This line creates an NSMutableArray from an existing array
arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
This combination first creates an NSMutableArray and then instantly discards it replacing it with what is returned by [someObj getArray]
arrayOfElements = [[NSMutableArray alloc] init]; // Create new NSMutableArray
arrayOfElements = [someObj getArray]; // Throw away the newly created array and replace with the result of [someObj getArray]
If you are not using ARC then it is purely by luck that either would work.
In both cases arrayOfElements is being assigned an autorelease'd object - which will be cleared soon (most likely the next runloop). It is only by chance that nothing else has been written over this point of memory which allows one of your implementations to still work.
If you are not using ARC then really you should update your project to be using it will handle a lot of cases like this for you.
You should definitely be using properties (not bare ivars) as this will help reduce memory issues (for non-ARC) and give a more consistent interface to your code.
In your header (or class extension) declare the property like this
#property (nonatomic, strong) NSMutableArray *arrayOfElements;
Now for ARC you can simple do
[self setArrayOfElements:[[someObj getArray] mutableCopy];
for non-ARC you can do
NSMutableArray *array = [[someObj getArray] mutableCopy];
[self setArrayOfElements:array];
[array release]; array = nil;
Also note that getArray is a bad method name.
The use of “get” is unnecessary, unless one or more values are returned indirectly.
Coding Guidelines
When you are adding objects to mutable array from another array, try this:
[arrayOfElements addObjectsFromArray: [someObj getArray]];
If you're not using ARC, you need to make sure its retained.
if (arrayOfElements) {
[arrayOfElements release];
}
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];

Resources