diff options
Diffstat (limited to 'docs/ObjectiveCLiterals.rst')
| -rw-r--r-- | docs/ObjectiveCLiterals.rst | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/docs/ObjectiveCLiterals.rst b/docs/ObjectiveCLiterals.rst index 8907c1efc77d..9fe7f66dffbf 100644 --- a/docs/ObjectiveCLiterals.rst +++ b/docs/ObjectiveCLiterals.rst @@ -119,8 +119,8 @@ Objective-C provides a new syntax for boxing C expressions: @( <expression> ) -Expressions of scalar (numeric, enumerated, BOOL) and C string pointer -types are supported: +Expressions of scalar (numeric, enumerated, BOOL), C string pointer +and some C structures (via NSValue) are supported: .. code-block:: objc @@ -136,6 +136,12 @@ types are supported: NSString *path = @(getenv("PATH")); // [NSString stringWithUTF8String:(getenv("PATH"))] NSArray *pathComponents = [path componentsSeparatedByString:@":"]; + // structs. + NSValue *center = @(view.center); // Point p = view.center; + // [NSValue valueWithBytes:&p objCType:@encode(Point)]; + NSValue *frame = @(view.frame); // Rect r = view.frame; + // [NSValue valueWithBytes:&r objCType:@encode(Rect)]; + Boxed Enums ----------- @@ -218,6 +224,42 @@ character data is valid. Passing ``NULL`` as the character pointer will raise an exception at runtime. When possible, the compiler will reject ``NULL`` character pointers used in boxed expressions. +Boxed C Structures +------------------ + +Boxed expressions support construction of NSValue objects. +It said that C structures can be used, the only requirement is: +structure should be marked with ``objc_boxable`` attribute. +To support older version of frameworks and/or third-party libraries +you may need to add the attribute via ``typedef``. + +.. code-block:: objc + + struct __attribute__((objc_boxable)) Point { + // ... + }; + + typedef struct __attribute__((objc_boxable)) _Size { + // ... + } Size; + + typedef struct _Rect { + // ... + } Rect; + + struct Point p; + NSValue *point = @(p); // ok + Size s; + NSValue *size = @(s); // ok + + Rect r; + NSValue *bad_rect = @(r); // error + + typedef struct __attribute__((objc_boxable)) _Rect Rect; + + NSValue *good_rect = @(r); // ok + + Container Literals ================== @@ -539,6 +581,22 @@ checks. Here are examples of their use: } #endif + #if __has_attribute(objc_boxable) + typedef struct __attribute__((objc_boxable)) _Rect Rect; + #endif + + #if __has_feature(objc_boxed_nsvalue_expressions) + CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"]; + animation.fromValue = @(layer.position); + animation.toValue = @(newPosition); + [layer addAnimation:animation forKey:@"move"]; + #else + CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"]; + animation.fromValue = [NSValue valueWithCGPoint:layer.position]; + animation.toValue = [NSValue valueWithCGPoint:newPosition]; + [layer addAnimation:animation forKey:@"move"]; + #endif + Code can use also ``__has_feature(objc_bool)`` to check for the availability of numeric literals support. This checks for the new ``__objc_yes / __objc_no`` keywords, which enable the use of |
