Extracting Data from NSMuableDictionary to NSString and compering to String issue - iphone

Im Extracting Data from NSMuableDictionary to NSString and try compering to String like this:
NSDictionary *error = [[NSDictionary alloc]init];
NSString *errorCode = [[NSString alloc]init];
error = [sing.globalCallsDitionary valueForKey:#"Error"];
NSLog(#"error code %#",[error valueForKey:#"error_code"]);
errorCode = [error valueForKey:#"error_code"];
if ([errorCode isEqualToString:#"-1"]) {
When the if statement executed i get this error talking about an array, the error looks like this:
2012-04-27 06:34:49.686 CallBiz[10319:707] error code (
"-1"
)
2012-04-27 06:35:00.602 CallBiz[10319:707] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10
2012-04-27 06:35:00.608 CallBiz[10319:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10'
*** First throw call stack:
(0x3541f88f 0x36846259 0x35422a9b 0x35421915 0x3537c650 0xb2a3 0x353793fd 0x32458faf 0x32458f6b 0x32458f49 0x32458cb9 0x324595f1 0x32457ad3 0x324574c1 0x3243d83d 0x3243d0e3 0x3667222b 0x353f3523 0x353f34c5 0x353f2313 0x353754a5 0x3537536d 0x36671439 0x3246be7d 0x2e61 0x28fc)
terminate called throwing an exception
It is as xCode is looking on my NSString *errorCode = [[NSString alloc]init]; as if it is an NSArray, can someone help me with this?

Error occurs because your errorCode came as a array, instead of string. If you are sure that only 1 object come here, then you can use -
if ([[errorCode objectAtIndex:0] isEqualToString:#"-1"])
It will work, although it will through a warning, because errorCode is defined as a string object. So you ned to sure what data you are getting in response and then define data structure appropriately.

Related

How to remove bytes from the end of NSMutableData object

I have a NSMutableData object that is giving me some trouble, I am trying to remove the last 6 bytes from the object like this
NSMutableData *reducedDataPacket = [[NSMutableData alloc] init];
reducedDataPacket = [myCompressedData copy];
NSRange range = NSMakeRange([reducedDataPacket length]-6, 6);
[reducedDataPacket replaceBytesInRange:range withBytes:NULL length:0];
However once the last line executes my app crashes and I am left with this error below.
-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
I have never tried doing this before and have been going off other answeres supplied I have investigated, but I just cannot get this to work... any help would be greatly appreciated.
Your first line is useless because you then redefine reducedDataPacket in the second line, so that first line should be deleted. I'm guessing that myCompressedData is NSData rather than NSMutableData, so change that second line to :
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
First you need a mutable instance, it isn't clear why you create one and then copy it. You should just do:
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
Then you want to reduce the length, not try to fill part of the data with nothing:
[reducedDataPacket setLength:(reducedDataPacket.length - 6)];

Issue with Calling Function

I have a method as follows use to correct empty values in Json
+(NSString *)CorrectJsonForEmptyValues:(NSString *)pasRawJson
{
NSLog(#"CorrectJsonForEmptyValues");
NSMutableString *tmpJson = [pasRawJson mutableCopy];
[tmpJson replaceOccurrencesOfString:#"[,"
withString:#"[{\"v\": \"N/A\",\"f\":\"N/A\"},"
options:0
range:NSMakeRange(0, [tmpJson length])];
[tmpJson replaceOccurrencesOfString:#",,"
withString:#",{\"v\": \"N/A\",\"f\":\"N/A\"},"
options:0
range:NSMakeRange(0, [tmpJson length])];
NSString *correctedJson=tmpJson;
return correctedJson;
}
Called the function like this
result = [self performSelector:#selector(CorrectJsonforEmptyvalues:) withObject:result];
But getting error
2011-11-11 11:11:33.217 HelloWorld10[38833:207] -[Data CorrectJsonforEmptyvalues:]: unrecognized selector sent to instance 0x5725cc0
2011-11-11 11:11:33.219 HelloWorld10[38833:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Data CorrectJsonforEmptyvalues:]: unrecognized selector sent to instance 0x5725cc0'
If any one can please provide a solution it will be helpful.
Thanks in advance.
You have declared CorrectJsonForEmptyValues: as a class method by starting its declaration/definition with a + instead of a -. Therefore you call it on the class object, not on an instance of the class. If your class is named Data, for example, you call it like this:
result = [Data CorrectJsonForEmptyValues:result];
By the way, you should not start method names with capital letters.
You can call function as follows
[self CorrectJsonForEmptyValues:result];
And replace the '+' in the following with '-'
+(NSString *)CorrectJsonForEmptyValues:(NSString *)pasRawJson{

[NSCFString stringValue]: unrecognized selector sent to instance

My created application crashed when executing the below lines of code
where c1 is an integer variable.
NSString *path = c1.stringValue;
Shows the following error in log:
-[NSCFString stringValue]: unrecognized selector sent to instance
0x5566e80 2011-05-11 14:56:15.813
e-TREND[1552:207] Uncaught Exception
happens!! (NSInvalidArgumentException:
-[NSCFString stringValue]: unrecognized selector sent to instance
0x5566e80) 2011-05-11 14:56:15.816
e-TREND[1552:207] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'-[NSCFString stringValue]:
unrecognized selector sent to instance
0x5566e80'
if anyone have any idea to solve this issue , please answer accordingly.
where c1 is an integer variable
What does that mean? How is c1 declared?
If c1 were an int, then c1.stringValue wouldn't even compile.
The dot syntax only works when the object reference -- c1 -- is of a specific object reference type (not id) and that reference-- that class-- responds to the method.
So, you have something like:
MyThingThatRespondsToStringValue *c1;
And then you are, somewhere, assigning an instance of NSString to that variable which leads to the crash.
Please try this,
NSString *path = [NSString stringWithFormat:#"%#",c1];
Assume C1 is the instance of NSString.
Try with
NSString *path = [NSString stringWithString:c1];
OR
NSString *path = [[NSString alloc] initWithString:c1];

UIAddressBook code puts error on device

I testing following code on simulator ,it works fine but when I select the device and run then it gives exception .
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
tempPeoples= [NSMutableArray arrayWithCapacity:0];
for(int i=0;i<nPeople;i++){
ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);
NSString* name = (NSString *)ABRecordCopyValue(i1,kABPersonFirstNameProperty);
[tempPeoples addObject:name];
// [peoples addObject:i1];
}// end of
following exception occurs
2011-01-06 12:12:42.384 Appointment[2849:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
2011-01-06 12:12:42.397 Appointment[2849:207] Stack: (
843263261,
825818644,
842812211,
842812115
Please help
You're adding a nil to an array (as the message says)
By a process of deduction, I see that you are adding objects to an array at this line
[tempPeoples addObject:name];
So it is likely that, for this snippet of code, this is where the error happens.
Probably, Not all of the Contacts have a first name, which is likely to be the case for contacts that are businesses rather than people.
You could put a breakpoint in the code and run it through a debugger to see what conditions cause this.

Search string in NSMutableArray for showing in Table

Okay, I have an Book Object Class. That has Book.name and Book.id as extensions.
Book.name being NSString. Which is what should be searchable and listed in UITableView.
NSMutableArray *matchingSymptomsArray = [[NSMutableArray alloc] initWithCapacity:50];
for(NSMutableArray *letterArray in DataArray){
for(Book *bk in letterArray){ //Heres the ERROR
NSLog(#"Uptil NEW");
if([bk matchesSearchString:searchString]){
//I couldnt reach here, according to debugging and Logs//
[matchingSymptomsArray addObject:bk];
}
}
}
DataArray is where all the Books are held.. more than a hundred. Its the main datasource for the UITableView. I'm trying to search and match each letter of the string with the Book.name, but even before I can begin, I cant do the
for(Book *bk in LetterArray) //producing the error..
What seems to be the problem?
This is the error in console
**** -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110
2009-09-29 06:17:41.073 Smart Diagnosis[3897:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110'
Any work arounds??
thanks for your help :)
Try this:
for (id bk in letterArray) { }
Did you check that letterArray isnt nil or empty?
Did you check that letterArray isnt nil or empty?