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