Iteration of NSMutableArray of UITextView causes app to crash - iphone

I want to iterate Uitextview NsmutableArray but the program crashes and it gives the following error,
"""-[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0"""
Please check this code , and give some possible solution...
Thank You...
for ( x = 0; x < TextViewArray.count; x++)
{
// here TextViewArray is a NSMutableArray
UITextView *textField1 = [TextViewArray objectAtIndex:x];
float diff3;
diff3 = [textField1.text floatValue]; // The program crashes in this line ,"""""" -[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0""""" , here I am checking for a float value but i want to use string value instead of float value in this line how to do this?
NSLog(#"diff3 is %f",diff3);
textField1.text = [[NSString alloc] initWithFormat:#"%.2f",diff3];
TXT_First_Tag = [TextViewArray objectAtIndex:x]; // here uitextview *TXT_First_Tag;
NSLog(#"txt3 is %d",x);
TXT_First_Tag.tag = x;
NSLog(#" p is %d", x);
//textField1.text = [[NSString alloc] initWithFormat:#"%.2f",diff2];
//NSLog(#"Diff is %f",diff2);
}

Your array contains a String at the given index, hence the error """-[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0""". Check that the Array only contains UITextViews

put a breakpoint before the crash and do a "po" in the gdb shell once control reaches breakpoint. This way you will no if you are trying to access right object or not

Related

unrecognized selector sent to instance

I'm attempting to initialize an array and add a chapter object to an array of book objects, but it crashes with the error:
2012-07-25 21:41:01.503 Project1[2364:f803] -[__NSCFString
arrayOfChapters]: unrecognized selector sent to instance 0x6ad80b0
2012-07-25 21:41:01.505 Project1[2364:f803] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString arrayOfChapters]: unrecognized selector sent to
instance 0x6ad80b0'
My code:
Chapter *myChapter = [[Chapter alloc]init];
myChapter.pageCount = self.TextField2.text;
myChapter.chapterTitle = self.TextField1.text;
if(!currentBook.arrayOfChapters)
{
currentBook.arrayOfChapters = [[NSMutableArray alloc]init];
}
currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
[currentBook.arrayOfChapters addObject:myChapter];
I think the code is correct, is there something set up wrong with my project? I believe it's the initialization which is causing the actual crash, but there isn't anything non-standard there.
you can make a breakpoint on the line "if(!currentBook.arrayOfChapters)" to check whether the currentBook is nil;
make a breakpoint on the line"currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
" to check whether the segControl.selectedSegmentIndex >= [books count]
It looks like currentBook is a string or not initialized.
I may be misreading this but...
if(!currentBook.arrayOfChapters)
{
// creating array for current book
currentBook.arrayOfChapters = [[NSMutableArray alloc]init];
}
// reassigning a new book to current book. Are you sure this new one has array of chapters?
currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
// trying to access array of chapters
[currentBook.arrayOfChapters addObject:myChapter];
Shouldn't you be assigning first and then creating array if it's not already there?

NSNumber causing a SIGABRT

I have a strange problem (strange if you ask me). Im using a NSNumber object to store a number (doh). When I try to "modify" it it crashes my application. The code I'm using looks like this:
if ([frequency intValue] > 19999)
return;
frequency = [NSNumber numberWithInt:([frequency intValue] + 1)]; //I think this line is causing me the problem
[freqLabel setText:[NSString stringWithFormat:#"%i Hz", [frequency intValue]]];
Where is frequency the NSNumber and freqLabel my label to which I write the value every time this gets called.
Why is this incorrect? It works when I call it for the first time. Is it that NSNumber numberWithInt always returns a new object which I'm trying to assign to frequency?
How do I fix this? Whats the correct way to change a NSNumber's value?
Sorry for my bad english (if there are any mistakes).
EDIT:
The error log looks like this:
[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0
2012-05-09 16:39:28.064 MyApp[31939:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0'
*** First throw call stack:
(0x17a6022 0x28afcd6 0x17a7cbd 0x170ced0 0x170ccb2 0x4821 0x17a7e99 0x49d14e 0x49d0e6 0x543ade 0x543fa7 0x543266 0x4c23c0 0x4c25e6 0x4a8dc4 0x49c634 0x2e49ef5 0x177a195 0x16deff2 0x16dd8da 0x16dcd84 0x16dcc9b 0x2e487d8 0x2e4888a 0x49a626 0x1cca 0x1c15)
terminate called throwing an exception
But it doesn't always show this error. Sometimes it causes an EXC_BAD_ACCESS. Should I store my variable in a temporary NSNumber?
You can't just alloc/init something once and then you have a lifelong reference to that type. When you assign frequency to numberWithInt, then you are overwriting the previous alloc/init value with an autorelease value (which will be released later and cause the exact behavior you are describing). The reason it works with self.frequency is because your property is set as a retain property, so it automatically retains the autorelease value. Add a retain to your numberWithInt line and it will be fine (or do what you are doing now with self.frequency).
I would try doing this instead
int myNumber = ([frequency intValue] + 1);
frequency = [NSNumber numberWithInt:myNumber];
Yes, numberWithInt: does indeed return a new object. You're probably not retaining this object. Just properly retain frequency when assigning it a new NSNumber. Without context, I'm not sure the best way to accomplish this, but one way is to make frequency a property of your object and using the accessor method.
It would seem that you've already initialized and assigned some value prior to NSNumber, like you have it inside an array for example.
Basically NSNumber objects are immutable, so changing their value is not possible anyway.
You can do it this workaround if you use it inside an array:
NSMutableArray *myOldArray = [[NSMutableArray alloc] init];
myOldInt = 3;
myOldArray[4] = [NSNumber numberWithInt:myOldInt]; // for example
NSMutableArray *myNewArray = [[NSMutableArray alloc] init];
myInt = myOldInt+2;
NSMutableArray *row = [NSMutableArray arrayWithObjects:myOldArray[1],myOldArray[2],myOldArray[3],[NSNumber numberWithInt:myInt],[NSNumber numberWithInt:myInt2],nil];
[myNewArray addObject:row];

Loop through UITextField's and store double values

I'm having trouble looping through a set of dynamically created UITextFields and storing those values as a double to be added to an array later on. I'm still pretty new to obj-c programming so bear with me if this question seems trivial. Thanks. This is what I have so far.
NSMutableArray *textFieldCashArray = [[NSMutableArray alloc] init];
double textFieldCash;
for (int y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
P.S and here is the error message I'm getting
Pro[962:b303] -[UIControl text]: unrecognized selector sent to instance 0x680f850
2012-04-01 16:05:46.305 iFinance Pro[962:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControl text]: unrecognized selector sent to instance 0x680f850'
*** Call stack at first throw:
I think what's going on here is that the loop variable is being used as the tag, and the loop starts at zero. viewWithTag will answer either the receiver or one of it's subviews with a given tag, so if the view controller's main view has tag==0 (which it probably does), your first text request is being sent to that top-level view.
Try setting the text field tags to some non-zero value, starting at SOME_OFFSET. Then in your loop:
for (y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y+SOME_OFFSET];
// ...
}
danh is most certainly right about the cause and solution to this problem. Just to add a little, cases like this can be somewhat avoided by checking the Class before casting.
if ([[self.view viewWithTag:y] isKindOfClass:[UITextField class]]) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
You should create array at first:
NSMutableArray *textFieldCashArray = [NSMutableArray array];
Edit
Your error log shows that you are receiving some another UIControl object (UIBtton for example) instead of UITextField. Check your tags on xib (or algorith if you set programmatically) and be sure that UITextField objects has corresponding tags

Problem with NSOrderedSet addObject when unarchiving from file

I have been searching everywhere on the web and trying everything that I can think of so far to no avail. Here is my problem:
In my program I have a class called Scale, Scale has several properties. The user can add scales to a tableView by clicking a button called addScale. When addScale is pressed, I add it to a OrderedMutableSet. I use the set to store the scales and prevent duplicate scales. I had to override the isEqual method and the Hash method to fool the computer into thinking that two Scales with the same properties are exactly the same. This works fine. But when I open the app and have had previously some scales in the tableView, I have archiving implemented so these scales reappear, as expected. This time, when addScale is pressed, I get an exception:
-[__NSOrderedSetI addObject:]: unrecognized selector sent to instance 0x746dbf0
2011-08-29 13:20:49.095 MusicLog[678:f203]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSOrderedSetI addObject:]: unrecognized selector sent to instance 0x746dbf0'
Here is the code for my isEqual method and my Hash method:
(tonic, mode, rhythm, octaves, and tempo are all ints)
- (NSUInteger)hash
{
NSUInteger prime = 31;
NSUInteger result = 1;
result = prime * result + tonic;
result = prime * result + mode;
result = prime * result + rhythm;
result = prime * result + octaves;
result = prime * result + tempo;
NSLog(#"%u", result);
NSLog(#"%u", NSUIntegerMax);
return result;
}
- (BOOL)isEqual:(Scale *)object
{
if ((tonic == [object tonic])
&& (mode == [object mode])
&& (rhythm == [object rhythm])
&& (octaves == [object octaves])
&& (tempo == [object tempo]))
return YES;
else
return NO;
}
Here is the code for my addScale method:
- (IBAction)addScale:(id)sender
{
Scale *chosenScale = [[Scale alloc] init];
[chosenScale setTonic:[myPicker selectedRowInComponent:0]];
[chosenScale setMode:[myPicker selectedRowInComponent:1]];
[chosenScale setRhythm:[myPicker selectedRowInComponent:2]];
[chosenScale setOctaves:[octavesSegmentedControl selectedSegmentIndex] + 1];
[chosenScale setTempo: (int) [tempoStepper value]];
[[ScaleStore defaultStore] addScale:chosenScale];
}
the addScale method of the ScaleStore just calls addObject from a NSOrderedMutableSet which is where all the scales are stored.
Any help you can give is greatly appreciated.
Thank you,
K
When you unarchive, the objects are restored to an immutable version (NSMutableSet becomes NSSet, NSMutableArray becomes an NSArray, etc.)
Just create a new NSOrderedMutableSet with objects from the restored set.

error “EXC_BAD_ACCESS” in my program

i have a problem.
When i compile my program there isn't a error but, when i start it these retern with “EXC_BAD_ACCESS”.
I looking for the error with the debug and i find it in these method but i don't understand where...
PS:the program enters in the loop sometimes.
-(void)updateMinPosition{
float valueMinX = 150;
float valueMinY = 150;
float valueMinZ = 150;
NSString *nameMinimoX = [NSString stringWithFormat:#"default"];
NSString *nameMinimoY = [NSString stringWithFormat:#"default"];
NSString *nameMinimoZ = [NSString stringWithFormat:#"default"];
for(int i = 0; i< [arrayPosizioniMovimento count]; i++){
//Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
if(tempPosition.valueX <= valueMinX){
valueMinX = tempPosition.valueX;
nameMinimoX = tempPosition.nome;
}
if(tempPosition.valueY <= valueMinY){
valueMinY = tempPosition.valueY;
nameMinimoY = tempPosition.nome;
}
if(tempPosition.valueZ <= valueMinZ){
valueMinZ = tempPosition.valueZ;
nameMinimoZ = tempPosition.nome;
}
[tempPosition dealloc];
}
labelMinX.text = nameMinimoX;
labelMinY.text = nameMinimoY;
labelMinZ.text = nameMinimoZ;
}
For the 1st glance there're several problems with your code:
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
Here in the second line you create new Posizione instance and right after that assign another value to the same variable. In effect that will mean that your created instance will be never used and cause memory leak. To use element from array just write
Posizione *tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
The second one - is the following line
[tempPosition dealloc];
First of all you should never call this method directly but rather send an object -release message - it will be deallocated automatically when its retain count becomes 0. In your case you do not retain tempPosition object in that code so there;s no need to release it here - just remove that line.
P.S. Using fast enumeration can also make your code more readable and less error prone:
for (Posizione *tempPosition in arrayPosizioniMovimento){
if(tempPosition.valueX <= valueMinX){
...
It looks like it may be the [tempPosition dealloc] call. You're declaring that variable and doing an alloc/init on it but then just assigning it to the object within the array, so the alloc/init is unneeded. When you make the call to dealloc at the bottom you're releasing that object that resides in the array, so your array will now have a null value which will cause the EXEC.... error
You can enable Objective-C Exception breakpoints to pinpoint the line.
On thing that jumps out is
[tempPosition dealloc];
I'm not sure what to tell you to do. You need to call release not dealloc.
[tempPosition release];
But im not sure how that fits into the rest of your code, where you are allocating a variable then immediately assigning it another value.
I think you should just remove the alloc and dealloc
so delete:
//Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
and
[tempPosition dealloc];
Pease show how do you initialize arrayPosizioniMovimento.
If you initialized it like arrayPosizioniMovimento = [NSMutableArray arrayWithCapacity:n]; than you'll need to add [arrayPosizioniMovimento retain];