Parsing same element name in iphone - objective-c

I am not getting that how to differentiate same element name for eg City,Area in two different trees. So help me for this in parsing the same element name in iphone.
<Contacts id="1">
<Fname>Siddharth</Lname>
<Lname>Chopra</Lname>
<Currentaddress>
<Area>Aundh</Area>
<City>Pune</City>
<Phone>8796xxxx</Phone>
</Currentaddress>
<Permanentaddress>
<Area>Bhatar</Area>
<City>Surat</City>
<Phone>989825xxxx</Phone>
</Permanentaddress>
</Contacts>

In your class create an mutable array, that you'll use as a stack.
if a new element starts, create a object representing it (a custom (managed) object, or a dictionary) and add it to the array. In didFoundCharacter: alter the object, that is last on the array. If didEndElement: is called, save it either to core data, a file, sql or in memory to another array. and remove it from the array.
If the elements <Area>,<City> or <Phone>are handled, you'll know, that the object on the second last position is the address, they belong to. either <Currentaddress> or <Permanetaddress>.

You have to set a flag in didstartElement, the value of the flag will let you know, which parent tree is under parsing process.

Related

What data types cause object collection to behave as it does?

I understand arrays. I know some java from 15 years ago, and I know about classes, objects, instances, variables, static variables and constructors. Not so familiar with these things in VB.
I don't understand object collections..
Suppose I draw a listbox, and name it lstbox1
I see that I can say lstbox1.items.item(0) or lstbox1.items(0)
The fact that I can say listbox1.items(0) puzzles me a little bit. If an object collection is not an array, then it's clearly not an object either.
This link https://msdn.microsoft.com/en-us/library/yb7y698k(v=vs.90).aspx says Collection is an object.
But then what is items(0) items is not a class so that can't be calling a constructor... and items is not a method, it's a property that is an instance of object collection, so I can't see how the (0) works.. I know what it refers to the first object, the element with index 0, but I don't understand how that can work. I know blah(0) would work if blah was an array. And I am sure lstbox1.items is not an array of object collections it's just 1 object collection.
Is it a data structure like an Array, that has its own syntax.. for example one can say dim blah as Integer() or dim blah() as Integer and thus define it without even stating the class Array. Is ObjectCollection a bit like that? It does seem to allow (index) after an instance of it.
VB has a concept called Default Properties. In the case of an ItemCollection type (and a number of other types, as well), the Item property is the default property for the collection. This allows you to use the shorthand from the question.
It's basically just a bit of syntactical sugar. When you say, lstbox1.items(0), it's just shorthand for lstbox1.items.item(0).
Also, don't mistake the various collection types for simple arrays. They will have similar syntax, but every collection type has it's own quirks and use cases, and it's generally worth your time to look at the documentation for the specific type you're working with. Don't assume something is an array, just because you can access the items by index.
Think about this just as a "language feature". This is what really called indexer property. It is implemented as default property in vb.net. In c# implementation is different. The data structure behind it could be anything you want - array, list, dictionary, hashtable. The fact is - it lets you access something by supplying a parameter without calling property syntax. myParentObject(1) instead of myParentObject.GetChildObject(1)
In VB, default property must be indexed.
listbox1 . Items . item(0) --> listbox1 - main object that has property Items , which is a collection. This collection has property item, which is default property or indexer. Item is a property exposing single object from underlying collection.

Global operations on all class instances

There is the question: there are many fields instantiated in my CFD code and I want to count them all and write their contents to an external file (for example to save my calculation progress). My field derived type has only one component:
type scalar_field
real , dimension (:,:,:) , allocatable :: nodes
contains
! some procedures
end type
I am trying to create another type with counters and pointers to the node components of all fields. Something like this:
type field_counter
private
real ,pointer :: scalar_fields(:,:,:,:)
integer :: number_of_scalars
contains
procedure set_num_scalar_fields
procedure set_scalar_field_pointer
procedure output_scalars
end type
The main idea is to pass object of this type to the field constructor , where num_scalar_field attribute is incremented and slice of pointer array scalar_fields(i,:,:,:) is associated with nodes array. After that I would be able to print all contents of scalar fields via calling scalar_fields pointer array.
But I don't know whether it would work and whether it is an easiest way to perform this task, and I'll have to add the target attribute to each nodes array, which seems a bit overwhelming. Maybe there is some OOP design pattern for this task, or maybe anybody already have solved this kind of problem?
If, as you say, the number of fields is static after the program has initialised, I'd be inclined to create an array of them, like this:
type(scalar_field), dimension(:), allocatable :: all_fields
Once your program starts and figures out how many fields to allocate, then it can go ahead and allocate them
allocate(all_fields(num))
and you can reference individual fields as you would any other array element, like this:
all_fields(1)
I don't see the need for a new user-defined type for the array of scalar fields nor any pointers or any of that stuff. Mind you, I'm not sure I see the need for any OO at all here, back in the day I'd have just defined all_fields as a rank-4 array and used the last index as the identifier of the field.

