1

我正在将从一本书中得到的一些 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变量。

4

1 回答 1

2

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)
于 2015-11-04T11:09:43.790 回答