Re: AVMetadataItem key
Re: AVMetadataItem key
- Subject: Re: AVMetadataItem key
- From: "Jan E. Schotsman" <email@hidden>
- Date: Wed, 07 Oct 2015 16:54:59 +0200
On Oct 5, 2015, at 12:39 AM, Fritz Anderson wrote:
On Oct 1, 2015, at 3:59 PM, Jan E. Schotsman <email@hidden>
wrote:
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.
Actually I got stuck because this did not work:
if let myNumber = item.key as? UInt32
Next morning I realized I should try
if let myNumber = item.key as? Int
which does work.
I had already defined a four char code struct. This is the improved
version inspired by your function:
/*
----------------------------------------------------------------------------------------------------------------------------------------- */
struct MyFCC:Equatable,CustomStringConvertible // four char code
{
var string = String()
init( _ fcc:String ) { string = fcc }
init?( rawCString:UnsafePointer<CChar>, encoding:UInt =
NSMacOSRomanStringEncoding )
{
let tempStr = String( CString:rawCString, encoding:encoding )
if tempStr == nil {return nil}
else
{
string = tempStr!
if string.utf8.count != 4 {return nil}
}
}
init?( ptr:UnsafePointer<CChar>, offset:Int = 0, encoding:UInt =
NSMacOSRomanStringEncoding )
{
let codePtr = GL_FourBytesToRawCStringFromPtr( ptr, offset:offset )
self.init( rawCString:codePtr, encoding:encoding )
codePtr.destroy()
}
init?( var uint32:UInt32 )
{
let ptr = withUnsafePointer(&uint32){$0}
self.init( ptr:UnsafePointer<CChar>(ptr) )
}
init?( data:NSData, offset:Int = 0 )
{
if data.length >= 4*(offset+1)
{
self.init( ptr:UnsafePointer<CChar>(data.bytes), offset:offset )
}
else { return nil }
}
init?( readFromQuickTimeFile fileHandle:NSFileHandle )
{
let theData = fileHandle.readDataOfLength(4)
self.init( data:theData )
self.byteSwap()
}
var data:NSData
{
return self.string.withCString{ NSData( bytes:$0, length:4 ) }
}
var uint32:UInt32
{
return UnsafePointer<UInt32>(self.data.bytes)[0]
}
mutating func byteSwap(){ self =
self.dynamicType.init( uint32:self.uint32.byteSwapped )! }
mutating func sanitize()
{
// replace unacceptable characters by ...
}
func copyToPtr( ptr:UnsafeMutablePointer<UInt32>, offset:Int = 0 )
{
ptr[offset] = UnsafeMutablePointer<UInt32>(self.data.bytes)[0]
}
func writeToQuickTimeFile( fileHandle:NSFileHandle )
{
var temp = self
temp.byteSwap()
fileHandle.writeData( temp.data )
}
var description:String {return "FCC " + self.string}
}
/*
----------------------------------------------------------------------------------------------------------------------------------------- */
func GL_FourBytesToRawCStringFromPtr( ptr:UnsafePointer<CChar>,
offset:Int = 0 )->UnsafeMutablePointer<CChar>
{
let codePtr = UnsafeMutablePointer<CChar>.alloc(5)
for index in 0 ..< 4 { codePtr[index] = ptr[index+4*offset] }
codePtr[4] = 0
return codePtr
}
/*
----------------------------------------------------------------------------------------------------------------------------------------- */
func == (lhs:GL_FCC, rhs:GL_FCC)->Bool
{
return lhs.string == rhs.string
}
Playground test:
var myFcc = MyFCC("name") // FCC name
myFcc.byteSwap() // FCC eman
var myFcc2 = MyFCC( uint32: 1851878757) // FCC eman
let data = myFcc2!.data // <656d616e>
let myFcc3 = MyFCC( data: data ) // FCC eman
var equal = myFcc == myFcc2 // true
let myFcc4 = MyFCC("moov") // FCC moov
equal = myFcc == myFcc4 // false
let myFcc5 = MyFCC(uint32:0) // nil
let myFcc6 = MyFCC(uint32:0xFFFFFFFF) // nil
let myFcc7 = MyFCC("0!?-") // FCC 0!?-
Jan E.
_______________________________________________
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