Files
swift-mirror/stdlib/public/SDK/AVFoundation/AVFoundation.swift.gyb
Joe Groff 86fbeee285 SE-0139: Bridge Cocoa framework structs to NSValue.
For every struct type for which the frameworks provides an NSValue category for boxing and unboxing values of that type, provide an _ObjectiveCBridgeable conformance in the Swift overlay that bridges that struct to NSValue, allowing the structs to be used naturally with id-as-Any APIs and Cocoa container classes. This is mostly a matter of gyb-ing out boilerplate using `NSValue.init(bytes:objCType:)` to construct the instance, `NSValue.objCType` to check its type when casting, and `NSValue.getValue(_:)` to extract the unboxed value, though there are a number of special snowflake cases that need special accommodation:

- To maintain proper layering, CoreGraphics structs need to be bridged in the Foundation overlay.
- AVFoundation provides the NSValue boxing categories for structs owned by CoreMedia, but it does so using its own internal subclasses of NSValue, and these subclasses do not interop properly with the standard `NSValue` subclasses instantiated by Foundation. To do the right thing, we therefore have to let AVFoundation provide the bridging implementation for the CoreMedia types, and we have to use its category methods to do so.
- SceneKit provides NSValue categories to box and unbox SCNVector3, SCNVector4, and SCNMatrix4; however, the methods it provides do so in an unusual way. SCNVector3 and SCNVector4 are packaged into `CGRect`s and then the CGRect is boxed using `valueWithCGRect:`. SCNMatrix4 is copied into a CATransform3D, which is then boxed using `valueWithCATransform3D:` from CoreAnimation. To be consistent with what SceneKit does, use its category methods for these types as well, and when casting, check the type against the type encoding SceneKit uses rather than the type encoding of the expected type.
2016-09-21 19:26:10 -07:00

67 lines
1.8 KiB
Plaintext

@_exported import AVFoundation // Clang module
import CoreMedia
import Foundation
%{
from gyb_foundation_support import \
ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods
}%
extension AVError {
/// The device name.
public var device: String? {
return userInfo[AVErrorDeviceKey] as? String
}
/// The time.
public var time: CMTime? {
return userInfo[AVErrorTimeKey] as? CMTime
}
/// The file size.
public var fileSize: Int64? {
return (userInfo[AVErrorFileSizeKey] as? NSNumber)?.int64Value
}
/// The process ID number.
public var processID: Int? {
return userInfo[AVErrorPIDKey] as? Int
}
/// Whether the recording successfully finished.
public var recordingSuccessfullyFinished: Bool? {
return userInfo[AVErrorRecordingSuccessfullyFinishedKey] as? Bool
}
/// The media type.
public var mediaType: String? {
return userInfo[AVErrorMediaTypeKey] as? String
}
/// The media subtypes.
public var mediaSubtypes: [Int]? {
return userInfo[AVErrorMediaSubTypeKey] as? [Int]
}
}
// Bridge CoreMedia structs to NSValue.
// AVFoundation provides internal NSValue subclasses for these structures that
// are incompatible with the NSConcreteValue subclasses you get using
// -[NSValue valueWithBytes:objCType:].
${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type="CMTime",
initializer="{ NSValue(time: $0) }",
getter="{ $0.timeValue }",
) }
${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type="CMTimeRange",
initializer="{ NSValue(timeRange: $0) }",
getter="{ $0.timeRangeValue }",
) }
${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type="CMTimeMapping",
initializer="{ NSValue(timeMapping: $0) }",
getter="{ $0.timeMappingValue }",
) }