Check whether the given object is a list?

How can we check whether the given object is a list or other type
in velocity. In that list i have another list which i need to iterate again.
I also have another data in the parent list which i want to print while iterating parent list. But the problem is the child list object also get printing with actual data. So i want to print the data by checking whether its list or not. Any help is much appreciated.
Before you get any remarks on using too much logic in templates, try this reflection based approach :
velocity (test instanceof)

How do I use a GtkComboBox with objects, as opposed to strings?

The usual use for a combo box is to let it display options to the user, and then you get an OBJECT out of it. In Win32, you do it by using the CB_SETITEMDATA and CB_GETITEMDATA messages, casting between int and object pointers. In XAML, you set up a data template and the item in the list IS the object.
What is the Correct way to get this effect with a GtkComboBox?
GtkComboBox normally uses a GtkListStore as the underlaying model.
You need to create one with an extra column for the object you want to store and as you insert new items in the combo's model you also need to provide the object you want to associate with that row/item.

How to use GtkTreeView correctly

I am using a TreeView with a ListStore as model. When the user clicks on a row I want to take some action but not using the values in the cells, but using the data I created the row from...
Currently I have the TreeView, the TreeModel (ListStore) and my own data (which I ironically call model)..
So the Questions are:
Is it "right" to have a model - an object representation of the data I want to display and fill a ListStore with that data to display in a TreeView, or would it be better to implement an own version of TreeModel (wrapping my data-model) to display the data?
And also:
If someone double-clicks in a row I can get the RowActivated event (using C#/Gtk#) which provides a Path to the activated row. With that I can get a TreeIter and using that I can get the value of a cell. But what is the best practice to find the data object from which the row was constructed in the first place?\
(Somehow this question got me to the first one - by thinking would getting the data object more easy if I tried to implement my own TreeModel...)
It's quite awkward/difficult to implement TreeModel, so most people simply synch the data from their "real" model into a TreeStore or ListStore.
The columns in the store do not have to match the columns in the view in any way. For example, you can have a column that contains your real managed data objects.
When you add a cellrenderer to a TreeView (visual) column, you can add mappings between its properties and the columns of the store. For example, you could map one store column to the font of a text cellrenderer, and another store column to the text property of the same cellrenderer. Each time the cellrenderer is used to render a particular cell, the mappings will be used to retrieve the values from the store and apply them to the properties of the renderer before it renders.
Here's an example of a mapping:
treeView.AppendColumn ("Title", renderer, "text", 0, "editable", 4);
This maps store column 0 to the renderer's text GTK property and maps store column 4 to the editable property. For GTK property names you can check the GTK docs. Note that the example above uses a convenience method that adds a column, adds a renderer to it and add an arbitrary number of mapping via params. To add mappings directly to a column, for example a column with multiple renderers, pack the renderers into the column then use TreeViewColumn.AddAttribute or TreeViewColumn.SetAttributes.
You can also set up a custom data function that will be used instead of mappings. This allows you to set the properties of the renderer directly, given a TreeIter and the store - so, if all the data you want to display is trivially derived from your real data objects, you could even have your store only contain a single column of these objects, and use data funcs for all the view columns.
Here's an example of a data func that does exactly what the mapping example above does:
treeColumn.SetCellDataFunc (renderer, delegate (TreeViewColumn col,
CellRenderer cell, TreeModel model, TreeIter iter)
{
var textCell = (CellRendererText) cell;
textCell.Text = (string) model.GetValue (iter, 0);
textCell.Editable = (bool) model.GetValue (iter, 4);
});
Obviously data functions are much more powerful because they enable you not only to use properties of more complex GTK objects, but also to implement more complex display logic - for example, lazily processing derived values only when the cell is actually rendered.