ios - Swift UnsafeMutablePointer<Unmanaged<CFString>?> allocation and print -


i'm new swift , have difficulties deal pointers of unmanaged cfstring (or nsstring). i'm working on coremidi project implies usage of unsafemutablepointer?> can see in function :

func midiobjectgetstringproperty(_ obj: midiobjectref,                            _ propertyid: cfstring!,                            _ str: unsafemutablepointer<unmanaged<cfstring>?>) -> osstatus 

my problem want allocate buffer receive content of property (_str) call function above, , print content in console using println.

at moment wrote :

// first midi source (i know exists) var midiendpoint : unmanaged<midiendpointref> = midigetsource(0)  //c reate "constant" of 256 let buf = nsmutabledata(capacity: 256)   // allocate string buffer of 256 characters (i'm not sure want) var name = unsafemutablepointer<unmanaged<cfstring>?>(buf!.bytes)  // call function fill string buffer display name of midi device var err : osstatus =  midiobjectgetstringproperty(&midiendpoint,kmidipropertydisplayname,name)  // print string ... here no surprises don't know write print content of pointer, prints address moment println(name) 

i didn't find sample code use coremidi functions on apple developper library not on internet. confused because come cpp , things lot different in swift.

edit :

after rintaro , martin answers still have problem, test done on ios 8.1 , if copy code brought me compiler tells me can't write :

let err = midiobjectgetstringproperty(midiendpoint, kmidipropertydisplayname, &property) 

results in 'unmanaged' not convertible 'midiobjectref'. added "&" because midiobjectref unsafemutablepointer<void>.

let midiendpoint = midigetsource(0) var property : unmanaged<cfstring>? let err = midiobjectgetstringproperty(&midiendpoint, kmidipropertydisplayname, &property) 

now : 'unmanaged<midiendpoint>' not convertible '@lvalue inout $t2'. had change first let var, without understanding why ?!?

var midiendpoint = midigetsource(0) var property : unmanaged<cfstring>? let err = midiobjectgetstringproperty(&midiendpoint, kmidipropertydisplayname, &property) 

the code compiles , runs midiobjectgetstringproperty returns osstatus err -50 corresponds iow or macerros.h :

paramerr  = -50,  /*error in user parameter list*/ 

so seems parameters not ones midiobjectgetstringproperty waiting for.

the source "0" exist on ipad because midigetnumberofsources() returns 1. here's complete code :

var numdestinations: itemcount = midigetnumberofdestinations()     println("midi destinations : " + string(numdestinations))      var : itemcount = 0 ; < numdestinations; ++i{         var midiendpoint = midigetdestination(i)          var property : unmanaged<cfstring>?         let err = midiobjectgetstringproperty(&midiendpoint, kmidipropertydisplayname, &property)         if err == noerr {             let displayname = property!.takeretainedvalue() string             println(displayname)         }else{             println("error : "+string(err))         }    } 

displays :

midi destinations : 1 error : -50 

i don't understand ...

update :

finally martin found solution, seems there 2 different definitions of midiobjectref in 32 , 64bits architectures. run code on old ipad 2 code tried compile in 32bits mode midigetsource(i) return value not convertible midiobjectref. solution "unsafe cast" midi endpoint on 32 bits architectures :

#if arch(arm64) || arch(x86_64)     let midiendpoint = midigetdestination(i) #else     let midiendpoint = unsafebitcast(midigetdestination(i), midiobjectref.self) #endif 

... or buy new 64bit device ...

thank precious help

i have no experience coremidi , not test it, how should work:

let midiendpoint = midigetsource(0) var property : unmanaged<cfstring>? let err = midiobjectgetstringproperty(midiendpoint, kmidipropertydisplayname, &property) if err == noerr {     let displayname = property!.takeretainedvalue() string     println(displayname) } 

as @rintaro correctly noticed, takeretainedvalue() right choice here because callers responsibility release string. different usual core foundation memory management rules, documented in midi services reference:

note

when passing core foundation object midi function, midi function never consume reference object. caller retains reference responsible releasing calling cfrelease function.

when receiving core foundation object return value midi function, caller receives new reference object, , responsible releasing it.

see "unmanaged objects" in "working cocoa data types" more information.

update: above code works when compiling in 64-bit mode. in 32-bit mode, midiobjectref , midiendpointref defined different kind of pointers. no problem in (objective-)c, swift not allow direct conversion, "unsafe cast" necessary here:

let numsrcs = midigetnumberofsources() println("number of midi sources: \(numsrcs)") srcindex in 0 ..< numsrcs {     #if arch(arm64) || arch(x86_64)     let midiendpoint = midigetsource(srcindex)     #else     let midiendpoint = unsafebitcast(midigetsource(srcindex), midiobjectref.self)     #endif     var property : unmanaged<cfstring>?     let err = midiobjectgetstringproperty(midiendpoint, kmidipropertydisplayname, &property)     if err == noerr {         let displayname = property!.takeretainedvalue() string         println("\(srcindex): \(displayname)")     } else {         println("\(srcindex): error \(err)")     } } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -