[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance - objective-c

I have a NSMutableArray, which I need to chance its values, but I have this error:
[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x5291db0
This is the declaration of my NSMutableArray:
NSMutableArray *selectedOptions = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], nil];
Then, I'm using replaceObjectAtIndex method, of this way:
[self.selectedOptions replaceObjectAtIndex:0 withObject:[NSNumber numberWithInteger:1]];
But I get, that error, and I'm using NSMutableArray.
Thanks

You are creating a regular non-mutable NSArray. Your code should be
NSMutableArray *selectedOptions = [NSMutableArray arrayWithObjects:[NSNumber numberWithInteger:0], nil];
Objective C is very dynamic, so it does not catch this mistake at compile time.

You need to initialize your NSMutableArray by doing
NSMutableArray *selectedOptions = [NSMutableArray alloc] init];
By initializing it with NSArray, you can no longer use the repalceObjectAtIndex:withObject: method and that's the cause of your problem.
After initializing your NSMutableArray with the line above, simply add objects to it with the addObject method.

Related

Unrecognized selector with array objects.

The NSImageView objects once in the array become strings. How to I convert them into NSImageView to avoid unrecognized selector error generated by the last line?
NSArray *array = [NSArray arrayWithObjects: #"chip1”, #"chip2” nil],
for (id image in array) {
[image setImage: nil];
}
The #"..." notation in Objective-C is shorthand for creating an NSString. In the above code example, you're creating an array of NSString's, which is why you're getting the unrecognized selector error when you call setImage:.
Assuming chip1 and chip2 are instances of NSImageView, you should be doing the following:
NSArray *array = [NSArray arrayWithObjects:chip1, chip2, nil];
You can avoid issues like this in future, by using Objective-C Generics when defining your array:
NSArray <NSImageView*>*array = [NSArray arrayWithObjects:chip1, chip2, nil];
This would give a compiler error if you tried to add a class other that NSImageView to the array.

[__NSCFString superview]: unrecognized selector sent to instance

I am trying to copy a NSMutableArray* into NSArray*, but it does not works and it generates a [__NSCFString superview]: unrecognized selector sent to instance error . Here is the code:
//where gc is a NSDictionary*, recentKey is a NSString*, and _objects is a NSArray*
//gc is an singleton is used to save chache data, with NSKeyedUnarchiver class
//_objects is used to reload the UITableView's data
NSArray *savedNews = [[NSArray alloc] initWithArray:[gc objectForKey:recentkey]];
//this not works...why??
_objects = [[NSArray alloc] initWithArray:savedNews];
Resolution:
Yes, as Herman suggests, the error was external. The savedNews Array was using a class with NSEncoding with an error:
- (void)encodeWithCoder:(NSCoder *)encoder {
//...where element was NSString* and not "UIImageView"
// element should be imgView
if (imgView) [encoder encodeObject:element forKey:#"imgView"];
}
Thanks for all guys.
Somewhere in your App is the superview of an NSString object fetched.
I guess that you assigned an NSString object to something where a UIView is expected.
Could be something like:
someButton.textLabel = someString; // wrong - but should generate a compiler warning
instead of
someButton.textLabel.text = someString; // correct
This is not directly related to your array issue.
First, check what the object in the dictionary is for that key.
NSLog(#"GC Object type for recentkey:%#", [[gc objectForKey:recentkey] class]);
You can only pass an NSArray to initWithArray:
So if that object isn't already an NSArray but you want that object to be IN an array. The do this..
id obj = [gc objectForKey:recentkey]; //Because I have no idea what this is
NSArray *savedNews = [NSArray arrayWithObject:obj];

Is this a correct declaration of an Array in Objective c?

NSArray *arr=[[[NSArray alloc]autorelease]autorelease];
Close, but no cigar.
If you want an autoreleased NSArray, you'd need to use:
NSArray *arr = [[NSArray alloc] init] autorelease];
That said, this will simply get you an empty immutable array, so you'll most likely want to populate it via one of the initWithObjects: style methods. (See the full NSArray class reference for more information.)
Or you can just declare it like this:
[NSArray array];
This gives you an autoreleased instance of the array.

How to convert NSArray to NSMutableArray

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *tempPeoples=[[NSMutableArray alloc]init];
for(int i=0;i<nPeople;i++){
ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);
[tempPeoples addObject:i1];
// [peoples addObject:i1];
}// end of the for loop
peoples=[tempPeoples copy];
This code gives exception b/c I want to convert NSMutableArray to NSArray
Please Help
The subject reads, "How to convert NSArray to NSMutableArray". To get an NSMutableArray from an NSArray, use the class method on NSMutableArray +arrayWithArray:.
Your code does not show the declaration for peoples. Assuming it's declared as an NSMutableArray, you can run into problems if you try to treat it as such. When you send the copy message to an NSMutableArray, you get an immutable object, NSArray, so if you try to add an object to a copied NSMutableArray, you will get an error.
CFArrayRef is toll free bridged to NSArray, so you could simplify your code this way:
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
//NSMutableArray *tempPeoples = [NSMutableArray arrayWithArray:(NSArray*)allPeople];
// even better use the NSMutableCopying protocol on NSArray
NSMutableArray *tempPeoples = [(NSArray*)allPeople mutableCopy];
CFRelease(allPeople);
return tempPeoples; // or whatever is appropriate to your code
In the above code tempPeoples is an autoreleased NSMutableArray ready for you to add or remove objects as needed.
This code gives exception b/c I want to convert NSMutableArray to NSArray
This is very unlikely. NSMutableArray is a derived class of NSArray, so copying in that direction isn't an issue.
Maybe you've got an error because you don't retain the array. arrayWithArray returns an autorelease object. Either use [tempPeoples copy] or [[NSArray alloc] initWithArray: tempPeoples];
Simply you can do that
NSArray *yourArray ; // Static Array
NSMutableArray* subArrayData = [yourArray mutableCopy];

How to append two NSMutableArray's in Iphone sdk or append an NSArray With NSMutableArray?

I need to append two NSMUtableArray's can any one suggest me how it possible?
My code is:
NSMutableArray *array1 = [appDelegate getTextList:1];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];//I am getting exception here.
Anyone's help will be much appreciated.
Thanks all,
Lakshmi.
What's probably happening, is that your [appDelegate getTestList:1] is not actually returning a NSMutableArray, but a NSArray. Just typecasting the array as mutable by holding a pointer to it like that will not work in that case, instead use:
NSMutableArray *array1 = [[appDelegate getTextList:1] mutableCopy];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];
Or you could store the 'textList' variable that you have in your appDelegate as an NSMutableArray in the first place. I am assuming that you have an NSArray of NSArrays (or their mutable versions). Eg.
// In the class interface
NSMutableArray *textLists;
// In the function in which you add lists to the array
NSMutableArray *newTextList;
[self populateArray:newTextList]; // Or something like that
[textLists addObject:newTextList];
Note: that you will probably have a different workflow, but I hope that you get the idea of storing the actual lists as NSMutableArrays.
Another Note: the second method WILL modify in place the NSMutableArray that [appDelegate getTextList:1]; returns
Try this:
NSMutableArray *result =
[[appDelegate getTextList:1] mutableCopy]
addObjectsFromArray:[appDelegate getTextList:2]];
You're getting the exception because you're trying to send mutating messages to an immutable array.