viewDidLoad using Persistence - iphone

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

Related

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

NSMutableDictionary "unrecognized selector sent to instance"

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

Crash when using URL for swapping images

#implementation SlideShowViewController
- (id)init
{
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
nil,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8)
autorelease];
NSData *dato = [NSData alloc];
dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
if (self = [super initWithNibName:nil bundle:nil])
{
NSArray * images = [NSArray arrayWithObjects:[UIImage imageWithData:dato],[UIImage imageWithData:dato], [UIImage imageWithData:dato], [UIImage imageWithData:dato], [UIImage imageWithData:dato], nil];
self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];
}
return self;
}
I used the following code to load images from the server and view it as that of a photo album
But when the code is run it gets crashed
the error message in console is as follows
2011-06-24 23:54:01.837
SlideShow[13654:207] *
-[NSPlaceholderString stringwithString:]: unrecognized
selector sent to instance 0x49117e0
2011-06-24 23:54:01.839
SlideShow[13654:207] Terminating
app due to uncaught exception
'NSInvalidArgumentException', reason:
'** -[NSPlaceholderString
stringwithString:]: unrecognized
selector sent to instance 0x49117e0'
2011-06-24 23:54:01.840
SlideShow[13654:207] Stack: (
42178640,
43336492,
42187355,
41649782,
41646578,
12567,
7791,
2906510,
2910543,
2936126,
2917623,
2949592,
51171708,
41457820,
41453736,
2908705 ) terminate called after throwing an instance of 'NSException'
it works if the URLS are replaced by the images
could any one help me
I'm a beginner so its hard for me to find it out
thanks
You are trying to call an NSString class method with an instance (an incorrectly created instance at that) here
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
Change to
NSString *temp = #"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg";
EDIT:
You are doing several things wrong like calling alloc on things and then setting them to something else. (*temp and *data) when you alloc something it should always be followed with a call to init or initXXXX. Next you do not even need those alloc calls because you are setting the pointer to something else on the line right beneath it which causes a memory leak.
This is all you need
NSData *dato = [NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
Then you are creating a bunch of images with the same data object. You are also blocking the calling thread while you are downloading the image which should done later probably around time of viewDidLoad asynchronously.
The init function of the view controller is not the place for setting the view. Implement loadView and the system will call it when it is needed to minimize the applications memory footprint.

Searching an NSMutableArray with a String and returning the entire compared Array

it seems my Problem isn't a problem for anybody eles because I havend fount anything about it.
so it maybe isn't such a big Problem but for me it is.
I have this MutableArray filled with alot of data from an XML file.
-Name -Age -Address
The search goes for the Name, and the filtering works pretty fine so far.
what I do is search the array with rangeOfString but that only returns the String (-Name) and not the Array with it's content like the original Array because its only a string now.
can anyone tell me how do I accomplish this
That's my search so far
if ([[self searcher] length] != 0)
{
for (NSString *currentString in [self listOfContent])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self filteredListOfContent] addObject:currentString];
}
searcher is the String in the SearchBar.
or is there any other more efficent way or is it possible to search for any value in the MutipleArry?!?
Any Ideas and suggestions are welcome
I changed the code to this
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfContent)
{
//NSArray *array = [dictionary objectForKey:LNAME];
[searchArray addObject:dictionary];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"array %#", searchArray);
if ([sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)
[filteredListOfContent addObject:searchArray];
}
the log shows that the filter seems to work but then I get this error
2010-10-22 16:18:09.708 TableView[6114:207] -[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540
2010-10-22 16:18:09.712 TableView[6114:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540'
can anyone tell me what the problem is
And Still no solution found I changed the Code to this:
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in contentsList)
{
NSArray *array = [dictionary allValues];
[searchArray addObjectsFromArray:array];
}
for (NSDictionary *dict in searchArray)
{
if ([[dict valueForKey:#"NAME"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(#"Filter %#", dict);
[searchResults addObject:dict];
}
now i Have the array with the values but still get the error
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
can anyone explain me waht taht that error means or waht I did wrong?!?
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
That error means you treated an NSString as if it were an NSDictionary by sending the message -objectForKey: to it.

-[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 :)