performSelector afterDelay with multiple arguments error - ios

I´m having some problems with performSelector method on this code:
This method are in other class called "JSONMethods":
+(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs {
[self sendPostMsg:[myArgs objectAtIndex:0]:[myArgs objectAtIndex:1]];
}
Then, on another class I have the call:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Loading...";
JSONMethods *methods = [[JSONMethods alloc]init];
NSArray *arguments = [[NSMutableArray alloc]initWithObjects:#"http://localhost/promos/txFirmas.php",[NSString stringWithFormat:#"sensor=%d",tableViewNumber], nil];
[methods performSelector:#selector(sendPostMsgWithMultipleArguments:)
withObject:arguments
afterDelay:3.0];
NSString *tit = [NSString stringWithFormat:#"Sign: %d",tableViewNumber];
self.title = tit;
}
I would like wait until the "sendPostMsgWithMultipleArguments:" finish for change the title, how I can make it?
When I test this, mi app crash and show me this by console:
2012-08-17 12:09:15.966 MapaProject[524:11603] -[JSONMethods sendPostMsgWithMultipleArguments:]: unrecognized selector sent to instance 0x7c85b70
2012-08-17 12:09:15.968 MapaProject[524:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JSONMethods sendPostMsgWithMultipleArguments:]: unrecognized selector sent to instance 0x7c85b70'
*** First throw call stack:
(0x133a022 0x2016cd6 0x133bcbd 0x12a0ed0 0x12a0cb2 0xa1c85d 0x130e936 0x130e3d7 0x1271790 0x1270d84 0x1270c9b 0x15db7d8 0x15db88a 0xf6626 0x236d 0x20c5)
terminate called throwing an exception
Can you help me?
Thanks in advance :)

Change +(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs
to -(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs
or even better change
[methods performSelector:#selector(sendPostMsgWithMultipleArguments:) withObject:arguments afterDelay:3.0];
to [[methods class] performSelector:#selector(sendPostMsgWithMultipleArguments:)
withObject:arguments
afterDelay:3.0];

NSArray * mutArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:2], nil]
[self performSelector:#selector(loadMore:) withObject:mutArray afterDelay:1.9];
-(void)loadMore:(NSArray *)array
{
NSNumber * myNumber = [array objectAtIndex:0];
NSNumber * myNumber2 = [array objectAtIndex:1];
}

In swift 3
You can make dictionary to save multiple arguments and pass it with the selector
let argumentsDict = ["arg1Key": arg1Value, "arg2Key": arg2Value] as [String : Any]
perform(#selector(selectorWithMultipleArg), with: argumentsDict, afterDelay: 3.0)
And inside the selector function, take out values using guard and use them. I am using 2 different type arguments to explain better .i.e. Int and String
func selectorWithMultipleArg(_ arguments: [String: Any]) {
guard let arg1Value = arguments["arg1Key"] as? Int else {
fatalError("Invalid value 1")
}
guard let arg2Value = arguments["arg2Key"] as? String else {
fatalError("Invalid value 2")
}
print("Value 1: \(arg1Value)")
print("Value 2: \(arg2Value)")
}
I hope this would help you

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

NSArrayM unrecognized selector error

I need some help understanding why I am getting an unrecognized selector error
I have a NSMutableDictionary
#interface CallDetailViewController () {
NSMutableDictionary * thisDictionary;
}
#end
That gets populated from the MasterTabController
- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
Outputting a description shows the data is there
Check dictinoary: (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
} )
But when I try to access an object in it:
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
I get the following error.
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7a73f9a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]:
unrecognized selector sent to instance 0x7a73f9a0'
The array is populated from a JSON value that returns an array of records called Record. After the JSON is returned, I pull out just section that contains the record.
NSDictionary * dictTowx = [NSDictionary dictionaryWithDictionary:[dictJSON objectForKey:#"ThisResponse"]];
NSDictionary * dictData = [NSDictionary dictionaryWithDictionary:[dictTowx objectForKey:#"Data"]];
NSArray * arrRecordSet = [dictData objectForKey:#"Recordset"];
NSDictionary * dicRecord= [arrRecordSet objectAtIndex:0];
self.detailDictionary = [dicRecord objectForKey:#"Record"];
The RAW JSON:
{
ThisResponse = {
Data = {
Recordset = (
{
Record = (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
}
);
}
);
};
};
}
What am I doing wrong?
Please try to assign like this
self.detailDictionary = [[dicRecord objectForKey:#"Record"] objectAtIndex:0];
and then access like this
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
Enjoy programming.
- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
try replacing
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
with
thisDictionary = [[NSMutableDictionary alloc]initWithDictionary: [[{MasterTabController *)self.tabBarController detailDictionary] mutableCopy]]];
i think your problem may be you are not instantiating the dictionary before setting it to objects from another.

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];

NSNull stringByReplacingOccurrencesOfString:withString: issue with a JSON

i've a problem with parsing a JSON:
in mycallback method i copy object for key "a" in a NSDictionary *test as follow:
[[test sharedManager] MyTestCallback:^(MyTestSessionStatus status, NSDictionary * t, NSError * error){
test = [t objectForKey:#"a"];
}
then in an other method i copy in an array the value
NSArray *temp = [discoveredMedia valueForKeyPath:#"b"];
and then when i try to do
NSString *temp2 = (NSString *)[temp objectAtIndex:0];
but value is -> Printing description of temp2: < null >
and in debugger -> temp2 NSString * class name = NSNull 0x383ed3d0
it crushed when
temp2 = (NSString *)[temp2 stringByReplacingOccurrencesOfString:#"<$size$" withString:#"350"];
in console:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x383ed3d0'
what's going on?
Thanks
In JSON you have 'null' in dictionary and it will return in objective c not nil but NSNull.
You need to treat NSNull as nil.
I have made a simple NSNull workaround, to avoid crashes on NSNull calls
1) Add this code in your AppDelegate
#import "EXTNil.h"
#import <objc/runtime.h>
+ (id)swizzled_null
{
return [EXTNil null];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SEL originalSelector = #selector(null);
SEL swizzledSelector = #selector(swizzled_null);
Method originalMethod = class_getClassMethod([NSNull class], originalSelector);
Method swizzledMethod = class_getClassMethod([self class], swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
return YES;
}
2) Added (EXTNil and EXTRuntimeExtensions) classes to your project from repo https://github.com/jspahrsummers/libextobjc/tree/master/extobjc
Now You can call any method on NSNull

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