How to add multiple strings to one index in NSMutableArray [duplicate] - ios

This question already has answers here:
Having trouble adding objects to NSMutableArray in Objective C
(2 answers)
Closed 8 years ago.
I want to add a first an last name to one index of a mutable array.
I'm loading a first and last name from the AdressBook API and I want the first and last name in an array, so I can display that later to the user.
Currently, I can log the first an last name, but when I try to add these to an array, the array stays null.
This is the code I'm using:
[_names addObject:[NSString stringWithFormat:#"%# %#", firstName, lastName]];
NSLog(#"%#", _names); //prints 'null'
NSLog(#"Name:%# %#", firstName, lastName);//prints 'John Appleseed'
Why isn't my array being updated?

Have you made sure you initiated the array properly using _names = [[NSMutableArray alloc] init]? You cannot add objects into an uninitialized array.

Related

Mutating method sent to immutable object - iOS [duplicate]

This question already has answers here:
insert object in an NSMutableArray saved with NSUserDefaults
(6 answers)
Closed 8 years ago.
I have an NSMutableArray and I am trying to use the insertObjectAtIndex method, but it does not work. It seems to think I am trying to do something mutable with an immutable array..... but I am using an NS ..... MUTABLE.... Array. So what an earth could be wrong??
Here is my code:
if ([dataArray count] == 0) {
// Initialise the audio arrays.
dataArray = [[NSMutableArray alloc] init];
// Now add the data to the array.
[dataArray addObject:#"test 1"];
}
else {
[dataArray insertObject:#"test 2" atIndex:0];
}
NSLog(#"d in: %#", dataArray);
The dataArray is defined in the header file like so:
NSMutableArray *dataArray;
Thanks for your time, Dan.
Oh sorry everyone, I just figured out what was wrong. I did edit the code in my question a bit, but basically what I am actually populating the NSMutableArray with is an array stored in an NSUserDefault, (which of course returns immutable arrays only).
So even though I am using an NSMutableArray to store the data, its still has immutable contents. In order to over come this, I ended up using "mutableCopy" iOS method to get a mutable copy of the contents of the NSUserDefaults array.

Sort NSMutableArray with objects [duplicate]

This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Closed 9 years ago.
Hopefully someone can help.
I'm adding multiple objects to a NSMutableArray and I need to sort the order based on the first element which will always be a number.
However I'm unsure how to do this?
For example:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *object = [NSArray arrayWithObjects: #"1",#"Test",#"Test"];
[array addObject:object];
Thanks
If your array always contains other arrays, and the first element of the innermost array is always a string containing a number, you could use the NSMutableArray method sortUsingComparator to sort your array:
[array sortUsingComparator: ^(NSArray* obj1, NSArray* obj2)
{
int value1 = [obj1[0] integerValue];
int value2 = [obj2[0] integerValue];
if (value1==value2)
return NSOrderedSame;
else if (value1 < value2)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
];
In the sortUsingComparator family of methods, you supply a block of code that the sort method uses to compare pairs of objects in your array. The block uses the standard typedef NSComparator, which takes 2 objects as parameters and returns a value of type NSComparisonResult.
The code above will probably crash if all the objects in your array are not arrays of strings. (Actually it would work if the first element of each component array was an NSNumber, since NSNumber also responds to the integerValue message.)
If you are going to use this code in a very controlled environment where you can be sure that the data you are sorting is well-formed, it should work as written. If there is any chance that the objects in the array would be of a different type, or be empty, or that their first element would not respond to the integerValue messages, then you should add error checking code.
If you sort your array alphanumerically, the object #"1" will appear before any words. Keep in mind though that #"1" in your code above is a string, not a number.
As to how to sort an array, look into [NSArray sortedArrayUsingComparator:] and similar methods.

Can I create a pointer to an object using its name in a string variable? [duplicate]

This question already has answers here:
Can I do NSVariableFromString like NSClassFromString and NSSelectorFromString?
(3 answers)
Closed 9 years ago.
In essence, what I want to do is to has a string I've created as the name for a variable. However with the code below I understandably get a redefinition error.
NSString *pointerName = #"myPointer";
NSArray *pointerName = [NSArray array];
Is there a way to use the string as its content rather than having the compiler think I mean that's the name of the new object I'm trying to create?
Instead of store string value for pointer. You can use like this
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for (int i = 0; i < [array1 count]; i++)
{
[dict setObject:[NSArray array] forKey:[array1 objectAtIndex:i]];
}
I hope it is useful for you..
According to cocoa convention you cannot create the pointer of an object of the same name variable. Also the apple doc says the same thing please refer the link https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html

Getting and array of values from an array of CustomObject [duplicate]

This question already has answers here:
Getting an NSArray of a single attribute from an NSArray
(3 answers)
Closed 9 years ago.
I have the following issue, I need to get an array of values from an array of custom Objects. Is there a method for doing this without iterating the principal array, let me graphic it a little bit.
NSArray *principalArray = #[
customObject1,customObject2,customObject3,....customObject(n)
];
This customObject instances have a properties lets say id,name,lastname.
I want to get an NSArray with the value of name from the principalArray
Thanks for your help.
EDIT:
As somebody pointed out in comments: its a duplicate of existing SO question: Getting an NSArray of a single attribute from an NSArray
There is a method for NSArray - valueForKey - with key being an attribute of your first array. This method returns you an NSArray from an NSArray.
In your case you can do the following:
NSArray *nameArray = [principalArray valueForKey:#"name"];

sorting of an array alphabetically in objective-c [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
Closed 10 years ago.
I want to sort my array alphabetically in objective-c. I have implemented it this way.
//Sorting of the Array
NSArray *sortedArray = [arrName sortedArrayUsingComparator:^(Cars *firstObject, Cars *secondObject) {
return [firstObject.str_name compare:secondObject.str_name];
}];
arrName =[NSMutableArray arrayWithArray:sortedArray];
The problem is all the numbers appear followed by captial letters words followed by lowercase letters items...
I want it to appear alphabetically-> meaning to say that the capital letters and lowercase letters maybe mixed.
Replace compare: with caseInsensitiveCompare:.
Since arrName is mutable, use the 'sortUsingComparator' method instead. It will sort the mutable array in place without creating a new array.
[arrName sortUsingComparator:^(Cars *firstObject, Cars *secondObject) {
return [firstObject.str_name caseInsensitiveCompare:secondObject.str_name];
}];
try this,
NSArray *array=[[NSArray alloc]initWithObjects:#"Object1",#"object1", nil];
array =[array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"%#",array);

Resources