> ## Content Index
> Fetch the complete content index at: https://blogs.remobjects.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Strong Typed Rows in the DataAbstract for Cocoa
- URL: https://blogs.remobjects.com/2014/09/01/strong-typed-rows-in-the-dataabstract-for-cocoa/
- Published: 2014-09-01T19:06:20.000Z
- Updated: 2018-08-22T06:17:38.000Z
- Author: alexk
- Tags: #Import 2026-08-17 20:17

## Challenge

In [DataAbstract for Cocoa](http://wiki.remobjects.com/wiki/Data%5FAbstract%5Ffor%5FXcode?ref=blogs.remobjects.com), you can retrieve the values for a particular table field using the *KVC* approach, where the field name is passed as a string key:

| DADataTableRow \*row \=\[\[self table\] rowAtIndex:idx\]; NSString\*name \=\[row valueForKey:@"ClientName"\]; |
| ------------------------------------------------------------------------------------------------------------- |

Later, with help of the new indexing syntax for arrays and dictionaries, this code can be rewritten in a more simple manner:

| DADataTableRow \*row \= self.table.rows\[idx\]; NSString\*name \= row\[@"ClientName"\]; |
| --------------------------------------------------------------------------------------- |

But there is one flaw to that approach – the field name is passed as a string constant and thus can’t be checked in design time or selected in the code completion system. The user still has to work with \*DADataTabeRow\* instances, where data is represented by the dictionary, rather than by business objects.

## Solution

With the help of some “Magic” in *Objective-C*, we can now offer another approach for getting values for a particular column in a row.  
You can just operate with a particular field in the table row as with a usual property.  
This means that you can call any property in a row instance like this:

| id fieldValue \=\[self.dataTable.rows\[idx\] MyFieldName\]; |
| ----------------------------------------------------------- |

If there is no such field in the table, you will get a proper exception, otherwise you will get its value.

It would also be great though if we could choose available fields using the *Code Completion* system inside the IDE.  
This would simplify the code and allow to avoid various possible mistakes in specifying field names when working with data of the row.

And the answer is yes, we can do that.  
All we need to do is to represent the available table fields as properties of a special protocol (interface) that the row can support.

This approach can be used in two ways:  
The most simple way is to just generate protocols ad later cast the row instance to the row protocol. For example:

| // Protocol @protocol DepsTableRow\_Protocol @property(strong)NSString\*DepName; @property(strong)NSNumber\*DepId; @property(strong)NSString\*DepPhone; @end // Code for obtaining data id row \= self.dataTable.rows\[0\]; id typedRow \= row; NSNumber\*depId \=\[typedRow DepId\]; NSString\*depName \=\[typedRow DepName\]; NSString\*depPhone \= typedRow.DepName;// you also can use dot(.) for accessing field property NSLog(@"%@(%@): Phone:%@:", depName, depId, depPhone); |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Another approach is to provide a special row class for the particular table. This class must be inherited from the base \*DADataTableRow\* class, but in addition, it can expose different properties:

| //Protocol:@protocol DepsTableRow\_Protocol @property(strong)NSString\*DepName; @property(strong)NSNumber\*DepId; @property(strong)NSString\*DepPhone; @end // Custom row class @interface DepsTableRow : DADataTableRow // you don't need to implement DepsTableRow\_Protocol for declaring its properties@end // Code for obtaining data:// First we need to register our custom row class for the particular table// It should be done once per application start // The init method of the DataAccess module is usually a good place for doing that\[DADataTable registerRowClass:\[DepsTableRow class\] forTableName:@"Deps"\]; // then later each row in the table rows array is the instance of our custom row DepsTableRow \*typedRow \= self.dataTable.rows\[0\]; NSNumber\*depId \=\[typedRow DepId\]; NSString\*depName \=\[typedRow DepName\]; NSString\*depPhone \= typedRow.DepName; // You can use a dot(.) to access fields as properties NSLog(@"%@(%@): Phone:%@:", depName, depId, depPhone); |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

\## Supporting Strong Typed Rows in Nougat

The same approaches are also available for Nougat projects and Oxygene:

| // Protocol DepsTableRow\_Protocol\=publicinterfaceproperty DepName: strong NSString readwrite;property DepId: strong NSNumber readwrite;property DepPhone: strong NSString readwrite;end; // Code for obtaining data var row: id :\=self.dataTable.rows\[0\];var typedRow : DepsTableRow\_Protocol :\= duck(row); var depId :\= typedRow.DepId;var depName :\= typedRow.DepName;var depPhone :\= typedRow.DepPhone; NSLog("%@(%@): Phone:%@:", depName, depId, depPhone); |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

and for RemObjects C# (Hydrogene):

| // Protocolpublicinterface DepsTableRow\_Protocol { \_\_strong NSString DepName {get;set;} \_\_strong NSNumber DepId {get;set;} \_\_strong NSString DepPhone {get;set;}} // Code for obtaining data  id row \= self.dataTable.rows\[0\]; DepsTableRow\_Protocol typedRow \= duck<DepsTableRow\_Protocol\>(row); NSNumber depId \= typedRow.DepId; NSString depName \= typedRow.DepName; NSString depPhone \= typedRow.DepPhone; NSLog("%@(%@): Phone:%@:", depName, depId, depPhone); |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

\## Generating protocols

The one remaining question is how to get those row protocols?

For *Nougat* projects created in *MS Visual Studio*, these protocols will be generated automatically by the *New Project Wizard*.  
You can also update/regenerate these protocols any time you want – just run the wizard by clicking *Create Table Definition Classes* in the Solution Explorer window.

![TableDefinitions Wizard](https://blogs.remobjects.com/wp-content/uploads/2014/06/TableDefinitionsWizard.png)

For *Xcode* projects, you can generate the protocols directly from the schema node inside the [Relativity Server](http://www.remobjects.com/da/relativity/?ref=blogs.remobjects.com) in the [Server Explorer for Cocoa](http://www.remobjects.com/da/serverexplorer/mac.aspx?ref=blogs.remobjects.com) tool. It allows to generate *Objective-C*, *Oxygene* and *RemObjects C#* protocols for *Nougat* projects.

![TableDefinitions Generator](https://blogs.remobjects.com/wp-content/uploads/2014/06/TableDefinitionsGenerator.png)

## Samples

Take a look at the *StrongTypedRows (iOS)* samples written in *Nougat*, *Oxygene* and *RemObjects C#*. We are providing them with the *DataAbstract for Cocoa* package.

Thanks for reading!