Problems accessing 1:N relationship in entity - core-data

https://github.com/lokming/QuestionBank
I have entities: Bank,Section,Subsection,Set,Question.
I am having problems accessing relationship "NSSet Subsection" in entity Section and getting the message "[UITableViewCell thesubsection]: unrecognized selector sent to instance" from this code
in CRSectionVC.m which fills a tableview cell
- (NSArray *)allQuestions
{
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision" ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
}
I can however access the "NNSet section" relationship in Bank entity using this code
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
_section2 is declared in CRSubsectionVC.h
#property (strong, nonatomic) Section *section2;
The storyboard is
1. CRMasterViewController which displays ‘category’ attribute from Bank entity into a tableview,
Bank.h
#class Section;
#interface Bank : NSManagedObject
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSSet *thesection;
#end
Bank.m
#implementation Bank
#dynamic category;
#dynamic thesection;
#end
When I tap a ‘category’ I segue and pass a Bank object to CRDetailViewController. I use following code:
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
to fetch section relationship (NSSet *thesection) ‘division’ attribute from Bank into tableview.
Section.h
#class Bank, Subsection;
#interface Section : NSManagedObject
#property (nonatomic, retain) NSString * division;
#property (nonatomic, retain) Bank *bank;
#property (nonatomic, retain) NSSet *thesubsection;
#end
Section.m
#implementation Section
#dynamic division;
#dynamic bank;
#dynamic thesubsection;
#end
If I tap a ‘section’ ’ I segue and pass a Section object to CRSubsectionVC named _section2. When I try to access NSSet *thesubsection to get ‘subdivision’ attribute using code
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision"ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
I get the error [UITableViewCell thesubsection]: unrecognized selector sent to instance. I cannot figure out why automated accessor ‘thesection’ works OK but not ‘thesubsection’ .
Subsection.h
#class Section, Set;
#interface Subsection : NSManagedObject
#property (nonatomic, retain) NSString * subdivision;
#property (nonatomic, retain) Section *section2;
#property (nonatomic, retain) NSSet *set;
#end
Subsection.m
#implementation Subsection
#dynamic subdivision;
#dynamic section2;
#dynamic set;
#end

Fixed the problem. A case of not copying code exactly from working template and understanding core data and table views.

Related

NSManagedObject Custom Class --NSInvalidArgumentException unrecognized selector --

