在Swift中填充NSRectPointer

时间:2015-11-04 10:35:03

标签: objective-c swift nsrect

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

1 个答案:

答案 0 :(得分:2)

NSRectPointer定义为

public typealias NSRectPointer = UnsafeMutablePointer<NSRect>

UnsafeMutablePointer有一个

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

属性,因此Swift等同于Objective-C代码

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

应该是

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