• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary


  • Subject: Re: NSMutableDictionary
  • From: Paul Hoadley <email@hidden>
  • Date: Mon, 08 Feb 2016 09:09:40 +1030

Hi Bart,

On 8 Feb 2016, at 12:24 am, jazzsalsa <email@hidden> wrote:

The data looks like a tree (e.g. the AjaxTreeModel in the Ajax framework). Each node of the tree has a title (like a folder name for example). Each folder contains files, and can contain a sub-folder as well. 

I rewrote the code to avoid dictionaries as Hugi suggested. Like this I can access the folders and the files in the folder:

String content = new String(Files.readAllBytes(Paths.get(inputFile)));
Node rootNode;
rootNode = objectMapper.readValue(content, Node.class); // Jackson
List<Node> children = rootNode.getChildren().get(2).getChildren();
children.forEach(e -> log.info(e.getTitle()));

Now the problem:
Depending on how many subfolders there are I should use either children1 or children2:
List<Node> children1 = rootNode.getChildren().get(2).getChildren().get(0).getChildren();
List<Node> children2 = rootNode.getChildren().get(2).getChildren();

Is there a way to get all the files (children.getTitle) in all the subfolders?

It’s still not clear to me how much you know about this data structure before it’s read in. It sounds like it’s completely arbitrary ("Each folder contains files, and can contain a sub-folder as well”), but then you’re accessing parts of it with magic numbers (get(2), get(0)). If it’s arbitrary (by which I mean you don’t know the shape of it before you build it from the input), then I’d just walk the tree processing each node as you originally proposed. I don’t think there’s any shortcut here.

Is should only find the file names, not the name of the subfolder. Both are retreived from the rootNode object with rootNode.getChildren().getTitle(), but one is a folder name and the other a file name. 

Last question: how to add a list (called children) into the editing context? I can only think of this:

List<Node> children = rootNode.getChildren().get(2).getChildren();
ERXEC ec = new ERXEC();
ChildrenEO child = new ChildrenEO();
for (Node node : children) {
child.setTitle(node.getTitle());
child.setDateAdded(node.getDateAdded());
[...many more...]
}
ec.insertObject(child);
ec.saveChanges();

Is there a better way to convert a list to an EO automatically for all the items in the list?

I wouldn’t have thought so, no, but the code above needs fixing:

ERXEC ec = new ERXEC();
ChildrenEO child = new ChildrenEO();

You need to immediately insert that EO into the EC. Use:

ChildrenEO child = ChildrenEO.createChildrenEO(ec, …);

if you’re using templates that create such a method, otherwise:

ChildrenEO child = (ChildrenEO) EOUtilities.createAndInsertInstance(ec, “ChildrenEO”);

for (Node node : children) {
child.setTitle(node.getTitle());
child.setDateAdded(node.getDateAdded());
[...many more...]
}

You have many Nodes, but you’ve only created a single ChildrenEO, so this needs to be re-thought.

ec.insertObject(child);

Too late, see above.

ec.saveChanges();

This line looks good. :)


-- 
Paul Hoadley
http://logicsquad.net/



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSMutableDictionary (From: jazzsalsa <email@hidden>)
 >Re: NSMutableDictionary (From: Hugi Thordarson <email@hidden>)
 >Re: NSMutableDictionary (From: jazzsalsa <email@hidden>)
 >Re: NSMutableDictionary (From: Paul Hoadley <email@hidden>)

  • Prev by Date: HTTP 1.1's chunked encoding
  • Next by Date: Attachments ??
  • Previous by thread: Re: NSMutableDictionary
  • Next by thread: Re: NSMutableDictionary
  • Index(es):
    • Date
    • Thread