gpt4 book ai didi

objective-c - 在 Swift 中填充 NSRectPointer

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:32 26 4
gpt4 key购买 nike

我正在将我从一本书中获得的一些 Objective-C 代码翻译成 Swift。有问题的代码是 NSTextContainer 方法的自定义实现:

-(NSRect)lineFragmentRectForProposedRect:(NSRect)proposedRect
sweepDirection:(NSLineSweepDirection)sweepDirection
movementDirection:(NSLineMovementDirection)movementDirection
remainingRect:(NSRectPointer)remainingRect
{

// ... now set value of the struct pointed at by NSRectPointer
*remainingRect = NSRectMake(0, 0, 100, 50);

//...
return mainRect;
}

我努力在 Swift 中复制这个 - 无论我尝试什么,我总是被告知我不能分配给 let 变量。

最佳答案

NSRectPointer 定义为

public typealias NSRectPointer = UnsafeMutablePointer<NSRect>

UnsafeMutablePointer有一个

/// Access the underlying raw memory, getting and setting values.
public var memory: Memory { get nonmutating set }

属性,因此是 Objective-C 代码的 Swift 等价物

*remainingRect = NSRectMake(0, 0, 100, 50);

应该是

remainingRect.memory = NSMakeRect(0, 0, 100, 50)

关于objective-c - 在 Swift 中填充 NSRectPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33519587/

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