如何在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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2015-10-10 swift 自定义TabBarItem
2015-10-10 UITabBarController中自定义UITabBar
2015-10-10 IOS引导页的编写
2015-10-10 Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value