Jak owinąć strukturę w NSObject

To ma być trywialne ... myślę, ale nie mogę znaleźć sposobu, jak zawinąć zmienną Struct w NSObject. Czy istnieje na to metoda? Jeśli nie, to jak miałbym dodać strukturę do NSMutableArray?

Dzięki.
Author: Wayne Chen, 2011-04-17

3 answers

Hm, spróbuj spojrzeć na NSValue na https://developer.apple.com/documentation/foundation/nsvalue

Możesz go używać jak

struct aStruct
{
    int a;
    int b;
};
typedef struct aStruct aStruct;

Następnie "zawiń" go do NSValue obiektu typu:

aStruct struct; struct.a = 0; struct.b = 0;
NSValue *anObj = [NSValue value:&struct withObjCType:@encode(aStruct)];
NSArray *array = @[anObj];

Aby wyciągnąć strukturę z NSValue Użyj:

NSValue *anObj = [array firstObject];
aStruct struct;
[anObj getValue:&struct];

Myślę, że później, możesz mieć kategorię z NSValue, aby to było lepsze =D

 74
Author: Rpranata,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-01 02:57:44

Coś w tym stylu byłaby Kategoria @Rpranata mówi o:)

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

Użycie:

// Boxing
Pair pair; pair.val = 2; pair.count = 1;
NSValue *value = [NSValue valueWithPair:pair];

// Unboxing
NSValue value = ...
Pair anotherPair = [value pairValue];
 12
Author: nacho4d,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:10

Użyj + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;

Z dokumentu: "tworzy nowy obiekt value zawierający podaną strukturę CoreGraphics affinise transform."

CGAffineTransform fooAffineTransform = CGAffineTransformMakeScale(.5, .5);
[NSValue valueWithCGAffineTransform:fooAffineTransform];

+valueWithCGAffineTransform istnieje od iOS 2.0.

 0
Author: Kyle Robson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-06 02:09:38