在 Swift 中填充 NSRectPointer
Populating NSRectPointer in Swift
我正在将我从书中获得的一些 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 }
属性,因此 Swift 相当于 Objective-C 代码
*remainingRect = NSRectMake(0, 0, 100, 50);
应该是
remainingRect.memory = NSMakeRect(0, 0, 100, 50)
我正在将我从书中获得的一些 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 }
属性,因此 Swift 相当于 Objective-C 代码
*remainingRect = NSRectMake(0, 0, 100, 50);
应该是
remainingRect.memory = NSMakeRect(0, 0, 100, 50)