NSMutableDictionary "unrecognized selector sent to instance" - iphone

-(void)saveDictionary:(int)counter
{
NSString *path=[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSString *test = [NSString stringWithFormat:#"%d", counter];
[theDictionary setObject:test forKey:#"Counter"]; <---- Error
[theDictionary writeToFile:path atomically:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveDictionary:[_viewController counter]];
}
Error:
-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
I can Load the value for Key "Counter" from plist.
If I want to save the new value for same key "Counter" ... Error.
Need Help, spent hours.
bye
Here is the Code to initialize theDictionary:
-(void)initDictionary {
if (theDictionary == nil) {
NSString *path=[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theString = [theDictionary objectForKey:#"Counter"];
}
}
Found it!
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:#"Counter"]];
Thanks All!

This is because the object stored in theDictionary is actually an NSString and NSString doesn't contain a method called -setObject:forKey. Check your code for any places where theDictionary is being assigned and be sure that is actually an NSMutableDictionary.

check the initilization .. you could have done it like this .
NSMutableDictionary *dictoForSyncing = [[NSMutableArray alloc] init];

Sounds like theDictionary is a string instead of an NSMutableDictionary. Where is it created and what happended to it in the meantime?

This says that 'theDictionary' is not a dictionary at all. Most likely it was released earlier and some NSString has taken its place.
Are you using ARC? Where was 'theDictionary' defined.
And have you tried the zombies Instrument to track this down? That should help.

It seems that your property theDictionary isn't actually a dictionary, but a string (NSString). Where is theDictionary defined?

NSMutableArray *myObjFromFile = ....;
NSMutableDictionary *tmpDictFromFile =
[[[myObjFromFile objectAtIndex:xx] mutableCopy]; mutableCopy];
[tmpDictFromFile setObject:"YOUR OBJECT"
forKey:"YOUR KEY"];

Related

viewDidLoad using Persistence

I have problem with my Iphone project in viewDidLoad event the app crash on
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
I am trying to store information from text Filed can someone help me to solve the problem
- (void)viewDidLoad{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
for (int i = 0; i < 2; i++) {
UITextField *theField = self.lineFields[i];
theField.text = array[i];
}
NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
BIDThreeLines *threelines = [unarchiver decodeObjectForKey:kRootKey];
[unarchiver finishDecoding];
for (int i = 0; i < 2; i++) {
UITextField *theField = self.lineFields[i];
theField.text = threelines.lines[i];
}
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
Error
2013-03-25 23:29:45.592 MobilePaymentsApp[1182:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0
2013-03-25 23:29:45.593 MobilePaymentsApp[1182:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0'
*** First throw call stack:
(0x1c96012 0x10d3e7e 0x1d214bd 0x1c85bbc 0x1c8594e 0x1c0ae18 0xb030e8 0x339c 0xf91c7 0xf9232 0x483d5 0x4876f 0x48905 0x51917 0x2cc5 0x15157 0x15747 0x1694b 0x27cb5 0x28beb 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1617a 0x17ffc 0x29fd 0x2925)
libc++abi.dylib: terminate called throwing an exception
(lldb)
https://github.com/a-elnajjar/MobilePaymentsApp
NSKeyedArchiver returns object that you have stored in it. eg. if you have stored an array then it will return an array. so be careful while unarchiveing objects.
in following example i have read an array from NSKeyedUnarchiver.
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Look in the crash log's stack trace to see where exactly this call is
happening.
If the variable you're sending -row to isn't actually typed as an
NSArray, it's likely that you've failed to follow the memory
management rules for that variable. These same symptoms are very
commonly caused by that. Something that responds to -row could have
existed at one point, been deallocated because you didn't -retain it,
and then an NSArray was later allocated in that spot.
Run a "Build & Analyze," and re-re-review the memory management
guidelines until you know them in your sleep.
Source: [NSCFArray row]: unrecognized selector sent to instance 0x3953a20

JSONKit invalid arguement when try to copy

I am parsing JSON data with JSONKit as NSMutableDictionary.
NSString *str = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *jsonResponse = [self.responseData objectFromJSONData];
NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
[newData addEntriesFromDictionary:[jsonResponse mutableCopy]];
When i do this i am getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableDictionary addEntriesFromDictionary:]: dictionary argument is not an NSDictionary'
I am trying to figure out what is causing this problem. I know that jsonResponse is an object of JKArray from my other experience.
I need help.
Thanks.
Try the following:
id object = [self.responseData objectFromJSONData];
NSLog(#"%#", [object class]);
Most likely your response is an array instead of a dictionary.
If you really want to convert the array into a dictionary, you could do something like this, using a self-defined key:
NSArray *array = [self.responseData objectFromJSONData];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:array forKey:#"posts"];
Though perhaps there are some better options if you could show me the contents of your array.

how to update NSMutableDictionary

I have
NSMutableDictionary *mutDic;
its loaded with some values from other NSMutableDictionary
from an alert i am trying to update its value
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self.mutDic setValue:[[alertView textFieldAtIndex:0] text] forKey:#"lname"];
}
but i am getting this exception
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
how we can update dictionary ?
I had the same exception before even though my dictionary was mutable. Let me explain my scenario to you, may be it will help :
I had NSMutableArray of NSMutableDictionary,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [array objectAtIndex:0];
[dict setObject:#"" forKey:#""]; <-- it was crashing on this line...
so I changed my code as below,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]];
it worked fine :)
found out the exception problem, but not solved fully
on load i need to take values from other dictionary for that the method i used was wrong, i just assign oldDic to mutDic, i changed to
self.mutDic = [[[NSMutableDictionary alloc] initWithDictionary:manager.oldDic] retain];
their initialized it by
self.oldDic = [[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"F Name",#"fname",#"L Name",#"lname", nil ]retain];
that solved the exception

problem in parsing JSON response in iphone

i am getting json data from this link
now i want data from "html_instructions": part
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
NSString *string;
for(NSDictionary *di in resultarr){
NSLog(#"for loop");
string = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
but after printing "for loop" in console it is throwing an exception
2011-05-20 16:16:26.997 speedymap[759:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60
2011-05-20 16:16:26.999 speedymap[759:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60'
please help me
i want html_instructiuons field value
Check if resultarr and di really contains anything or not.
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
CFShow(resultarr);
NSString *string;
for(NSDictionary *di in resultarr){
CFShow(di);
NSLog(#"for loop");
NSArray *tempArr = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
http://jsonviewer.stack.hu/#http://maps.googleapis.com/maps/api/directions/json?origin=delhi&destination=noida&waypoints=&sensor=true
Check the viewer and set the values accordingly. [di objectForKey:#"legs"] returns an array. the first object of that array is a dictionary which has the key steps. But that too returns another array.

-[NSPathStore2 objectForKey:]: unrecognized selector sent to instance 0x6a3fc60'

I was preparing for a customCellView in a tableView. According to me, everything was perfect in .xib and methods. I am getting an exception in configuring the cell.
int listIndex = [indexPath indexAtPosition:[indexPath length] - 1];
NSLog(#"outside");
cell.lblName.text = (NSString *) [[directoryContent objectAtIndex:listIndex] objectForKey:#"filesName"];
cell.lblSize.text = (NSString *) [[directoryContent objectAtIndex:listIndex] objectForKey:#"filesSize"];
cell.lblType.text = (NSString *) [[directoryContent objectAtIndex:listIndex] objectForKey:#"filesType"];
return cell;
The compiler works till the NSLog(#"outside");. But it does not proceeds to the next line. I am terminated by the error
2010-11-15 20:32:28.623 iDataTraveller[5346:207] outside
2010-11-15 20:32:28.625 iDataTraveller[5346:207] -[NSPathStore2 objectForKey:]: unrecognized selector sent to instance 0x5f19cf0
2010-11-15 20:32:28.627 iDataTraveller[5346:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPathStore2 objectForKey:]: unrecognized selector sent to instance 0x5f19cf0'
Please help me to proceed.. Thank you in advance..
Wherever you created directoryContent, you stored the wrong object in it.
NSPathStore2 is an object that will be created if you use something like stringByAppendingPathComponent: on a NSString.
I guess you just stored the filename in the array but wanted to store a NSDictionary created from NSFileManagers attributesOfItemAtPath:error:
Check that part were you add objects to directoryContent again.
This is my code. Have a look at this. Let me know what i am lagging.
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = #"/Users/akilan/Documents";
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
error = nil;
}
NSMutableArray *array = [[NSMutableArray alloc] init];
self.directoryContent = array;
[array release];
for (NSString *path in paths){
filesName = [[path lastPathComponent] stringByDeletingPathExtension];
filesPath = [parentDirectory stringByAppendingPathComponent:path];
filesDirectory = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:filesPath error:nil]];
filesSize = [filesDirectory objectForKey:NSFileSize];
filesType = [path pathExtension];
createdDate = [filesDirectory objectForKey:NSFileCreationDate];
modifiedDate = [filesDirectory objectForKey:NSFileModificationDate];
filesDirectory = [NSDictionary dictionaryWithObjectsAndKeys:filesPath,#"filesPath",
filesName,#"filesName",filesSize, #"filesSize", filesType, #"filesType",
createdDate,#"createdDate", modifiedDate,#"modifiedDate", nil];
NSLog(#"%#", filesDirectory);
}
}
Thanks..
You have not retained directoryContent.
Can you show the code where directoryContent is created?
You can tell this because your error message says NSPathStore2 when it should be trying to call objectforKey on NSDictionary - this means that the memory where your dictionary was is not being used by something else :)