I creating a game in Cocos2D 2, but my issue is with NSArray/NSMutableArray.
A class called SocketManager that manages the socket class which is derived from CCSprite. Socket only has extra member variable called row which is an int. The SocketManager keeps an array with all the Sockets sprites that are within the game.
In the game class I have this code to add the sockets to the manager:
for (int i = 1; i <= numRows; i++) {
for (int j = 1; j <= i; j++) {
Socket *socket =[Socket spriteWithFile:#"Tile_Socket.png"];
socket.row = i;
socket.anchorPoint = ccp(0.5, 0.5);
/* Shortend to cut out useless code */
[socketsManager addSocket:socket];
[self addChild:socket];
}
}
This is the SocketsManger add function:
-(void)addSocket:(CCSprite *)socket {
[sockets addObject:sockets];
}
Later on in the game in the update function it checks if the row wants to be changed and calls the SocketManager's function called :
-(NSArray *)searchSocketByRow:(int)row; {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (Socket *socket in sockets)
CCLOG(#"Row: %i", socket.row);
// if ([socket row] == row)
// [array addObject:socket];
// CCLOG(#"Found %i sockets.", [array count]);
// return [NSArray arrayWithArray:[array mutableCopy]];
}
When I try to call socket.row or [socket row] I get the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM row]: unrecognized selector sent to instance
This method looks wrong try to change it to:
-(void)addSocket:(CCSprite *)socket {
[sockets addObject:socket];
}
You try to add array to array but you want to add CCSprite object.
It should help.
You have an extra S in the mix. In the add socket function change this
[sockets addObject:sockets];
to
[sockets addObject:socket];
Try
[sockets addObject:socket];
without s at the object you want to add
Related
Hi I'm getting following exception when I try to sum up the values of a response array from both the method mentioned below.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]: unrecognized selector sent to instance 0x1c463ade0'
Tried code below
-(void)getSelectedServiceTypeArray:(NSArray *)selectedServicesTypeArray
{
NSInteger sum = 0;
selectedMultiServiceType = [[NSArray alloc]initWithObjects:selectedServicesTypeArray, nil];
NSArray *priceArray = [selectedMultiServiceType valueForKey:#"price_per_unit"];
NSLog (#"PriceArray%#",priceArray);
// sum = [priceArray valueForKeyPath:#"#sum.self"];
for (NSNumber *num in priceArray)
{
sum += [num intValue];
NSLog(#"SUMR1%ld",(long)sum);
}
NSLog(#"SSTAA%#",selectedMultiServiceType);
}
Mean while I tried both the method for an array contains manually entered values. Anyway Both methods works for this..
NSArray *testArray = #[#"24",#"75",#"80",#"44"];
NSNumber *sum = [testArray valueForKeyPath:#"#sum.self"];
NSLog(#"SUM SUM1%#",sum);
double sum2 = 0;
for (NSNumber *serviceFeeTotal in testArray)
{
sum2 += [serviceFeeTotal doubleValue];
}
NSLog(#"SUM SUM2%f",sum2);
I don't why this not working and app is crashing on either of both methods when I using an array that retrieved from a previous ViewController by delegate method.
Response of the array I tried to sum up
PriceArray(
(
30,
45
)
)
Please explain me with a proper code...
Thanks for the All the support I solved the issue by replacing following code
selectedMultiServiceType = [[NSArray alloc]initWithObjects:selectedServicesTypeArray, nil];
**NSArray *priceArray = [selectedMultiServiceType[0] valueForKey:#"price_per_unit"];**
NSLog (#"PriceArray%#",priceArray);
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];
I am sitting since a hour on some strange exception.
I try to call some method with:
for (int i = 0; i < [[DBElements objectAtIndex:index] count]; i++) {
NSLog(#"selected Element: %#", [[DBElements objectAtIndex:index] objectAtIndex:i]);
[self addElementsToView:dash withString:[[DBElements objectAtIndex:index] objectAtIndex:i] index:index andSubindex:i];
}
the method is of types: - (void) addElementsToView: (UIView *) dash withString: (NSString *) type index:(NSInteger)index andSubindex : (int) i {}
NSLog shows me:
selected Element: NUMBER
so the index stuff is ok.
Why I get on the next step the following exception:
[__NSArrayI intValue]: unrecognized selector sent to instance
0x14d44f70 * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]:
unrecognized selector sent to instance 0x14d44f70'
UPDATE: The DBElements is:
(
(
NUMBER,
LABEL
),
LABEL
)
index is 0
i is 0. so it shows to NUMBERS of type NSString.
Try to cast
withString:[[DBElements objectAtIndex:index] objectAtIndex:i]
like this:
withString:(NSString *)[[DBElements objectAtIndex:index] objectAtIndex:i]
Hope it helps!
Somewhere in your code you are calling intValue on a NSArray. Maybe in the addElementsToView method.
Looking at the (short) code sample I assume it has something to do with DBElements. You probably want to call intValue on a NSString to convert it to number value.
If you have an Arrays nested in Arrays then make sure you're calling the right selectors on the right objects.
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];
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];