Search code examples
macoscocoaappkitfoundation

macOS - [NSValue valueWithCGAffineTransform:]


How can I create an instance of NSValue that contains a CGAffineTransform?

UIKit provides [NSValue valueWithCGAffineTransform:], but AppKit does not.

Do I need to use the valueWithBytes:objCType: static method?


Solution

  • CGAffineTransform is a struct

    struct CGAffineTransform {
      CGFloat a, b, c, d;
      CGFloat tx, ty;
    };
    

    The correct way how to handle structs is mentioned in Key-Value Coding Programming Guide - Representing Non-Object Values (Wrapping and Unwrapping Structures)

    CGAffineTransform transform;
    NSValue *value = [NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)];