se supone que esto es trivial … creo, pero no puedo encontrar la manera de envolver una variable de Struct en un NSObject
. ¿Hay algún método para hacerlo? Si no, ¿cómo voy a agregar una estructura en un NSMutableArray
?
Gracias.
Hm, intenta ver NSValue
en https://developer.apple.com/documentation/foundation/nsvalue
Puedes usarlo como
struct aStruct { int a; int b; }; typedef struct aStruct aStruct;
Luego, envuélvela en un objeto NSValue
como:
aStruct struct; struct.a = 0; struct.b = 0; NSValue *anObj = [NSValue value:&struct withObjCType:@encode(aStruct)]; NSArray *array = @[anObj];
Para extraer la estructura del uso de NSValue
:
NSValue *anObj = [array firstObject]; aStruct struct; [anObj getValue:&struct];
Supongo que más adelante, puedes tener una categoría de NSValue para mejorarla = D
Algo como esto sería la categoría @Rpranata está hablando 🙂
struct _Pair { short val; short count; }; typedef struct _Pair Pair; @interface NSValue (Pair) + (id)valueWithPair:(Pair)pair; - (Pair)pairValue; @end @implementation NSValue (Pair) + (id)valueWithPair:(Pair)pair { return [NSValue value:&pair withObjCType:@encode(Pair)]; } - (Pair)pairValue { Pair pair; [self getValue:&pair]; return pair; } @end
Uso:
// Boxing Pair pair; pair.val = 2; pair.count = 1; NSValue *value = [NSValue valueWithPair:pair]; // Unboxing NSValue value = ... Pair anotherPair = [value pairValue];
Utilice + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
De los documentos: “Crea un nuevo objeto de valor que contiene la estructura de transformación afín CoreGraphics especificada”.
CGAffineTransform fooAffineTransform = CGAffineTransformMakeScale(.5, .5); [NSValue valueWithCGAffineTransform:fooAffineTransform];
+ valueWithCGAffineTransform existe desde iOS 2.0.