In the afternoon in the adaptation of the Ipadui, used UIPopoverPresentationController
, and then in the turn of the screen need to call UIPopoverPresentationControllerDelegate
to return to an adaptation of the View and CGRect, here first look at the wording in 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;}
In OC you can easily modify the parameter values here to return the correct pointer address, but in swift the method is like this:
func popoverPresentationController( popoverPresentationController:UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) { // What to do here?}
What the hell is Unsafemutablepointer and autoreleasingunsafemutablepointer? How are these two things going to return the value to them?
First, the conclusion :
In Swift, it's inside rect.memory
the objective-c.*rect
Reason:
These two parameters are not the confusion of Swift itself, but for swift and cocoa and Uikit under the OBJECTIVE-C and C frameworks mutual conversion compatibility, consult the Apple SDK you can find UnsafeMutablePointer
is a struct:
struct UnsafeMutablePointer<Memory>
You can think of it as a memory pointer, and now look at the two parameters in protocol:
UnsafeMutablePointer<CGRect>
It's a cgrect pointer.
AutoreleasingUnsafeMutablePointer<UIView?>
is a pointer to an optional view
So here you can access its reference store value through its Memory property:
/// Access the underlying raw memory, getting and setting values.public var memory: Memory { get nonmutating set }
For example:
// Objective-C assuming CGRect *rect;CGRect oldRect = *rect;*rect = newRect;// Swift assuming rect: UnsafeMutablePointer<CGRect>oldRect = rect.memoryrect.memory = newRect
In short: Inside Swift rect.memory
is the objective-c inside.*rect
So in the end we can write this in swift:
func popoverPresentationController( popoverPresentationController:UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) { view.memory = someView rect.memory = someView.bounds}
Reference:
Swift Standard Library Reference-unsafemutablepointer Structure Reference
How to dereference a Unsafe Mutable Pointer in Swift
How to use Unsafemutablepointer in Swift