如何在Swift里用UnsafeMutablePointer


下午在適配iPadUI的時候,用到了UIPopoverPresentationController,然后在轉屏的時候需要調用UIPopoverPresentationControllerDelegate來返回一個適配后的view和CGRect,這里先看下在OC里的寫法:

- (void)popoverPresentationController: (nonnull UIPopoverPresentationController *) popoverPresentationController willRepositionPopoverToRect:(inout nonnull CGRect *)rect inView:(inout UIView *__autoreleasing __nonnull * __nonnull)view { *view = self.someView; *rect = self.someView.bounds; }

在OC里面你可以很方便的修改這里的參數值來返回正確的指針地址,但是在swift里面方法是這樣子的:

func popoverPresentationController( popoverPresentationController:UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) { // What to do here? }

UnsafeMutablePointer和AutoreleasingUnsafeMutablePointer是什么鬼?這倆玩意要怎么返回值給他們?
先說結論
   在Swift里面rect.memory就是Objective-C里面的*rect

原因:

  這兩個參數並不是swift本身的寫法混亂,而是為了讓swift和Cocoa和UIKit下的Objective-C和C的frameworks 相互轉換兼容,查閱apple的SDK你可以發現UnsafeMutablePointer就是一個結構體:

struct UnsafeMutablePointer<Memory>

你可以把它看成一個Memory的指針,那現在再來看protocol里面的兩個參數:

  • UnsafeMutablePointer<CGRect>是一個CGRect的指針
  • AutoreleasingUnsafeMutablePointer<UIView?>是一個可選View的指針

所以這里你可以通過它的memory屬性來訪問它的引用存儲值:

/// Access the underlying raw memory, getting and setting values.public var memory: Memory { get nonmutating set }

例如:

// Objective-C assuming CGRect *rect; CGRect oldRect = *rect; *rect = newRect; // Swift assuming rect: UnsafeMutablePointer<CGRect> oldRect = rect.memory rect.memory = newRect

簡單來說:在Swift里面rect.memory就是Objective-C里面的*rect
所以最后我們可以在Swift里面這么寫:

func popoverPresentationController( popoverPresentationController:UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) { view.memory = someView rect.memory = someView.bounds }

參考:
Swift Standard Library Reference - UnsafeMutablePointer Structure Reference
How to Dereference an Unsafe Mutable Pointer in Swift


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM