iOS: replace an object inside an array of array - objective-c

My code is:
[[arrayOne objectAtIndex:indexSelected] replaceObjectAtIndex:1 withObject:new];
NSLog(#"indexSelected:%d", indexSelected); // = 0
NSLog(#"new:%#", new); // = 26
indexSelected is an int and new is a string
When I try to do this I have an exception that says: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x1cee60'
Why?

This is because NSArray is immutable. Use an NSMutableArray.

Related

return Dictonary from Objective c file to Swift?

I am working with open cv and swift. While returning NSDictionary from Objective-C file to Swift getting an error.
2018-10-10 12:43:25.972927+0530 OPencvwithSwift[2430:618249] -[__NSFrozenDictionaryM length]: unrecognized selector sent to instance 0x1c022b6e0
2018-10-10 12:43:25.973704+0530 OPencvwithSwift[2430:618249] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSFrozenDictionaryM length]: unrecognized selector sent to instance 0x1c022b6e0'
*** First throw call stack:
(0x183eb6d8c 0x1830705ec 0x183ec4098 0x183ebc5c8 0x183da241c 0x1053c23d4 0x105360aa0 0x10585294c 0x104aec9a0 0x104ae287c 0x105cd51dc 0x105cd519c 0x105cd9d2c 0x183e5f070 0x183e5cbc8 0x183d7cda8 0x185d62020 0x18dd9c758 0x104af984c 0x18380dfc0)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here is Objective c code -
- (NSDictionary *)predict:(UIImage*)img confidence:(double)confidence {
cv::Mat src = [img cvMatRepresentationGray];
int label;
NSLog(#"%d",label);
std::cout<<_labelsDictionary;
self->_faceClassifier->predict(src, label, confidence);
NSLog(#"%f",confidence);
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
[dict setObject:[NSNumber numberWithInt:confidence] forKey:_labelsDictionary[#(label)]];
NSLog(#"%#",dict);
return dict;
}
Calling this function from Swift:
let result = facemodel?.predict(greyimage, confidence: confidence) // crash on this line
Your application crash while executing Objective-C method probably. Use break point inside Objective-C method and continue step-by-step so you will find your crash.
There is no way to crash assign pretict method's return dict to result variable. It gives [AnyHashable: Any] to result, so you will have a [AnyHashable: Any] type result object.
If I understand you correctly, before you use the dictionary,In other places it has been pointed to a string. so crash

NSString converting to NSCFString

I have created a category of NSString Class , and i am trying to do some custom NSString operation .
My NSString object is converting to NSCFString and its crashing at calling custom NSString method.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringByDeletingLastAndAddingFirstSlash]: unrecognized selector sent to instance 0xa6015e0'

IntValue of an object from array causing errors

I am having a slight code problem. I have an array but when I pull data out of it, I need it in the form of an int. To do this, I tried to use:
int main_id = [[courses valueForKey:#"id"] intValue];
but this gives me the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]: unrecognized selector sent to instance 0x6b438d0'
*** First throw call stack:
(0x16ac022 0x183dcd6 0x16adcbd 0x1612ed0 0x1612cb2 0x4bf8 0x4fdd 0xdea1e 0xddfec 0x104f1d 0xef1cb 0x105df1 0x105e0d 0x105ea9 0x446f5 0x4473c 0x15596 0x16274 0x25183 0x25c38 0x19634 0x1596ef5 0x1680195 0x15e4ff2 0x15e38da 0x15e2d84 0x15e2c9b 0x15c65 0x17626 0x36ed 0x2695 0x1)
terminate called throwing an exception
I have found the answer. I was using an NSArray
NSArray *array;
That had another nsarray in it. I was trying to call the intvalue of a NSArray.

[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830'

so i am getting an uncaught exception and im not sure why. this is the error i get:
2010-08-22 10:39:25.080 MayanGlyphs[10903:207] * -[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830
2010-08-22 10:39:25.081 MayanGlyphs[10903:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830'
also i am getting a warning that UILabel may not respond to setAnswerText (answerText is the name of the label) even though i have created the outlet, connected it in IB, and synthesized it.
does anyone know why this might be happening?
answerText is the name of the label? then what are you trying to do?
If you want to access the answerText object (that being an instance of UILabel) you need to use self.answerText not [UILabel setAnswerText];
If you want to change the text in the label use:
answerText.text = #"Hello, this is my text";
or similar.
You need to do [setAnswerText setText:#"my text"]; What you are doing now is wrong. The reason you get that exception is that UILabel does not have a class method called setAnswerText:.

When I am replacing or inserting an object into nsmutable array, I am getting an Exception

While replacing or inserting into an NSMutable array, I am getting exception as:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
[list replaceObjectAtIndex:indexRow withObject:editcontacts];
//or
[list insertObject:editcontacts atIndex:indexRow];
You are still using an NSArray instead of an NSMutableArray. You need to allocate list as such:
NSMutableArray *list = [[NSMutableArray alloc] init];
See this question