nsmutablearray interview questions
Top nsmutablearray frequently asked interview questions
I know I can create an NSArray
with @[@"foo", @"bar"]
or an NSDictionary
with @{@0 : @"foo", @1 : @"bar"}
.
Is there a literal syntax for creating an NSMutableArray
or an NSMutableDictionary
?
Source: (StackOverflow)
So I have a nsmutablearray with a bunch of objects in it. I want to create a comma separated string of the id value of each object.
Source: (StackOverflow)
Is there an efficient way to add an object to start of an NSMutableArray? I am looking for a good double ended queue in objective C would work as well.
Source: (StackOverflow)
How can an object be added at a specific index of an NSMutableArray
?
How is an object added to the front of the array?
Source: (StackOverflow)
I'm using an array to store cached objects loaded from a database in my iPhone app, and was wondering: are there any significant disadvantages to using NSMutableArray that I should know of?
edit: I know that NSMutableArray can be modified, but I'm looking for specific reasons (performance, etc..) why one would use NSArray instead. I assume there would be a performance difference, but I have no idea whether it's significant or not.
Source: (StackOverflow)
I'm able to put the contents of an NSSet
into an NSMutableArray
like this:
NSMutableArray *array = [set allObjects];
The compiler complains though because [set allObjects] returns an NSArray
not an NSMutableArray
. How should this be fixed?
Source: (StackOverflow)
I have tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING is working for me. I tried setting it equal to a newly allocated and initialized NSMutableArray, just allocating, initializing the variable by itself, every combination I could think of and always the same result.
Here's the code:
Object.h
NSMutableArray *array;
@property (copy) NSMutableArray *array;
Object.m
@synthesize array;
if ( self.array ) {
[self.array addObject:anObject];
}
else {
self.array = [NSMutableArray arrayWithObjects:anObject, nil];
}
NOTE: In debug "anObject" is NOT nil at time of execution...
I have tested anObject and it isThe initialization works just fine, but I keep getting the error below when I try to addObject: to self.array.
2010-07-10 11:52:55.499 MyApp[4347:1807] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480
2010-07-10 11:52:55.508 MyApp[4347:1807] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480'
Does anyone have any idea what's going wrong?
Source: (StackOverflow)
What is the difference between the "copy" & "mutableCopy"?
EDIT_001:
My original post was a bit of a mess, partly due to a lack of understanding and partly due to a bit of pilot error on my part. Here is my attempt to better explain how "copy" & "mutableCopy" work.
// ** NSArray **
NSArray *myArray_imu = [NSArray arrayWithObjects:@"abc", @"def", nil];
// No copy, increments retain count, result is immutable
NSArray *myArray_imuCopy = [myArray_imu copy];
// Copys object, result is mutable
NSArray *myArray_imuMuta = [myArray_imu mutableCopy];
[myArray_imuCopy release];
[myArray_imuMuta release];
.
// ** NSMutableArray **
NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil];
// Copys object, result is immutable
NSMutableArray *myArray_mutCopy = [myArray_mut copy];
// Copys object, result is mutable
NSMutableArray *myArray_mutMuta = [myArray_mut mutableCopy];
[myArray_mutCopy release];
[myArray_mutMuta release];
EDIT_002:
- mutableCopy always returns a mutable result.
- copy always returns an immutable result.
thank you for all the answers, comments ... much appreciated.
gary
Source: (StackOverflow)
I have declared an NSMutableArray *categories
in my view controller .h file, and declared a property for it.
In the parser:foundCharacters:
method of the NSXMLParser
delegate in my .m file, I have this code:
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
element = string;
[self.categories addObject:element];
}
}
But when I hover over the [self.categories addObject:element]
line after stepping into it in debug mode, XCode tells me the size is 0x0, 0 objects. There are 3 elements in my XML file so 3 items should be in the array.
I'm missing something really obvious and I can't figure out what.
Source: (StackOverflow)
I have a UITableView displaying a list of Cities.
I want to separate them by State. I can't seem to figure out how to get it to pick the right items out of my Array.
If Section 1 (Arizona) has 2 Cities and Section 2 (California) has 2 Cities, during cellForRowAtIndexPath, Section 2, City 1 has an index of 0, even though it's the 3rd item in my array.
I thought about just turning my City Array into a State Array, where each item holds an Array of Cities, but I still don't know what section I'm on and therefore don't know which City Array under the States Array I would need to access.
Any help would be appreciated.
Source: (StackOverflow)
This question already has an answer here:
I'm trying to achieve something like this
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
NSLog(@"Array: %@", myArray);
//logs A, B, C.
//reverse code here
NSLog(@"Array: %@", myArray);
//logs C, B, A
The code isn't exact, just a demo. But you get the idea.
Any way to do this?
Source: (StackOverflow)
I recently started working with UIActivity to share my app to the world, but I have few problems. First, I didn't find how to set the subject of my email. Is there any way? Second, when I set the body text of my email, there is a extra "enter" (first line of the email is blank and my text starts at the second line). Here's the code:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Test", nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:array applicationActivities:nil];
And in the email, it shows that:
"
Test
"
Third: is there a way to know which sharing method has been selected? Because I want to include a hashtag in my post when the user shares on twitter, but now it gets integrated in the email also, which obviously doesn't make sense.
Thanks!
Source: (StackOverflow)