Ignacio Oroná
Ignacio Oroná

Reputation: 4399

RLMException', reason: 'Property 'productos' requires a protocol defining the contained type - example: RLMArray

So I am using a Realm for my project, I have an object, 'Pedido' (spanish for order) that has many 'V3Producto' objects, as follows:

@interface V3Producto : RLMObject

@property NSString                  *codeProd; // Este es el código de barras!
@property NSString                  *codigo;
@property NSNumber<RLMDouble>       *descuento;
@property NSString                  *detailProd;
@property NSInteger     idid;
@property NSInteger     idCompania;
@property NSNumber<RLMDouble> *priceProd;
@property NSInteger     stock;

@property int      cantidadComprada;
@property int      cantidad; // cantidad de stock

And the code for the 'Pedidos' (orders)

@interface Pedido : RLMObject

@property NSNumber<RLMDouble>   *idUbicacion;
@property NSString              *fechaPedido;
@property NSString              *sucursal;
@property NSNumber<RLMDouble>   *filterId;
@property RLMArray<V3Producto*>       *productos;

When I run the app, as soon as it loads I get the error: 'RLMException', reason: 'Property 'productos' requires a protocol defining the contained type - example: RLMArray' which is quite strange since, before adding the RLMArray seemed to work just fine! Any V3Producto seems to be a perfectly fine and valid RLMObject! Any ideas?

EDIT: I've tried renaming 'productos' to something else as other threads with the same name suggest but that didn't fix it.

Upvotes: 0

Views: 463

Answers (2)

Kuldeep Tanwar
Kuldeep Tanwar

Reputation: 3526

This is because when declaring an RLMArray property, the type must be marked as conforming to a protocol by the same name as the objects it should contain according to Realm Docs

Syntex to declare an RLMArray is :-

RLM_ARRAY_TYPE(ObjectType)
@property RLMArray<ObjectType *><ObjectType> *arrayOfObjectTypes;

Your code should be:-

RLM_ARRAY_TYPE(V3Producto)

@interface Pedido : RLMObject
@property NSNumber <RLMDouble>   *idUbicacion;
@property NSString *fechaPedido;
@property NSString *sucursal;
@property NSNumber <RLMDouble> *filterId;
@property RLMArray <V3Producto*> <V3Producto> *productos;
@end

Upvotes: 1

Ignacio Oron&#225;
Ignacio Oron&#225;

Reputation: 4399

The proper way to declare a RLMArray, would be as follows:

RLM_ARRAY_TYPE(V3Producto)

@interface Pedido : RLMObject

@property NSNumber<RLMDouble>   *idUbicacion;
...    
@property RLMArray<V3Producto*><V3Producto>       *productosPedido;

Note the I had to declare the type I want to use as an array with a MACRO (in the first line) and the declaration of the array is slightly different (you have to set the type plus the macro you've declared).

Upvotes: 0

Related Questions