How to use Unsafemutablepointer in Swift

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.