[英]Non Bool in if statement / Obj-C closures in Swift
我一直在嘗試將教程https://github.com/fastred/CustomScrollView從Objective-C轉換為Swift,但是我被困在一些地方。
在Objective-C中,它具有以下屬性:
@property (nonatomic, weak) UIDynamicItemBehavior *decelerationBehavior;
@property (nonatomic, weak) UIAttachmentBehavior *springBehavior;
然后具有if語句:
if self.decelerationBehavior && !self.springBehavior { ... }
由於這些顯然不是Bool
的,這在Swift中意味着什么?
void(^solveForY)(CGPoint*) = ^(CGPoint *anchor) {
if (deltaY != 0) {
anchor->y = a * anchor->x + b;
}
};
這似乎是一種關閉,我嘗試使用以下方法將其轉換為Swift:
func solveForY(_ anchor: inout CGPoint) { if deltaY != 0 { anchor.y = a * anchor.x + b } }
__weak typeof(self)weakSelf = self;
decelerationBehavior.action = ^{
CGRect bounds = weakSelf.bounds;
bounds.origin = weakSelf.dynamicItem.center;
weakSelf.bounds = bounds;
};
但是轉換后的應用程序的行為與原始行為完全不同。 有人可以告訴我如何將其轉換為Swift嗎?
如果我正確地解釋了所有這些,那么我將在Swift中執行以下操作:
var decelerationBehavior: UIDynamicItemBehavior?
var springBehavior: UIAttachmentBehavior?
// the if statement in Objective-C is checking for nil. In Objective-C nil == 0 == false
if let decelerationBehavior = decelerationBehavior, let springBehavior = springBehavior { ... }
let solveForY: (CGPoint) -> () = { (anchor) in
if deltaY != 0 {
anchor.y = a * anchor.x + b;
}
}
decelerationBehavior.action = { [weak self] in
if let weakSelf = self {
CGRect bounds = weakSelf.bounds
bounds.origin = weakSelf.dynamicItem.center
weakSelf.bounds = bounds
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.