gpt4 book ai didi

objective-c - Swift 3 中的 withMemoryRebound 不适用于 Objective-C 结构指针

转载 作者:行者123 更新时间:2023-12-03 17:01:38 25 4
gpt4 key购买 nike

我一直在尝试连接一个 Objective-C 库,该库仅公开指向 C 结构数组的指针以及该数组中的计数。

我已经对结构使用了 withMemoryRebound 并设置了容量,正如我在此处和其他地方的其他帖子中看到的那样。然而,我在 Swift 中得到的数组有垃圾数据。作为解决方法,我向库中添加了一个访问器方法。但我真的不想修改这个库。

以下是测试应用程序的组件,用于演示问题和预期结果:

圆.h:

#import <Foundation/Foundation.h>

typedef struct {
float x, y; /**< Position in pixels. */
float radius; /**< Radius in pixels. */
} circle;

@interface CircleGenerator : NSObject {
}

@property (nonatomic) size_t numCircles;
@property (nonatomic, readonly) const circle* circles;

- (id)init;
// Added as a workaround:
- (void)fetchCircle:(size_t) i circle:(circle *) c;

@end

圆.m:

#import "Circle.h"

@interface CircleGenerator ()

@end

const circle myCircles[] = {
{.x=2467.38184, .y=338.520477, .radius=140.096466},
{.x=438.689453, .y=1879.302, .radius=116.18354}
};

@implementation CircleGenerator

- (id)init {
_numCircles = 2;
_circles = myCircles;
return self;
}

// Added as a workaround:
- (void)fetchCircle:(size_t) i circle:(circle *) c {
*c = _circles[i];
}

@end

ViewController.swift:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let circleGen = CircleGenerator()!

var circlesPtr = UnsafePointer(circleGen.circles)
withUnsafePointer(to: &circlesPtr) {
$0.withMemoryRebound(to: circle.self, capacity: circleGen.numCircles, {
for i in 0..<circleGen.numCircles {
print($0[i])
}
})
}
print("\nShould be:")
var nextCircle:circle = circle(x: 0, y: 0, radius: 0)
for i in 0..<circleGen.numCircles {
circleGen.fetchCircle(i, circle: &nextCircle)
print(nextCircle)
}
}
}

输出:

circle(x: 6.70394638e-38, y: 1.40129846e-45, radius: 2.47778904e+18)
circle(x: 4.59163468e-41, y: 6.7002974e-38, radius: 1.40129846e-45)
<小时/>

应该是:

circle(x: 2467.38184, y: 338.520477, radius: 140.096466)
circle(x: 438.689453, y: 1879.302, radius: 116.18354)

此外,$0.pointee$.pointee.x 等显示第一个元素的值不正确。

我是否有错误,或者我应该向 Apple 报告此问题吗?

最佳答案

circleGen.circles已经是类型化指针( UnsafePointer<circle> ),无需使用 withUnsafePointer() :

for i in 0..<circleGen.numCircles {
let circle = circleGen.circles[i]
print(circle)
}

输出:

circle(x: 2467.38184, y: 338.520477, radius: 140.096466)
circle(x: 438.689453, y: 1879.302, radius: 116.18354)

或者创建一个UnsafeBufferPointer这是一个“类似数组”的集合:

let circles = UnsafeBufferPointer(start: circleGen.circles, count: circleGen.numCircles)
for circle in circles {
print(circle)
}

但是如果您好奇为什么您的代码不起作用:您正在将指针重新绑定(bind)到 circlesPtr变量本身,不是存储在该变量中的指针。您将得到正确的结果

let circlesPtr = UnsafePointer(circleGen.circles)!
circlesPtr.withMemoryRebound(to: circle.self, capacity: circleGen.numCircles, {
for i in 0..<circleGen.numCircles {
print($0[i])
}
})

关于objective-c - Swift 3 中的 withMemoryRebound 不适用于 Objective-C 结构指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com