core data countByEnumeratingWithState:objects:count: error - iphone

i have this error when i add to core data new relationship. I read in internets many resources but they can`t help me... Maybe you can...
This is my code then i adding relationship:
std = [NSEntityDescription insertNewObjectForEntityForName:#"Student" inManagedObjectContext:self.context];
std.fio = fio.text;
std.phone = phone.text;
NSError *error;
self.selectedGroups = MSGTVC.selectedGroups;
for(Group *info in self.selectedGroups){
[std addGroupsObject:info];
} if (![self.context save:&error]) {
NSLog(#"Cant Save: %#", [error localizedDescription]); }
in line [std addGroupsObject:info]; is error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Group countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x816a1c0'
This is my code then i assign value to selectedGroups:
NSArray *indexselected = [[NSArray alloc] initWithArray:[self.tableView indexPathsForSelectedRows
__OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_5_0)]];
for (NSIndexPath *into in indexselected)
{
self.selectedGroups = [self.group objectAtIndex:into.row];
}
I'll be happy if you help solve this problem, thanks in advance.

Related

RestKit 0.20: Map relational data from local file

I'm taking a local JSON file and trying to map it into a relational mapping. It works fine without the relationship, but once I add the relationship, I get an error.
JSON:
https://gist.github.com/4675414
Code:
// Get json from destination
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:contentPath encoding:NSUTF8StringEncoding error:NULL];
NSString* MIMEType = #"application/json";
NSError* parseError;
NSData *data = [myJSON dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&parseError];
if (parsedData == nil && parseError) {
NSLog(#"Cannot parse data: %#", parseError);
}
// Setting up objectmapping for issue
RKObjectMapping *issueMapping = [RKObjectMapping mappingForClass:[Issue class]];
[issueMapping addAttributeMappingsFromDictionary:#{
#"title": #"title",
#"description": #"description",
#"cover_url": #"cover_url",
#"published_at": #"published_at",
#"issue_number": #"issue_number"
}];
//Setting up objectmapping for article
RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]];
[articleMapping addAttributeMappingsFromDictionary:#{
#"title": #"title",
#"main_text": #"main_text",
#"article_image_url": #"article_image_url"
}];
[issueMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"articles" toKeyPath:#"articles" withMapping:articleMapping]];
Issue *issue = [[Issue alloc] init];
RKMappingOperation* mapper = [[RKMappingOperation alloc] initWithSourceObject:[parsedData objectForKey:#"issue"] destinationObject:issue mapping:issueMapping];
RKManagedObjectMappingOperationDataSource *mappingDS = [RKManagedObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;
[mapper performMapping:&parseError];
NSLog(#"Parse error: %#", parseError);
NSLog(#"Issue title: %#", issue.title);
Error:
2013-01-30 13:07:42.486 uninkd[13722:907] *** Assertion failure in -[RKManagedObjectMappingOperationDataSource mappingOperation:targetObjectForRepresentation:withMapping:inRelationship:], /Users/holgersindbaek/Projects/Uninkd/Uninkd_IOS/Pods/RestKit/Code/CoreData/RKManagedObjectMappingOperationDataSource.m:232
2013-01-30 13:07:42.487 uninkd[13722:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'RKManagedObjectMappingOperationDataSource must be initialized with a managed object context.'
Error if I take away the data source:
2013-01-30 13:27:32.601 uninkd[13754:907] *** Assertion failure in -[RKMappingOperation applyRelationshipMappings], /Users/holgersindbaek/Projects/Uninkd/Uninkd_IOS/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:699
2013-01-30 13:27:32.603 uninkd[13754:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot perform relationship mapping without a data source'
Hope you can help.
This is because RKManagedObjectMappingOperationDataSource is expecting a managed object context, this is specifically for Core Data. Are you using core data?
Forget everything I previously said and use:
#import "RKObjectMappingOperationDataSource.h"
RKObjectMappingOperationDataSource *mappingDS = [RKObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;
instead of:
RKManagedObjectMappingOperationDataSource *mappingDS = [RKManagedObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;
A hint, don't use any classes that say ManagedObject if you're not using CoreData. This is referring to a ManagedObject in a ManagedObjectContext which is unique to CoreData. That being said, core data + restkit is awesome and you should check it out.

"[CFString release]: message sent to deallocated instance" when using CoreData

I have started using CoreData in my project. Without the pieces of code from CodeData my project was working pretty fine. I added the methods for accessing the NSManagedObjectContext from the coreData project template. Now I try to create new CoreData object with the following code:
- (void)saveSearchResultToHistory:(NSArray *) productsArray {
[productsArray retain];
NSLog(#"context: %#", self.managedObjectContext);
Product *product = [NSEntityDescription
insertNewObjectForEntityForName:#"Product"
inManagedObjectContext:self.managedObjectContext];
product.productId = [(Product *) [productsArray objectAtIndex:0] productId];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
[productsArray release];
}
When this method is ran once then everything is fine, when I try to run it for the second time, the processing is stopped at:
Product *product = [NSEntityDescription
insertNewObjectForEntityForName:#"Product"
inManagedObjectContext:self.managedObjectContext];
with the following error message in the console:
[CFString retain]: message sent to deallocated instance 0x5a23b0
Any ideas what might be wrong?
Thanks!
First off you do not need to save the context every time you add something, just save when the app closes or goes in the background.
The error you are getting looks like you over release a NSString some where.
To check if the error isn't in the coredata context use this save function:
- (void)saveContext {
if ([self.managedObjectContext hasChanges]) {
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
dbgPrint(#"Failed to save to data store: %#", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if (detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
dbgPrint(#"--DetailedError: %#", [detailedError userInfo]);
}
} else {
dbgPrint(#" %#", [error userInfo]);
}
}
}
}

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.

updating fetched object results in 'unrecognized selector sent to instance' error

Trying to update an fetched object and getting 'unrecognized selector sent to instance' error whenever I try and set any property on the object, here is my code:
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entities = [NSEntityDescription entityForName:#"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
[request setEntity:entities];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ItemSKU=%#",collectionItem.ItemSKU];
[request setPredicate:predicate];
NSArray *results= [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if ([results count]==0) {
/* add another purchase order item */
PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:#"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
[newPOItem setProductName:productName.text];
[newPOItem setPrice:price];
[newPOItem setQuantity:qty];
[newPOItem setItemSKU:ItemSKU];
[newPOItem setSubTotal:itemSubTotal];
}else {
/* update item */
PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
[updateObject setSubTotal:itemSubTotal];
[updateObject setQuantity:qty];
}
EDIT:
here is the exact error:
2010-07-12 22:07:37.920 RCoreData[46733:207] *** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270
2010-07-12 22:07:37.921 RCoreData[46733:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270'
2010-07-12 22:07:37.921 RCoreData[46733:207] Stack: (
46504016,
47661868,
46512731,
45975158,
45971954,
209089,
3382510,
3879998,
3889344,
3884141,
3509736,
3401283,
3432920,
53940604,
45783196,
45779112,
53934237,
53934434,
3425138,
10940,
10794
)
terminate called after throwing an instance of 'NSException'
Note: Adding the item is working great, it is the update part that is throwing the error:
/* update item */
PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
[updateObject setSubTotal:itemSubTotal];
[updateObject setQuantity:qty];
PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:#"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
I think this line having problem. Are u sure that your newPOItem is in the correct class, are you sure that casting works well. Can you try with some code like: [newPOItem isKindOfClass:] and [newPOItem respondsToSelector:]
First, the cast is completely unnecessary. Both the -insertNewObjectForEntityForName:inManagedObjectContext: and -objectAtIndex: return id which is a generic object. The cast does nothing.
Second, when you run this in the debugger, what do you get when you
po 0x587b270
That will tell you what object it is trying to send the message to.
Third, you are not checking the error. You should always check the error before you start manipulating the array returned.

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