ios - Convert from UnsafeMutableRawPointer to UnsafeMutablePointer, which approach is correct?
问题描述
To convert from UnsafeMutableRawPointer to UnsafeMutablePointer, both the approach works. (I'm using Swift 5.2)
// Approach 1
let unsafeMutablePointer = UnsafeBufferPointer(
start: unsafeMutableRawPointer.assumingMemoryBound(to: UInt8.self), count: Int(size.width * size.height * 4))
// Appraoch 2
let unsafeMutablePointer = unsafeMutableRawPointer
.bindMemory(to: UInt8.self, capacity: Int(size.width * size.height * 4))
Are they the same? Or they are meant for different purpose? (when to use which?)
I also notice for Approach 2, even if I give the wrong capacity (e.g. 0
), it still works. Is it automatically assign the minimum needed capacity from the unsafeMutableRawPointer
?
let unsafeMutablePointer = unsafeMutableRawPointer
.bindMemory(to: UInt8.self, capacity: 0)
解决方案
推荐阅读
- javascript - 无法在 Angular 8 中使用 write-good npm 模块
- python - Django PostgreSQL - 对单个单词进行模糊搜索
- flutter - 如何使 DataTable 或 PaginatedDataTable 占据其父卡的整个宽度?
- react-native - 如何在 React Native 中使用 AppState 的 Blur 事件
- r - 在基本图形R中围绕特定面板绘制一个矩形
- c# - 如何使用字段 ID 从 SQL Server DB 获取字段名
- sharepoint - 自定义人员选取器未显示 SharePoint 组的结果,其中用户是使用 SPD 工作流添加的
- azure-data-lake - 我们如何将数据插入到表中?
- azure-active-directory - 支持 SAML 4.0 的 Azure AD 能否与支持 SAML 2.0 的 SP 握手
- php - 如何在 SQL 查询的一个迭代中添加一个空格而不影响我的 PHP 中的另一个