How to correctly display the enumerated objects using NSTreeController - objective-c

I'm trying to understand how to use NSTreeController. When I added a method (segment of the SourceView example's class and method are provided below) to iterate the contents of the directory, which is transmitted through NSTreeController to the NSOutlineView. But, NSOutlineView displays only the first objects (root objects) (you can be seen it on the schemes below).
Class NSTreeController Methods:
- (void)performAddChild:(TreeAdditionObj *)treeAddition {
if ([[treeController selectedObjects] count] > 0) {
// we have a selection
if ([[[treeController selectedObjects] objectAtIndex:0] isLeaf]) {
// trying to add a child to a selected leaf node, so select its parent for add
[self selectParentFromSelection];
}
}
// find the selection to insert our node
NSIndexPath *indexPath;
if ([[treeController selectedObjects] count] > 0) {
// we have a selection, insert at the end of the selection
indexPath = [treeController selectionIndexPath];
indexPath = [indexPath indexPathByAddingIndex:[[[[treeController selectedObjects] objectAtIndex:0] children] count]];
} else {
// no selection, just add the child to the end of the tree
indexPath = [NSIndexPath indexPathWithIndex:[contents count]];
}
// create a leaf node
BaseNode *node = [[BaseNode alloc] initLeaf];
node.urlString = [treeAddition nodeURL];
if ([treeAddition nodeURL]) {
if ([[treeAddition nodeURL] length] > 0) {
// the child to insert has a valid URL, use its display name as the node title
if ([treeAddition nodeName])
node.nodeTitle = [treeAddition nodeName];
else
node.nodeTitle = [[NSFileManager defaultManager] displayNameAtPath:[node urlString]];
}
}
// the user is adding a child node, tell the controller directly
[treeController insertObject:node atArrangedObjectIndexPath:indexPath];
// adding a child automatically becomes selected by NSOutlineView, so keep its parent selected
if ([treeAddition selectItsParent])
[self selectParentFromSelection];
}
- (void)addChild:(NSString *)url withName:(NSString *)nameStr selectParent:(BOOL)select {
TreeAdditionObj *treeObjInfo = [[TreeAdditionObj alloc] initWithURL:url
withName:nameStr
selectItsParent:select];
if (buildingOutlineView) {
// add the child node to the tree controller, but on the main thread to avoid lock ups
[self performSelectorOnMainThread:#selector(performAddChild:)
withObject:treeObjInfo
waitUntilDone:YES];
} else {
[self performAddChild:treeObjInfo];
}
}
Method for displaying enumerated objects of the directory:
- (void)addFinderSection {
[self addFolder:#"FINDER FILES"];
NSError *error = nil;
NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
for (NSURL *url in urls) {
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
if (isDirectory) {
if (![[NSWorkspace sharedWorkspace] isFilePackageAtPath:[url path]]) {
NSLog(#"IS DIRECTORY %#", url);
[self addChild:[url path] withName:NO selectParent:YES];
}
} else {
NSLog(#"IS FIlE %#", url);
[self addChild:[url path] withName:NO selectParent:YES];
}
}
}
[self selectParentFromSelection];
}
My mistake is probably that is NSTreeController does not differentiate between the simple objects (files) and the directories (folders).
However, when I fill NSOutlineView without NSTreeController, all content is displayed are correctly:
NOTE: If use method addFolder: (in SourceView example it used for create parent groups for other objects) then, every next object displayed as a subgroup of previous.
Can you help me to correctly display the folder contents in these methods?

Related

How to save data in Core Data?

I created an entity named House. This entity has two attributes which are street (of type string) and numOfStories (of type integer 32). I was able to successfully save and NSLog data in my AppDelegate.m didFinishLaunchingWithOptions method. However, when I try to make 2 textfields and show the User's input, and make a button to save, the result is SIGABRT.
Here is all the code I'm using in my MainViewController.m:
- (BOOL)createNewHouseWithStreet:(NSString *)paramStreet numOfStories: (NSUInteger)paramNumOfStories
{
BOOL result = NO;
if ([paramStreet length] == 0)
{
NSLog(#"Street name is mandatory");
return NO;
}
House *newHouse = [NSEntityDescription insertNewObjectForEntityForName:#"House" inManagedObjectContext:self.managedObjectContext];
if (newHouse == nil)
{
NSLog(#"Failed to create the new House");
return NO;
}
newHouse.street = paramStreet;
newHouse.numOfStories = #(paramNumOfStories);
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError])
{
return YES;
}
else
{
NSLog(#"Failed the save the new House. Error = %#", savingError);
}
return result;
}
and below this I have
- (IBAction)save:(id)sender {
[self createNewHouseWithStreet:#"10 WYOMING" numOfStories:2];
[self createNewHouseWithStreet:#"6 WYOMING" numOfStories:3];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"House"];
NSError *requestError = nil;
NSArray *houses = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requestError];
if ([houses count] > 0)
{
NSUInteger counter = 1;
for (House *thisHouse in houses)
{
NSLog(#"House %lu Street Name = %#", (unsigned long)counter, thisHouse.street);
NSLog(#"House %lu number of stories = %ld", (unsigned long)counter, (unsigned long)[thisHouse.numOfStories unsignedIntegerValue]);
counter++;
}
}
else
{
NSLog(#"Could not find any House entities in the context.");
}
}
So what's weird is that this code ends up resulting in sigabrt whenever I tap the save button, but when I put the code from the save method into my AppDelegate.m didFinishLaunchingWithOptions method it works great.
All help is appreciated, thanks.

CoreData and UITableView: display values in cells

I'm working with Core Data and web service, I want to add my data to my table,
but I don't know how should I call them, would you please help me, since when I used this way it's not working.
Here is my method for update database in my HTTP class
- (void)updateLocalCardsDataBase:(NSArray*) cardsArray
{
//check if current user has cards in local database
NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
for(NSDictionary *cardDic in cardsArray)
{
Card *card = [NSEntityDescription insertNewObjectForEntityForName:#"Card" inManagedObjectContext:managedObjectContext];
card.remote_id = [NSNumber numberWithInt:[[cardDic objectForKey:#"id"] intValue]];
card.stampNumber = [NSNumber numberWithInt:[[cardDic objectForKey:#"stampNumber"] intValue]];
card.createdAt = [NSDate dateWithTimeIntervalSince1970:[[cardDic objectForKey:#"createdAt"] intValue]];
[managedObjectContext lock];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
NSLog(#"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) {
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else {
NSLog(#" %#", [error userInfo]);
}
}
[managedObjectContext unlock];
}
Here is my table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
static NSString *CellIdentifier = #"CardsCell";
CardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"CardCell" owner:nil options:nil];
for (id currentObject in objects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CardCell *) currentObject;
break;
}
}
NSDictionary *f = [_cards objectAtIndex:indexPath.row];
cell.stampId.text = [f objectForKey:#"stampNumber"];
NSLog(#"%#fdssfdfddavds",[f objectForKey:#"stampNumber"]);
cell.createdAt.text = [f objectForKey:#"createdAt"];
cell.CardId.text = [f objectForKey:#"id"];
return cell;
}
Edit:
My problem is how I can show data in a UITableView
Before call [tableView reloadData], you need to get a data source first. You will get back an array of your data models, not an NSDictionary. You can place the my example method (or a variation that suits you best) where ever best suits your needs, but this one will not filter or sort the models, it will only get all of them. Also, I will place the method in your view controller that stores the table view:
-(NSArray*)getMycards {
NSManagedObjectContext *context = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Card" inManagedObjectContext:context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSError *error;
[request setEntity:entityDescription];
NSArray *cards = [context executeFetchRequest:request error:&error];
// now check if there is an error and handle it appropriatelty
// I usually return 'nil' but you don't have if you don't want
if ( error != nil ) {
// investigate error
}
return cards;
}
I recommend creating a property #property NSArray *cards in the view controller where you place your table, it will be easier to manage. One assumption I have made (since I have no other information about your view controller, a property named 'tableView' is declared in your view controller's header file (#property UITableView *tableView;), adjust the naming as needed.
With the above method, when you want to populate your array before loading the table's data:
// you put this block of code anywhere in the view controller that also has your table view
// likely in 'viewDidLoad' or 'viewDidAppear'
// and/or anywhere else where it makes sense to reload the table
self.cards = [self getMyCards];
if ( self.cards.count > 0 )
[self.tableview reloadData];
else {
// maybe display an error
}
Now, your cellForRowAtIndexPath should look like
-(UITableViewCell*tableView:tableView cellForRowAtIndexPath {
UITbaleViewCell *cell = ...;
// creating the type of cell seems fine to me
.
.
.
// keep in mind I don't know the exact make up of your card model
// I don't know what the data types are, so you will have to adjust as necessary
Card *card = self.cards[indexPath.row];
cell.stampId.text = [[NSString alloc] initWithFormat:#"%#",card.stamp];
cell.createdAt.text = [[NSString alloc] initWithFormat:#"%#",card.createdAt];
// you might want format the date property better, this might end being a lot more than what you want
cell.CardId.text = [[NSString alloc] initWithFormat:#"%#",card.id];
return cell;
}
Core Data is extremely powerful, I highly recommend the Core Data overview, followed by the Core Data Programming Guide.

How can I sort or add an item to a UITableView the opposite way?

So, I have an UITableView which holds entries for an app I am making. The entriesViewController is its own class, with a .xib file. I have a button that adds a new item.
It does this with the following code:
-(IBAction)newItem:(id)sender {
LEItem *newItem = [[LEItemStore sharedStore] createItem];
NSLog(#"New Item = %#", newItem);
[TableView reloadData];
}
Now this works, and adds the item, however it puts it at the bottom of the list. Since this app logs things for days, I do not want the items in this order. The newest items should be placed at the top of the list. How do I do this? I didn't see any easy way to add items to the table view at the top, but I might be missing something pretty basic.
This doesn't seem like it should be hard, I am probably just overlooking something.
Ideas are welcome.
Edit:
Here is LEItem Store:
//
// LEItemStore.m
//
// Created by Josiah Bruner on 10/16/12.
// Copyright (c) 2012 Infinite Software Technologies. All rights reserved.
//
#import "LEItemStore.h"
#import "LEItem.h"
#implementation LEItemStore
+ (LEItemStore *)sharedStore
{
static LEItemStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
-(id)init
{
self = [super init];
if (self) {
NSString *path = [self itemArchivePath];
allItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!allItems)
{
allItems = [[NSMutableArray alloc] init];
}
}
return self;
}
- (NSArray * )allItems
{
return allItems;
}
-(LEItem *)createItem
{
LEItem *p = [LEItem addNewItem];
[allItems addObject:p];
return p;
}
- (void)removeItem:(LEItem *)p
{
[allItems removeObjectIdenticalTo:p];
}
-(void)moveItemAtIndex:(int)from toIndex:(int)to
{
if (from == to) {
return;
}
LEItem *p = [allItems objectAtIndex:from];
[allItems removeObjectAtIndex:from];
[allItems insertObject:p atIndex:to];
}
- (NSString *)itemArchivePath {
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:#"item.archive"];
}
-(BOOL)saveChanges {
NSString *path = [self itemArchivePath];
return [NSKeyedArchiver archiveRootObject:allItems toFile:path];
}
#end
It looks like the simplest solution would be to modify -[LEItemStore createItem] to this:
-(LEItem *)createItem {
LEItem *p = [LEItem addNewItem];
[allItems insertObject:p atIndex:0];
return p;
}
You can do it even without rearrange the array internally.If you implement the data source and you define this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Assuming that in your array the oldest objects are at the lowest indexes,supposing that your table view has M rows, return a cell with the format of the object at index M-rowIndex-1.
Unless I'm missing something, after you create the new item, instead of using
[allItems addObject:p];
you just need:
[allItems insertObject:p atIndex:0];
Do you have any type of createdDate or other sortable property on the item? Simply sort your retained list of items (or NSFetchedResultsController) or whatever you are binding to by that property.
You can override the comparison mechanism in your LEItem class, and have it compare dates easily:
-(NSComparisonResult)compare:(LEItem*)otherItem {
return [self.dateCreated compare:otherItem.dateCreated];
}
Then, it's just a matter of using sortArrayUsingSelector: with the selector compare:.

Remove object from an array stored in a singleton

Im working with a singleton to store some data, her's the implementation
static ApplicationData *sharedData = nil;
#implementation ApplicationData
#synthesize list;
+ (id)sharedData
{
static dispatch_once_t dis;
dispatch_once(&dis, ^{
if (sharedData == nil) sharedData = [[self alloc] init];
});
return sharedData;
}
- (id)init
{
if (self = [super init])
{
list = [[NSMutableArray alloc]init];
}
return self;
}
if list have less than 3 (2<) object i the app crash with "index 0 beyond bounds for empty array"
// NSMutableArray *anArray = [[NSMutableArray alloc]initWithObjects:#"", nil];
while ([[[ApplicationData sharedData]list] lastObject] != nil)
{
File *file = [[[ApplicationData sharedData]list] lastObject];
BOOL isDir;
if (![[NSFileManager defaultManager] fileExistsAtPath:file.filePath isDirectory:&isDir])
{
NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:file.fileName,file.filePath,logEnteryErrorfileNotFoundDisplayName,[formatter stringFromDate:[NSDate date]], nil] forKeys:[NSArray arrayWithObjects:logShredFileName,logShredFilePath,logShredStatue,logShredDate, nil]];
[logArray addObject:tmpDic];
errorOccured = YES;
[[[ApplicationData sharedData]list] removeLastObject];
continue;
}
... other code
}
if i use the anArray that work perfectly.
what is the problem ?
That's totally weird, you've probably did something else to achieve this. Why don't you use - (void)removeAllObjects?
Maybe you remove objects in the while cycle the last line, ie:
while ([[[ApplicationData sharedData]list] count] != 0)
{
// remove object from list
// ...
[[[ApplicationData sharedData]list] removeLastObject];
}
And just a note, you don't need to check if (sharedData == nil) in sharedData as far as it's guaranteed to be executed only once. (unless you do something outside to your static variable, but that's not how it's supposed to be done I believe)

NSTextView Drag & Drop -- Characters not visible after drop

I've subclasses an NSTextView so that I can drop a file and copy the string contents of the file into the view (as opposed to the standard implementation which drops the filepath into the view). The text seems to be dropping correctly, but then is not visible after the drop. I can see that the cursor has moved and can even copy the dropped text out of the view and paste into, for example, TextEdit. I tried adding [self setNeedsDisplay:YES] at the end of my -performDragOperation: method, but the behavior did not change.
Here's the code I've written so far. I imagine this is not the best way to implement this. I'm new to drag and drop implementation in cocoa.
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
NSPasteboard *pb = [sender draggingPasteboard];
NSDragOperation dragOperation = [sender draggingSourceOperationMask];
if ([[pb types] containsObject:NSFilenamesPboardType]) {
if (dragOperation & NSDragOperationCopy) {
return NSDragOperationCopy;
}
}
if ([[pb types] containsObject:NSPasteboardTypeString]) {
if (dragOperation & NSDragOperationCopy) {
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pb = [sender draggingPasteboard];
if ( [[pb types] containsObject:NSFilenamesPboardType] ) {
NSArray *filenames = [pb propertyListForType:NSFilenamesPboardType];
for (NSString *filename in filenames) {
NSStringEncoding encoding;
NSError * error;
NSString * fileContents = [NSString stringWithContentsOfFile:filename usedEncoding:&encoding error:&error];
if (error) {
// handle error
}
else {
[self setString:fileContents];
}
}
}
else if ( [[pb types] containsObject:NSPasteboardTypeString] ) {
NSString *draggedString = [pb stringForType:NSPasteboardTypeString];
[self setString:draggedString];
}
return YES;
}
I had a stub for -drawRect: that had no implementation.
After removing the stub, everything works exactly as intended.