How do I use CFArrayRef in Swift?

后端 未结 2 1682
鱼传尺愫
鱼传尺愫 2021-01-19 05:40

I\'m using an Objective-C class in my Swift project via a bridging header. The method signature looks something like this:

- (CFArrayRef)someMethod:(someType         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-19 06:40

    As explained in Working with Core Foundation Types, there are two possible solutions when you return a Core Foundation object from your own function that is imported in Swift:

    • Annotate the function with CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED. In your case:

      - (CFArrayRef)someMethod:(someType)someParameter CF_RETURNS_NOT_RETAINED;
      
    • Or convert the unmanaged object to a memory managed object with takeUnretainedValue() or takeRetainedValue() in Swift. In your case:

      var cfArr = myInstance.someMethod(someValue).takeUnretainedValue()
      

提交回复
热议问题