Re: AVMetadataItem key
Re: AVMetadataItem key
- Subject: Re: AVMetadataItem key
- From: Fritz Anderson <email@hidden>
- Date: Sun, 04 Oct 2015 17:39:49 -0500
On Oct 1, 2015, at 3:59 PM, Jan E. Schotsman <email@hidden> wrote:
>
> Sorry for this stupid question but I just don't know how to do this.
>
> I have an AVMetaDataItem with
> key = protocol<NSCopying, NSObjectProtocol>? Int32(1851878757)
> as shown by the debugger
>
> How can can extract the value 1851878757 which is the four char code I need?
> Using Swift 2
My cut at a UInt32-to-OSType (four-byte string) converter is at the end of this message. The doc comment should speak for itself.
You can use it in source code.
You can use the function from the LLDB command line by passing it to an `expression` command, so long as the function is visible in the context.
You can (if it’s convenient) extend the class you’re interested in to be CustomReflectable and use the function to represent that part of the object.
— F
import Foundation
enum OSTypeError: ErrorType {
case BadStringFormat
}
/// Given a 32-bit integer, produce a good ol'fashion Mac `OSType`.
///
/// - parameters:
/// - code: The integer value of the code
/// - encoding: The string encoding by which to interpret the code.
/// Defaults to MacRoman for historical reasons.
///
/// - returns:
/// A `String` representing the code as four characters (see **Warnings**)
///
/// - throws:
/// `OSTypeError.BadStringFormat` if `NSString` could not find a correctly-encoded string in the four bytes.
///
/// Warnings:
/// - The presence of `NUL` bytes truncates the resulting string, because the conversion to `String` assumes the source bytes are a C string.
/// - The code must be representable in four bytes of the chosen encoding.
///
/// Bugs:
/// - The function could check-and-throw for more preconditions, such as the absence of `NUL` bytes in `code`.
func OSTypeFor(code: UInt32,
encoding: UInt = NSMacOSRomanStringEncoding)
throws -> String
{
let codePtr = UnsafeMutablePointer<UInt32>.alloc(1)
codePtr.initializeFrom([code])
defer { codePtr.destroy() }
let fourChars = UnsafeMutablePointer<CChar>(codePtr)
if let retval = String(CString: fourChars,
encoding: encoding)
{
return retval
}
else { throw OSTypeError.BadStringFormat }
}
var code: UInt32 = 1_851_878_757
try? OSTypeFor(code) // "eman"
try? OSTypeFor(0) // "" — see warnings in the documentation comment
do {
try OSTypeFor(0xFFFF_FFFF) // (four haceks)
try OSTypeFor(0xFFFF_FFFF, encoding: NSUTF8StringEncoding)
// (throws)
}
catch { print("Error:", error) }
// Error: BadStringFormat
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden