appending array in objective c - objective-c

i read this command from cocoadev.com but was not able to get it plz help me in explaining what this line of code do
[array addObject:[NSNumber numberWithInt:15]];

You have to use NSMutableArray instead of NSArray to use the method addObject:.

It adds a number object with the integer value of 15 to the collection (presumably an NSMutableArray) called array.

Related

NSDictionary to NSArray not converting (Signal SIGBRT)

I have a very simple HTTP query that runs and grabs some JSON object in another class. I have an NSDictionary object called finalDataArray being defined. I am basically trying to loop through and append names to a table, the number of rows are being calculated correctly. finalDataArray.count
But when I try to do this, I get an error on the first line below.
NSArray *contactArray = [finalDataArray allKeys];
NSLog(#"%#", contactArray);
Because I am such a newb, I didn't realize that if you had a "Miltidimensional" JSON object... it should be NSDictionary, but if you have a single dimenstional object... it should be NSArray which fixes my issue...
Thanks for all the help.

extracting properties from NSArray of objects

Is there any of way (other than looping) of extracting a particular property of all objects in an array. So say there in an array of people. I want to extract all their first names into an array.
Key Value coding will help you with that:
NSArray *result = [people valueForKey:#"firstname"];
I got answer for my question.
This is how we can achieve the same in swift.
let arraytWithProperties = arrayWithObjects.map{ $0.propertyName }

addObject to NSMutableArray in NSMutableArray

I have my UITableView data organized such that the sections of the table are the array elements of a NSMutableArray. Each element itself is a NSMutablearray of NSMutableDictionary, representing the row of each section and the data for each row.
When I create this, I load all the data and create each section's NSMutableArray, then add them one at a time to the "outer" NSMutableArray. I reference the rows by:
[[[objData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:#"name"]
and have had no problems doing this. I think the advantage here is I can have a variable number of sections, which I need, and a variable number of rows in each section, also a requirement.
I do, however, get an error when I try to add a row to a section.
[[objData objectAtIndex:0] addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: objectID,#"id",nameVar,#"name",nil]];
doesn't work. Is this the problem, or is there a problem with the way I've layed out the array? I'm thinking that the inner NSMutableArray is only alloted so much memory, but I would have thought the array is just an array of references and wouldn't have that issue. I could be wrong - it isn't the first time.
Thanks in advance for wrapping your head around this.
How are you intializing the array and the values inside it? I think you have an array that is not mutable.
Also an NSMutableArray has a variable size, calling initWithSize is completely unnecessary. The array will resize if it needs to.

Better way to convert NSArray of NSNumbers to array of NSStrings

I have an NSArray consisting of NSNumbers and I want to convert this to an NSArray of NSStrings, by getting the stringValue of each NSNumber in the first array.
The method that comes to my mind is iterating each value in the first one, getting its string value and adding it into another array. But there should be a more elegant solution for this. Do you know one?
NSArray implements the Key-Value Coding method valueForKey: in such a way that it returns a new array. The new array contains the results of asking each object in the original array for the specified value. In this case, NSNumber has its stringValue, so all you have to do is:
NSArray * b = [a valueForKey:#"stringValue"];
Plain old fast enumeration (or enumerateObjectsUsingBlock:) wouldn't be a terrible solution, though. NSArray's implementation of valueForKey: most likely uses a for loop internally, and that would be pretty readily understood by anyone who reads it later.

How to convert objective C array to a C array?

I have NSArray in Objective C, which store int only. How can I convert it to C array? Thanks.
My Objective C array:
NSArray *myArray = [[NSArray alloc] initWithObject:#"1", #"4", #"8", nil];
Allocate a chunk of memory using malloc to hold enough of the items (in this case, NSString*s and iterate over the NSArray, adding each item (and retaining each item) into an appropriate position in the c array.
Don't forget in doing this, you will have to send a release message to each member in the c array when you're done with it, before you also have to free() the array itself.
I guess you cannot explicitly since an NSArray encapsulates a C array, but an array of pointers to the objects referenced. So the better you can get is to retrieve (in reality copy) that array of pointers.
If you want to get the value of the objects you need to 'query' them for their values, have a look here