I have a custom NSManagedObject Class That Looks Like This.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Ingredient, MenuCategory, Price, ItemSize;
#interface Item : NSManagedObject
#property (nonatomic, retain) NSNumber * collected;
#property (nonatomic, retain) NSString * desc;
#property (nonatomic, retain) NSString * instructions;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * quantity;
#property (nonatomic, retain) NSNumber * selected;
#property (nonatomic, retain) MenuCategory *menuCategory;
#property (nonatomic, retain) NSSet *prices;
#property (nonatomic, retain) NSSet *itemSizes;
#property (nonatomic, retain) NSSet *itemIngredients;
-(NSMutableSet *)mutablePrices;
-(NSMutableSet *)mutableItemIngredients;
#end
#import "Item.h"
#import "Ingredient.h"
#import "MenuCategory.h"
#import "Price.h"
#import "ItemSize.h"
#implementation Item
#dynamic collected;
#dynamic desc;
#dynamic instructions;
#dynamic name;
#dynamic quantity;
#dynamic selected;
#dynamic itemIngredients;
#dynamic menuCategory;
#dynamic prices;
#dynamic itemSizes;
-(NSMutableSet *)mutablePrices{
return [self mutableSetValueForKey:#"prices"];
}
-(NSMutableSet *)mutableItemIngredients{
return [self mutableSetValueForKey:#"itemIngredients"];
}
#end
Nothing so special RIGHT ???
The Following Should Work Right ????
[item.mutablePrices addObject:newPrice]
BUT IT DOES NOT GIVES ME THE FOLLOWING ERROR
2014-03-30 10:25:34.594 restos[1192:60b] -[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
2014-03-30 10:25:34.597 restos[1192:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0'
BUT WHEN I DO THE FOLLOWING
[[item mutableSetValueForKey:#"prices"] addObject:newPrice];
WORKS JUST FINE --- I KNOW IS SOMETHING SIMPLE FOR SOME REASON I CAN NOT SEE IT ----
THANK YOU IN ADVANCE
The error message
-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
indicates that your item object is not an instance of the Item class. A possible reason could be that you did not set the class of the entity to "Item" in the Core Data
model inspector.
Generally it is less error prone if you let Xcode generate the NSManagedObject subclass files (from the Edit menu), and add your custom methods in a Class Category. Alternatively, use "mogenerator".

CoreData error failed

In my application i have used following code with core data,
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Adduser : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSString * contact_no;
#property (nonatomic, retain) NSString * address;
#property (nonatomic, retain) NSString * response;
#end
Method
-(IBAction)signup:(id)sender
{
Registration *adduser=[Registration new];//<-CoreData: error: Failed to call designated initializer on NSManagedObject class 'Registration'
adduser.name=txt1.text;
adduser.password=txt2.text;
adduser.email=txt3.text;
adduser.contact_no=txt5.text;
adduser.address=txtvu.text;
}
When i try to implement is shows the
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Registration'
with SIGABRT error?what wrong with my code?Please help
To create an instance of a managed object class you need to:
Registration *adduser = [NSEntityDescription insertNewObjectForEntityForName:#"Registration" inManagedObjectContext:context];

How to make predicate to Entity's customized property

This question is an ongoing one for my previous question.
I want to make a predicate to filter the entity by my customized property, this is the entity class
#interface Expense : NSManagedObject
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSNumber * amount;
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSString * note;
#property (nonatomic, retain) NSString * paidby;
//customized property
#property (readonly) int year;
#property (readonly) int month;
#property (readonly) NSString *yyyy_mmm;
#property (readonly) NSString *mm_yyyy;
#end
And this is my predicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"yyyy_mmm == %#", header];
When run:
error message NSInvalidArgumentException', reason: 'keypath yyyy_mmm
not found in entity
This is not completely unexpected, I searched and find out it has something to do with key-value, listener stuff. But I do not know what to do exactly.
Any help will be so appreciated, and BTW the coding block has some problem, please forgive me.
Make any property you need to use in the predicate an actual key on the entity in your data model. If you can set the value directly, great, do that. If not you either want to implement willSave or use KVO and update your predicate key values from the other values in your entity.

[__NSCFSet entity]: unrecognized selector when insertin new nsmanegedobject to an to-many relation

I am adding a new instance of a Chore object to an Account instance. They are both NSManagedObjects, the relation from Account to Chore is a "one to many" relation.
The account instance is valid during run time as I am able to insert other object to it that follow the same rule for relation as chore does (1 to many), and chore is also valid as in the debugger both the account object and newChore have valid memory addres. The only thing is in the error the addres given, as 0x6d85cb0 is not the same as the address for newChore and account where at the time of this error report. 0x06b74a70 for newChore and 0x06d49700 for account, so I do not know to what 0x6d85cb0 is referring to.
This is the line of code “[account addChoresObject:newChore];” that generates the following error.
this is the line of code [account addChoresObject:newChore]; generates the folowing error.
2012-05-31 10:19:05.440 Allocation[61436:fb03] -[__NSCFSet entity]: unrecognized selector sent to instance 0x6d85cb0
2012-05-31 10:19:05.441 Allocation[61436:fb03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet entity]: unrecognized selector sent to instance 0x6d85cb0'
this is the code for the methode where the error is generated.
-(void)addChore:(Chore *)newChore
{
[account addChoresObject:newChore];
NSError *error;
[managedObjectContext save:&error];
if (error != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error Adding chore"
message:#"program not able to add new chore to account"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}
this is the interface for the Account class, as generated by xcode:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Chore, Gift, Goal, Operation;
#interface Account : NSManagedObject
#property (nonatomic, retain) NSNumber * balance;
#property (nonatomic, retain) NSDate * birthday;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSData * photo;
#property (nonatomic, retain) NSSet *chores;
#property (nonatomic, retain) NSSet *gifts;
#property (nonatomic, retain) NSSet *goals;
#property (nonatomic, retain) NSSet *operations;
#end
#interface Account (CoreDataGeneratedAccessors)
- (void)addChoresObject:(Chore *)value;
- (void)removeChoresObject:(Chore *)value;
- (void)addChores:(NSSet *)values;
- (void)removeChores:(NSSet *)values;
- (void)addGiftsObject:(Gift *)value;
- (void)removeGiftsObject:(Gift *)value;
- (void)addGifts:(NSSet *)values;
- (void)removeGifts:(NSSet *)values;
- (void)addGoalsObject:(Goal *)value;
- (void)removeGoalsObject:(Goal *)value;
- (void)addGoals:(NSSet *)values;
- (void)removeGoals:(NSSet *)values;
- (void)addOperationsObject:(Operation *)value;
- (void)removeOperationsObject:(Operation *)value;
- (void)addOperations:(NSSet *)values;
- (void)removeOperations:(NSSet *)values;
#end
and the Chore class is:
#interface Chore : NSManagedObject
#property (nonatomic, retain) NSString * interval;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * reward;
#property (nonatomic, retain) Account *account;
#end
To me it looks as if the selector is sent to at the wrong address.
I tryed to use Clean to rid of potential bad build files.
I have had a hard time before this to get the files generated correctly, as the generated files where called with the name of the app instead of being called with the name of the class and also had problem with some source files that would crash xcode when trying to auto generate iboutlets and ibaction. I am wondering if any one else as had problem with xcode and if re-installing it may fix my problem.
I think it has to do with the fact that the variable account in your addChore: method is not in the state you expect.
NSLog the account variable before adding the chore and see what is there. Maybe it is undefined and you have to make sure it refers to what you intend.

Core Data Custom Class Unknown Linker Error

Ey guys, I have been referencing this project, but I get this linker error:
Undefined symbols:
"_OBJC_CLASS_$_NSManagedObject", referenced from:
_OBJC_CLASS_$_ParkingLot in ParkingLot.o
_OBJC_CLASS_$_Permit in Permit.o
"_OBJC_METACLASS_$_NSManagedObject", referenced from:
_OBJC_METACLASS_$_ParkingLot in ParkingLot.o
_OBJC_METACLASS_$_Permit in Permit.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here is the relevant code(that I made following this[scroll down to "Custom Managed Object Class"]):
//Permit.h
#import <CoreData/CoreData.h>
#class ParkingLot;
#interface Permit : NSManagedObject
{
}
#property (nonatomic, retain) NSString * eligibility;
#property (nonatomic, retain) NSString * pricing;
#property (nonatomic, retain) NSString * type;
#property (nonatomic, retain) NSString * summary;
#property (nonatomic, retain) NSString * timeValid;
#property (nonatomic, retain) NSSet* parkingLots;
#end
#interface Permit (CoreDataGeneratedAccessors)
- (void)addParkingLotsObject:(ParkingLot *)value;
- (void)removeParkingLotsObject:(ParkingLot *)value;
- (void)addParkingLots:(NSSet *)value;
- (void)removeParkingLots:(NSSet *)value;
#end
//Permit.m
#import "Permit.h"
#import "ParkingLot.h"
#implementation Permit
#dynamic eligibility;
#dynamic pricing;
#dynamic type;
#dynamic summary;
#dynamic timeValid;
#dynamic parkingLots;
#end
//ParkingLot.h
#import <CoreData/CoreData.h>
#interface ParkingLot : NSManagedObject
{
}
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) id region;
#property (nonatomic, retain) NSSet* permits;
#end
#interface ParkingLot (CoreDataGeneratedAccessors)
- (void)addPermitsObject:(NSManagedObject *)value;
- (void)removePermitsObject:(NSManagedObject *)value;
- (void)addPermits:(NSSet *)value;
- (void)removePermits:(NSSet *)value;
#end
//ParkingLot.m
#import "ParkingLot.h"
#implementation ParkingLot
#dynamic title;
#dynamic region;
#dynamic permits;
#end
If you take a look at the sample code I followed, it is pretty much identical, yet it refuses to work and I can't figure out why. Thanks in advance for any help!
Stupid me... I needed to add the Core Data framework to my project. I could have sworn I had already done this... that's what I get for programming into the wee hours of the morning.

Resources