Pregunta

Entonces, entiendo que las categorías en Objective-C se pueden usar para agregar métodos a las clases sin la necesidad de subclasificación. También entiendo que estas categorías no se pueden usar para agregar variables de instancia a las clases.

Hice un poco de lectura sobre las extensiones de clase, que se pueden usar para agregar variables de instancia, pero no entiendo cómo puedo usar las extensiones de clase para modificar una clase existente, como NSDATA.

Mi problema es lo siguiente:

Tengo un modelo de datos básicos que contiene un NSURL y NSDATA. El NSDATA muestra los datos para el NSURL. Cuando una vista necesita mostrar los datos, hago el siguiente control: --- Si [nsdata bytes]> 0, muestre el NSDATA. --- De lo contrario, busca los datos en NSURL y muestre los datos cuando devuelva

lo suficientemente simple. Sin embargo, me encuentro con problemas cuando se actualiza el NSURL. Por lo tanto, si modifico la ruta NSURL con una imagen nueva, ya que [NSDATA BYTES] ya es mayor que 0, no hago la llamada adicional para buscar la nueva imagen.

Lo que me gustaría hacer es agregar una variable de instancia a NSDATA llamada URLKEY que mantendría información sobre cómo provienen los datos. No puedo subclase NSDATA porque estoy usando Coredata.

¿Alguien sabe algunas soluciones simples para esto? Tal vez haya una brecha en mi entendimiento de las extensiones de clase, o tal vez simplemente no haya una manera simple.

¿Fue útil?

Solución

Class Extensions should be used on classes you implement yourself as a way of keeping ivars and some properties hidden from the header File, that should contain only stuff that should be visible outside the class (and ivars are't that kind of stuff).

Categories are used on classes already implemented, as a way of adding additional functionality. They are usually needed when you want to add a general kind of behavior to a known Class. E.g. adding a method to NSString +(NSString*)reversedString; that returns a reversed instance so you can then use it like this:

NSString *someString = @"string";
NSString *reverse = [someString reversedString]; 
NSLog(@"%@", someString); //this would output "gnirts"

.

Regarding your particular problem, I can assure you that your CoreDataModel does not contain NSURL or NSData. The supported types are primitives, strings, binary Data and transformables. So, if you want to, you can subclass NSData or NSURL and then use it with CoreData by setting the type to "transformable". And after you have done this, you can then subclass NSData as you wish and use class extensions in the process, or just use a category to add the methods you require to the class.

Quote from Apple about transformable attributes:

The idea behind transformable attributes is that you access an attribute as a non-standard type, but behind the scenes Core Data uses an instance of NSValueTransformer to convert the attribute to and from an instance of NSData. Core Data then stores the data instance to the persistent store.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top