UISearchBar unrecognized selector sent to instance - ios

I've a .json file stored locally from which I'm loading in an array called countries as countries = (NSArray *)[NSJSONSerialization JSONObjectWithData [fileContents dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]; after loading the filepath ofcourse. I'm Loading the .json into countries array as:
NSString * filePath =[[NSBundle mainBundle] pathForResource:#"countries" ofType:#"json"];
NSError * error;
NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if(error||!filePath)
{
NSLog(#"Error reading file: %#",error.localizedDescription);
}
countries = (NSArray *)[NSJSONSerialization
JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
After that I've taken an NSMutableArraycalled displayItems and initialized it as displayItems = [[NSMutableArray alloc] initWithArray:countries];. Than in UITableView delegate methods I've used displayItems in count which after NSLog gives 248 means it's correctly getting the values cellForRowAtIndexPath and didSelectRowAtIndexPath See this image below for the hierarchy of the ViewController
THE PROBLEM:
Based on the break points, this method is creating issue.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:countries];
} else {
[displayItems removeAllObjects];
for (NSString *string in countries) {
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[displayItems addObject:string];
}
}
}
[tableView reloadData];
}
When it reaches the line Range r the app crashes and the error is unrecognized selector sent to instance. Everything is working great until I type a letter in the UISearchBar the app crashes at that time.
The Exact Error is:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x12ddd5e90'
*** First throw call stack:
(0x180a55900 0x1800c3f80 0x180a5c61c 0x180a595b8 0x18095d68c 0x1000d0e3c 0x185953d28 0x18577fe50 0x18577fdcc 0x185767a88 0x185953ad4 0x18578a8f8 0x18591fee8 0x18591f104 0x185aafae4 0x186095b20 0x18591ee70 0x185b5cc90 0x185b5c854 0x185772e84 0x181417e20 0x180a0cefc 0x180a0c990 0x180a0a690 0x180939680 0x181e48088 0x1857b0d90 0x1000e0e94 0x1804da8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

Related

App crash - when installed from App store but not locally

When i test my app as DEBUG then it works. But when i release to App store and from App Store install the app then it crash.
Therefore i changed the DEBUG RUN method into RELEASE run method for testing.
When i now run as RELEASE run method, then the app crash too (same as APP store Crash). The app crash is pointing at line 19.
How do i fix it? here i am telling to make an DNS (A or AAAA record) self.server = [self.server stringByAppendingString:#".example.com"]; by joining one string with the string i have from line 18.
- (void)myMethodHere:(NSString *)a {
//where a = #"splitme://test1/test2/test3";
NSArray *work_array = [a componentsSeparatedByString:#"splitme://"];
self.use_url =[work_array objectAtIndex:1];
if ([self.use_url length] > 0) {
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
self.server = [work_array objectAtIndex:0];
// -------------------
// CRASH HERE // CRASH HERE
// -------------------
if([self.server length] > 0) {
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 1: %#", self.server); // WORKS
// Here we go CRASHing...
self.server = [self.server stringByAppendingString:#".example.com"];
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 2: %#", self.server); // CRASH CRASH
}
else {
self.server = #"test.example.com";
}
}
else {
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 9: %#", self.use_url);
}
[self load_later];
}
EDIT 1: Following way of doing let me pass but if i have again self.server outside of this scope then app crash again.
NSString *part1 = self.server;
NSString *part2 = #".example.com";
NSString *result =[part1 stringByAppendingString:part2];
self.server = result;
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 2: %#", self.server);
EDIT 2: crashing on AppDelegate.m, after commenting out the NSLog lines.
2017-04-08 10:32:01.773648+0200 app[344:18271] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-04-08 10:32:01.774146+0200 app[344:18271] [MC] Reading from public effective user settings.
2017-04-08 10:32:01.778647+0200 app[344:18271] -[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
2017-04-08 10:32:01.778888+0200 app[344:18271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0'
*** First throw call stack:
(0x18a956fd8 0x1893b8538 0x18a95def4 0x18a95af4c 0x18a856d2c 0x100006f78 0x1000076e8 0x1000066cc 0x190a85f9c 0x190a85b78 0x190a8c3f8 0x190a89894 0x190afb2fc 0x190d038b4 0x190d092a8 0x190d1dde0 0x190d0653c 0x18c4ff884 0x18c4ff6f0 0x18c4ffaa0 0x18a905424 0x18a904d94 0x18a9029a0 0x18a832d94 0x190af045c 0x190aeb130 0x100007d78 0x18984159c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
EDIT 3: Crashing NSString not NSString even with explicit cast
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"!!!!!!!!!!!lol!!!!!!!!!!!!! Bug! Set breakpoint here!");
}
self.server = value;
}
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
NSString *setServerURL =(NSString *) [work_array objectAtIndex:0];
[self setServer:setServerURL];
EDIT 4: Crashing
- (void)myMethodHere:(NSString *)a {
NSArray *work_array = [a componentsSeparatedByString:#"test://"];
id objectOne = [work_array objectAtIndex:1];
if ([objectOne isKindOfClass:[NSString class] ] ) {
NSString *objectOneIntoString = objectOne;
self.use_url = (NSString *) objectOneIntoString;
}
if ([self.use_url length] > 0) {
NSLog(#"3: %#", self.use_url);
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
id objectTwo = [work_array objectAtIndex:0];
if ([objectTwo isKindOfClass:[NSString class] ]) {
NSString *objectTwoIntoString = objectTwo;
self.server = objectTwoIntoString;
}
if([self.server length] > 0) {
NSLog(#"4 - part 1: %#", self.server);
NSString *part1 = self.server;
NSString *part2 = #".example.com";
NSString *result = (NSString *) [part1 stringByAppendingString:part2];
self.server = result;
NSLog(#"4 - part 2: %#", self.server);
}
else {
self.server = #"dns.example.com";
NSLog(#"5:%#", self.server);
}
NSLog(#"8: %#", self.server);
}
else {
NSLog(#"9: %#", self.use_url);
}
NSLog(#"10: %#", self.use_url);
}
The error message in your pastebin is:
2017-04-08 10:32:01.778647+0200 app[344:18271] -[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
2017-04-08 10:32:01.778888+0200 app[344:18271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0'
This tells you that there was an array on which you called length. So if it really is related to your server property then you are accidentally storing an NSArray there somewhere.
Here's an idea on how to catch that: implement the setServer: method and check whether the type is correct.
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"Bug! Set breakpoint here!");
}
_server = value;
}
Edit: You implemented your setter incorrectly and would normally have produced an endless recursion. So let's start from scratch:
I assume you have a property defined like this:
#property (strong) NSString * server; // Or maybe "copy"
If your property does not look like this you need to tell us!
Then you implement the setter exactly like this:
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"Bug! Set breakpoint here!");
}
_server = value;
// DO NOT DO self.server = value HERE!
}
Then you can just do self.server = blabla; in the rest of your code and if your code sets something that is not a string the NSLog will be hit. So set a breakpoint there!
Edit 2:
Now I've seen in another comment of yours what's wrong…
#property (assign, nonatomic) NSString *server;
The assign does not retain the object! You need to use strong or (in the case if NSString *) copy. As it is, the following will happen:
You assign an object to server.
Since it is not retained, the object is deallocated soon.
But server still has a pointer to that memory location.
Two things can happen now:
Either a new, different object is created at memory location before you access the property. You now get an unexpected object back. Calling methods on this object may work or it may crash since the object does not know the method.
Or the memory location is deemed invalid and you get a crash right away.
Never use assign with objects!
-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
self.server class is NSArray .must string length

NSCFNumber escapedString Error with a NSDictionary

So I'm getting this error when creating a NSDictionary:
DLog(#"hiliteID: %# | regionID: %#", hiliteID, regionID);
if ([hiliteID isKindOfClass:[NSNumber class]]) {
DLog(#"hititeID is a number");
}
if ([hiliteID isKindOfClass:[NSString class]]) {
DLog(#"hiliteID is a string");
}
if ([regionID isKindOfClass:[NSNumber class]]) {
DLog(#"regionID is a number");
}
if ([regionID isKindOfClass:[NSString class]]) {
DLog(#"regionID is a string");
}
NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionID forKey:hiliteID];
DLog(#"regionDictionary: %#", regionDictionary);
id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:#[self.mapContext, regionDictionary]];
What is logged:
DEBUG | hiliteID: 160399 | regionID: 950
DEBUG | hititeID is a number
DEBUG | regionID is a number
DEBUG | regionDictionary: {
160399 = 950;
}
DEBUG -[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0
DEBUG *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'
0x7947f5d0 has a value of 160399 so it is hiliteID.
hiliteID is a returned value from our server and is set as a NSString. I cast it to a NSNumber:
NSArray *hiliteIDs = [result allKeys];
if ([[hiliteIDs firstObject] isKindOfClass:[NSString class]]) {
return [NSNumber numberWithInteger:[[hiliteIDs firstObject] integerValue]];
}
else if ([[hiliteIDs firstObject] isKindOfClass:[NSNumber class]]) {
return [hiliteIDs firstObject];
}
As far as I know, there is no issue with what I am doing here.
the line:
id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:#[self.mapContext, regionDictionary]];
I've used this class dozens of times in the code and never had an issue.
What can be causing the error?
reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'
Someone trying to call -escapedString method from NSNumber class, so it seems like the problem is that you passing NSNumber argument when NSString required. Try to use only NSString values inside your NSDictionary.
Whatever XMLRPCCall:withObjects: is calling an invalid method. I would recommend making all inputs into this method into NSStrings so that that call doesn't internally call methods on NSNumbers that NSNumber isn't capable of responding to.
NSString *regionIDString = [regionID stringValue];
NSString *hiliteIDString = [hiliteID stringValue];
NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionIDString forKey:hiliteIDString];

iOS app crashing with error: NSCFNumber stringByReplacingOccurrencesOfString:withString: unrecognized selector sent to instance

Our iOS app terminates with an error of [__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090.
We're testing on an iPhone 5 w iOS 7.
It crashes on this line: [self parseDictionary:notificationMessage intoJSON:jsonStr].
Method containing this line:
- (void)notificationReceived {
NSLog(#"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:#"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:#"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:#"foreground:\"%d\"", 0];
[jsonStr appendString:#"}"];
NSLog(#"Msg: %#", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:#"%#(%#);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
self.notificationMessage = nil;
}
}
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
{
NSArray *keys = [inDictionary allKeys];
NSString *key;
for (key in keys)
{
id thisObject = [inDictionary objectForKey:key];
if ([thisObject isKindOfClass:[NSDictionary class]])
[self parseDictionary:thisObject intoJSON:jsonString];
else
[jsonString appendFormat:#"\"%#\":\"%#\",",
key,
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
}
}
Stack trace (along with one debugging line to show the variable value):
2014-01-07 16:32:36.980 Wopple[195:60b] Notification received
Printing description of self->notificationMessage:
{
"_" = "gHf8EeO3_ZDiugJkgA";
aps = {
alert = "hi foo bar right";
badge = 3;
};
}
2014-01-07 16:33:22.774 Wopple[195:60b] -[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
2014-01-07 16:33:22.776 Wopple[195:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090'
*** First throw call stack:
(0x308c3e83 0x3ac246c7 0x308c77b7 0x308c60af 0x30814dc8 0x88da5 0x88d1f 0x88915 0x8757b 0x3335ec93 0x3335f75d 0x340d0b37 0x3088e777 0x3088e713 0x3088cedf 0x307f7471 0x307f7253 0x3552b2eb 0x330ac845 0x7f5d3 0x3b11dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
When you're using:
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
You're assuming that objectForKey is going to return an NSString object. But, in the case where your app is crashing, the object returned is actually an NSNumber.
You should use isKindOfClass: to determine the object type, or use stringValue on your NSNumber to get a string representation of the object.

iOS app crash when trying to log NSDictionary

I have a problem where I try to log NSDictionary content and as soon as the method is called the app crashes.
This is the code I have tried after advice from a talented "hacker":
%hook UserData
-(int)getVariable:(NSDictionary *)fp8 {
for (NSString *key in [fp8 allKeys]) {
%log(#"key: %#, value: %# \n", key, [fp8 objectForKey:key]);
}
return %orig;
}
%end
also tried:
%hook UserData
-(int)getVariable {
int originalValue = %orig;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSError *error;
[[fp8 description] writeToFile:[NSString stringWithFormat:#"%#/lol_%d.txt",basePath,fp8.count] atomically:NO encoding:NSUTF8StringEncoding error:&error];
return %orig;
}
%end
Both ways result in a crash of the app. This is on a iphone 4 with ios 6.1.3 tethered JB.
DoD EN[1000]: -[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88
DoD EN[1000]: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88'
*** First throw call stack:
Does anyone have a suggestion on what may be wrong here?
The crash log provide quite an explanation
-[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88
I guess this is the crash log associated with the first piece of code you posted :
%hook UserData
-(int)getVariable:(NSDictionary *)fp8 {
for (NSString *key in [fp8 allKeys]) {
%log(#"key: %#, value: %# \n", key, [fp8 objectForKey:key]);
}
return %orig;
}
%end
It crashes at the execution of [fp8 allKeys], claiming that allKeys is not a valid selector for objects of class __NSCFConstantString
What it tells you is that fp8 is not an NSDictionary* but rather a __NSCFConstantString* (that is, a pointer to a constant instance of NSString such as one defined like NSString* foo = #"bar").
If that is true, then the second code you posted would crash also because count is not a valid selector for class NSString.
Why don't you just try the following, see what it gives you :
%hook UserData
-(int)getVariable:(id)fp8 {
%log(#"fp8: %# : %#\n", NSStringFromClass([fp8 class]), [fp8 description]);
return %orig;
}
%end
If you print the description it should work. Following Way :
NSLog(#"%#", [dictionary description]);

Get Row from NSArray

Hello i get a json that looks like this:
features: (
{
attributes = {
Gecontroleerd = Ja;
};
geometry = {
x = "5.968097965285907";
y = "52.50707112779077";
};
}
)
From this code:
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSArray *row in data) {
NSString *latitude = row[5];
NSString *longitude = row[7];
NSString *crimeDescription = #"test";
NSString *address = #"banaan";
And u need to x values 5.968097965285907 for latitude
and y values 52.50707112779077 for longitude
But i get this error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450
2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
*** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone wich row i need to select?
I guess that the only thing is that the row number needs to be changed. Or maybe there should be something like this : [1][5]. Im not quite sure how this works
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSDictionary *dic in data) {
NSDictionary geometry = [dic objectForKey:#"geometry"];
// Do what you want..
NSString *myAwesomeX = [geometry objectForKey:#"x"];
NSString *myAwesomeY = [geometry objectForKey:#"y"];
}
The problem here is that you are trying to send a selector message to object row, that is in memory a NSDictionary (NSCFDictionary?) object, and you are trying to manage it like a NSArray.
The method objectAtIndexedSubscript (is underlying called by row[5] and row[7]) exists in NSDictionary, but no in NSArray.
Change
for (NSArray *row in data) {
by
for (NSDictionary *row in data) {
Also, you have to change the management of data inside for, look at the result of your log statement and act accord whit it.
I hope this will help!

Resources