problem when trying to put the content of NSArray in NSMutableArray - ios

please i try to put the content of two NSArray in two NSMutableArray but it seems not working :
NSMutableArray *newArrayTypeCarburant = [[NSMutableArray alloc]init];
NSMutableArray *newArrayNomStation = [[NSMutableArray alloc]init];
for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
{
[newArrayTypeCarburant addObjectsFromArray: [topStation.listeTypesDesCarburants objectAtIndex:i]];
}
for(int i=0; i <[topStation.listeDesEnseignes count]; i++)
{
[newArrayNomStation addObjectsFromArray: [topStation.listeDesEnseignes objectAtIndex:i]];
}
topStation.listeDesEnseignes and topStation.listeTypesDesCarburants are two NSArray and global variables declared in the appdelegate class . Help please, thx in advance ))
EDIT
i try that :
NSMutableArray *newArrayTypeCarburant = [[NSMutableArray alloc]init];
NSMutableArray *newArrayNomStation = [[NSMutableArray alloc]init];
for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
{
[newArrayTypeCarburant addObject: [topStation.listeTypesDesCarburants objectAtIndex:i]];
}
for(int i=0; i <[topStation.listeDesEnseignes count]; i++)
{
[newArrayNomStation addObject: [topStation.listeDesEnseignes objectAtIndex:i]];
}
self.pickerArrayTypeCarburant = newArrayTypeCarburant;
self.pickerArrayNomStation = newArrayNomStation;
the first picker contains value from the array but when i try to click on the second picker, an exception was thrown :
2011-05-04 15:10:06.631[1065:207] -[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6e389a0
2011-05-04 15:10:06.633[1065:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6e389a0'

You are using NSArray addObjectsFromArray: which will copy an entire array but you are doing it once for each container member (as well as not passing the correct type since it expects an NSArray ref and you're passing the individual elements). Instead try copying the arrays like this and remove the for-loops:
[newArrayTypeCarburant addObjectsFromArray: topStation.listeTypesDesCarburants];
[newArrayNomStation addObjectsFromArray: topStation.listeDesEnseignes];
If you still need to walk through them one at a time use the code you have but change addObjectsFromArray: to addObject::
for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
{
// perhaps some predicate here...
[newArrayTypeCarburant addObject: [topStation.listeTypesDesCarburants objectAtIndex:i]];
}

you could just use arrayWithArray.
NSMutableArray *newArrayNomStation = [NSMutableArray arrayWithArray:topStation.listeDesEnseignes];

Related

Unrecognized selector sent to instance Objective-C

I am receiving error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString
subjectType]: unrecognized selector sent to instance
I am trying to sort students in my app to arrays by the subject type that they are learning.
AMStudent* student = [[AMStudent alloc] init];
NSMutableArray* studentArray = [[NSMutableArray alloc] init];
NSArray* studentNameArray = [NSArray arrayWithObjects: #"Student1", #"Student2", #"Student3", #"Student4", #"Student5", #"Student6", #"Student7", #"Student8", #"Student9", #"Student10", nil];
[studentArray addObjectsFromArray:studentNameArray];
for (NSInteger i = 0; i < [studentNameArray count]; i++) {
student.name = [studentNameArray objectAtIndex: i];
[student randomAnswer];
NSLog(#"%#", student.description);
}
NSMutableArray* techArray = [NSMutableArray array];
NSMutableArray* humArray = [NSMutableArray array];
for (AMStudent* stud in studentArray){
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
}
I cant figure out what exactly I am doing wrong, because it crashes in this stage:
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
You are calling
stud.subjectType
in the studentArray after copying the studentNames (NSString) to the student array:
[studentArray addObjectsFromArray:studentNameArray];
NSString won't recognize subjectType.
You fill studentArray using:
[studentArray addObjectsFromArray:studentNameArray];
So studentArray contains NSString instances. You then attempt to process the array using:
for (AMStudent* stud in studentArray){
This does not magically convert the NSString instances in studentArray into AMStudent instances. You don't get an error at this point as studentArray can contain objects of any type so the compiler just trusts you know what you are doing and places a reference to an NSString into stud. You then do:
if ((stud.subjectType ...
and this requires stud to reference an AMStudent object, which it does not, it references a (constant) string and so you get the error:
NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance
Instead of copying the names of the students into studentArray you need to create instances of AMStudent and add those to the array. Did you intend to do that in the first loop maybe?
HTH
techArray and humArray (NSArray) type change not working add object function.
NSMutableArray *newtechArray = [techArray mutableCopy];
NSMutableArray *newhumArray = [humarray mutableCopy];
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[newtechArray addObject:stud];
} else {
[newhumArray addObject:stud];
}
Thanks a lot for Your wide answer, I understood my mistake. Just added one more loop and added object student.
for (NSInteger numberOfStudents = 0; numberOfStudents < 10; numberOfStudents ++){
AMStudent* student = [[AMStudent alloc] init];
student.name = [studentNameArray objectAtIndex:numberOfStudents];
}
[studentArray addObject:student];

How to add an index to an NSMutableArray based on value in another array?

I've looked at lots of questions about NS(Mutable)Arrays. I guess I am not grokking the concept, or the questions don't seem relevant.
What I' trying to do is the following:
Incoming Array 1:
Name
Code
Start time
End Time
etc
Incoming Array 2
Code
Ordinal
What I want:
Ordinal
Name
Code
Start time
End Time
etc
This is my code at present:
int i=0;
for (i=0; i < stationListArray.count; i++) {
NSString *slCodeString = [stationListArray[i] valueForKey:#"Code"];
NSLog(#"slCodeString: %#", slCodeString);
int j=0;
for (j=0; j< lineSequenceArray.count; j++) {
NSString *lsCodeString = [lineSequenceArray[j]valueForKey:#"StationCode"];
NSLog(#"lsCodeString: %#", lsCodeString);
if ([slCodeString isEqualToString:lsCodeString]) {
NSLog(#"match");
NSString *ordinalString = [lineSequenceArray[j] valueForKey:#"SeqNum"];
NSLog(#"ordinalString: %#", ordinalString);
[stationListArray[i] addObject:ordinalString]; <------
}
}
}
I'm logging the values and they return correctly.
The compiler doesn't like the last statement. I get this error:
[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7f9f63e13c30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7f9f63e13c30'
Here is an excerpt from the StationListArray:
(
{
Address = {
City = Greenbelt;
State = MD;
Street = ".....";
Zip = 20740;
};
Code = E10;
Lat = "39.0111458605";
Lon = "-76.9110575731";
Name = Greenbelt;
}
)
NSString *ordinalString = [lineSequenceArray[j] valueForKey:#"SeqNum"]; //Is NSString
[stationListArray[i] addObject:ordinalString];//<----- trying to call addObject method of NSMutableArray on NSDictionary -> Not GOOD
When you do [stationListArray[i] you get NSDictionary in your case
(Generally it returns an NSObject that is inside the NSArray at the given index, in your case is NSDictionary).
So in order to complete your desired operation: you should make an NSMutableDictionary instance (In this case it should be the mutableCopy from the stationListArray[i]'s NSObject which is NSDictionary, when you do mutableCopy it copies the entire NSDictionary and makes it Mutable)
make the changes on it and then assign it in to the stationListArray[i]
For example:
NSMutableDictionary * tempDict = [[stationArray objectAtIndex:i]mutableCopy];//Create a mutable copy of the `NSDictionary` that is inside the `NSArray`
[[tempDict setObject:ordinalString forKey:#"Ordinal"]; //In this line you are adding the Ordinal `NSString` in to the tempDict `NSMutableDictionary` so now you have the desired `NSMutableDictionary`. You can change the key to whatever you wish.
[stationArray replaceObjectAtIndex:i withObject:[tempDict copy]];//Swap the original(old NSDictionary) with the new updated `NSMutableDictionary` I used the copy method in order to replace it with the IMMUTABLE `NSDictionary`
[stationListArray[i] addObject:ordinalString]; <------
This is not an NSMutableArray. You must use
[stationListArray addObject:ordinalString]; <------
instead of the what you have done.
Here is the way you can write a better understandable code because for me the code is not clear.You can also try like this in loop to achieve what you want.
NSMutableArray *array = [NSMutableArray new];
NSMutableDictionary *dictMain = [NSMutableDictionary new];
NSMutableDictionary *dictAddress = [NSMutableDictionary new];
[dictAddress setValue:#"Greenbelt" forKey:#"City"];
[dictAddress setValue:#"MD" forKey:#"State"];
[dictAddress setValue:#"....." forKey:#"Street"];
[dictAddress setValue:#"20740" forKey:#"Zip"];
[dictMain setValue:dictAddress forKey:#"Address"];
[dictMain setValue:#"E10" forKey:#"Code"];
[dictMain setValue:#"39.0111458605" forKey:#"Lat"];
[dictMain setValue:#"-76.9110575731" forKey:#"Lon"];
[dictMain setValue:#"Greenbelt" forKey:#"Name"];
[array addObject:dictMain];

Getting an uncaught exception while iterating through result set in iOS sqlite DB

following snippet gives me :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFString count]: unrecognized selector sent to instance 0x8ce0c00'
-(void)loadInfo{
// Create the query.
NSString *query = [NSString stringWithFormat:#"select * from tableName where category=\"%#\" ", #"cat1"];
// Load the relevant data.
NSArray *results = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
NSMutableArray *List = [[NSMutableArray alloc]init];
NSLog(#"Count..:%d",results.count);
for(int i=0; i<results.count; i++){
List = [[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:#"itemName"]];
}
NSLog(#"Result..:::: %d", [List count]);
}
What is going wrong here? Cant i print List count? dbmanager is what I have implemented using eg. given in - http://www.appcoda.com/sqlite-database-ios-app-tutorial/
This operation replaces List on every loop iteration with a different object:
List = [[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:#"itemName"]];
Judging from the error, the object happens to be NSString.
What you probably wanted is to add objects to your mutable array List:
[List addObject:[[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:#"itemName"]]];

iOS replaceObjectAtIndex Error

I am new to iOS development, I encounter error when replaceObjectAtIndex. Any wrong with my codes? Please help.Thanks.
self.myArray =array;
for (NSDictionary *data in array) {
NSString *fbid = [data objectForKey:#"id"];
for (int index = 0; index < self.myPersonArray.count; index ++) {
for (IP_PERSON *person in self.myPersonArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[self.myArray replaceObjectAtIndex:index withObject:person];
break;
}
}
}
Error is :
Terminating app due to uncaught exception NSGenericException, reason: '*** Collection <__NSArrayM: 0xa34f6c0> was mutated while being enumerated.
You cannot use fast enumeration and mutate collection at the same time, hence the error message. You can resort to using an usual for-loop.
You're iterating over array, which is equal to self.myArray.
Further down, you're editing this array when you do: [self.myArray replaceObjectAtIndex:index withObject:person];
To resolve this, just make self.array a mutableCopy of the original array:
self.myArray = [array mutableCopy];
You can take another temporary array and iterate over that array, so you are enumerating and mutating different array.
NSArray *tempArray = [yourArray copy];
for (IP_PERSON *person in tempArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[self.myArray replaceObjectAtIndex:index withObject:person];
break;
}
}
[tempArray release];
Alternatively you can iterate without an enumerator, you can use regular for loop with starting index, exit condition and increment as you have done in outer loop.
You can..
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]
self.myArray =array;
for (NSDictionary *data in array) {
NSString *fbid = [data objectForKey:#"id"];
for (int index = 0; index < self.myPersonArray.count; index ++) {
for (IP_PERSON *person in self.myPersonArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[dictionary setObject:person forKey:#(index)]; //Notice this line
break;
}
}
}
}
And then..
for(id key in dictionary) {
[self.myArray replaceObjectAtIndex:[key intValue] withObject:[dictionary objectForKey:key]];
}

Error setting value of NSMutableDictionary

I initialize this NSMutableDictionary:
define TAREFA #"TAREFA"
define DATA_ITEM #"DATA"
define HORA_ITEM #"HORAS"
define ID #"ID"
define STATE #"NA"
define APROVED #"APROVED"
define REJECTED #"REJECTED"
itensArray = [[NSMutableArray alloc] init];
for (int i=0; i< [listagens size]; i++)
{
NSMutableDictionary *tmpDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[[listagens item:i] WI_ID], ID,
[[listagens item:i] WI_TEXT], TAREFA,
[[listagens item:i] WI_CD], DATA_ITEM,
[[listagens item:i] WI_CT], HORA_ITEM,
STATE, STATE,
nil];
[itensArray addObject:tmpDictionary];
}
Later on, i do the following:
for (int i=0; i<[itensArray count]; i++)
{
if ([[[itensArray objectAtIndex:i] objectForKey:ID] isEqualToString:cell.idLabel.text]) {
[[itensArray objectAtIndex:i] setString:APROVED forKey:STATE];
break;
}
}
I initialize the array, then later on, i change a string of the NSMutableDictionary. When i set the value i get the following runtime error:
'NSInvalidArgumentException', reason: '-[__NSCFDictionary
setString:forKey:]: unrecognized selector sent to instance 0x7b7d170'
It appears i´m not changing the value correctly. Is it a syntax error? How is it done?
maybe you misspelled the name of the method.
// WRONG
[[itensArray objectAtIndex:i] setString:APROVED forKey:STATE];
// GOOD
[[itensArray objectAtIndex:i] setValue:APROVED forKey:STATE];
'NSInvalidArgumentException', reason: '-[__NSCFDictionary setString:forKey:]: unrecognized selector sent to instance 0x7b7d170'
NSMutableDictionary has no setString:forKey: method.
Try setObject:forKey:
[[itensArray objectAtIndex:i] setObject:APROVED forKey:STATE];

Resources