Sorting an NSMutableArrray that contains Arrays? - nsmutablearray

I have a class called userResults which looks like this:
#import <Foundation/Foundation.h>
#interface userResults : NSObject
#property (nonatomic, strong) NSString *resultsMix;
#property (nonatomic, strong) NSNumber *resultsSaving;
#property (nonatomic, strong) NSNumber *resultsBlendComponent1;
#property (nonatomic, strong) NSNumber *resultsBlendComponent2;
#property (nonatomic, strong) NSNumber *resultsTotalProtein;
#property (nonatomic, strong) NSNumber *resultsPricePerPound;
#property (nonatomic, strong) NSNumber *resultsPricePerPoint;
#end
With a implementation like this:
#import "userResults.h"
#implementation userResults
#end
I will build up multiple values of this in an NSMutableArray called results.
I want to be able to sort the records in results on the *resultsSaving field.
I have populated two arrays, results1 and results2, and stuck them in the larger array, and then I try to sort with this code.
results = [NSMutableArray arrayWithObjects:results1,results2,nil];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"resultsSaving" ascending:YES];
[results sortUsingDescriptors:sort];
Doesn't work. I think that I may have some conceptual problems. Any help would be greatly appreciated.
Bryan

After much scouring of the internet, I found this answer:
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"resultsSaving"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
sortedArray = [results sortedArrayUsingDescriptors:sortDescriptors];
What is odd is that you can't do descending, but must give a ascending:NO. That is pretty weird.

Related

Problems accessing 1:N relationship in entity

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.

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.

CoreData fetch all related objects

I have two related entities in core data
a book entity
#property (nonatomic, retain) NSString * author;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSSet *reviews;
a review entity
#property (nonatomic, retain) NSString * reviewer;
#property (nonatomic, retain) NSString * reviewText;
#property (nonatomic, retain) Book *book;
the last property in each is the relationship.
I have a book object
Book *book
How do I fetch from CoreData all review object that are related to 'book'?
book.reviews should return a set of review objects for that book.

self.navigationController value gets null in Tabbar VIew

I have a navigation controller as the root view controller. I have a UIView then a UITableView, then again a UITableView, then a TabbarView and each of my tabbarview is a hierarchy of views. Now my problem is that I am able to get the value of self.navigationcontroller till the last UITableView but the value gets null in the TabbarView. Now is there a way I can access the value in the Tabbarview. Do I have to create a new navigation controller for each tabs in the tabbarview and then assign the root navigation controller value to it?
My below code does not work,
The last controller where I can get the value of the navigationController is AssignmentViewController.
Code from AssignmentViewController:
- (void)viewDidLoad {
UITransTabViewController *transtvController = [[UITransTabViewController alloc] initWithNibName:#"TransTabViewController" bundle:nil];
self.transtabViewController = transtvController;
transtvController.naviController=self.navigationController;
[transtvController release];
}
In my UITranTabViewController.h I have the below code,
#interface UITransTabViewController : UIViewController <UITabBarDelegate> {
NSArray *viewControllers;
IBOutlet UITabBar *tabBar;
IBOutlet UITabBarItem *arrivalsTabBarItem;
IBOutlet UITabBarItem *departuresTabBarItem;
UIViewController *selectedViewController;
UINavigationController *naviController;
}
#property (nonatomic, retain) NSArray *viewControllers;
#property (nonatomic, retain) IBOutlet UITabBar *tabBar;
#property (nonatomic, retain) IBOutlet UITabBarItem *arrivalsTabBarItem;
#property (nonatomic, retain) IBOutlet UITabBarItem *departuresTabBarItem;
#property (nonatomic, retain) UIViewController *selectedViewController;
#property (nonatomic, retain) IBOutlet UINavigationController *naviController;
I am trying to set the value of naviController from the AssignmentViewController, but still I get the
NSLog(#"MY NAV CONTROLLER IN TRANSTABVIEWCONTROLLER IS = %#", self.naviController);
value as NULL.

How To Pass ManagedObjectContext To Other View Controllers in Special Case

I have a rootViewController like so:
Header:
#interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *lastUpdateLabel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
-(IBAction)selectHome:(id)sender;
//-(void)loadOverlays;
-(void)testCoreData;
#end
Implementation:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//...
[self testCoreData];
//creating label in tool bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
//label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
//label.highlighted = YES;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
[lastUpdateLabel initWithCustomView:label];
[label release];
[self.view addSubview:self.navigationController.view];
[self.navigationController.view setFrame:self.view.frame];
}
But I need to transfer my managedObjectModel to my table view and then to a map view so that the map view can make queries depending on what the user wants to see. I was consulting some apple sample code which looks like(from Recipes):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
recipeListController.managedObjectContext = self.managedObjectContext;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
I know that is in the appDelegate, but I figure that I can do the same when a row is selected or another view is pushed onto the stack, right? The problem is that I have configured most of my view with a nib which looks like so:
Due to this, I am unable to use a similar strategy that Apple employs to transfer a managedObjectModel to an alternate viewController(in this case PermitListViewController) because I don't access PermitListViewController directly when adding a subview. If anyone has any idea of how I would go about getting my managedObjectModel to my PermitListViewController. Please share! Thanks in advance!
EDIT: I am thinking of placing the managedObjectModel in a singleton class. What are your guys' thoughts on that? Good programming practice? Anything I should be aware of? Thanks.
Why not have the NSManagedObjectContext on the app delegate? Then it would be easily accessible from all your view controllers, and as they are UI they execute on the main thread, and can therefore share the same MOC.
I ended up creating a singleton class for the managedObjectModel using this as a reference (scroll down to "Creating a Singleton Instance").

Resources