2014-06-29 191 views
2

以下代碼會導致編譯器錯誤:Could not find member 'Left'Swift:強制關閉參數?

let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in 
    return constraint.firstAttribute == .Left 
} 

前面加上NSLayoutAttribute之前.Left修復的錯誤,但有沒有辦法投constraintNSLayoutConstraint

例如,在Objective-C中,我可以在指定參數時投出constraint

NSUInteger indexOfConstraint = [constraints indexOfObjectPassingTest:^BOOL(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { 
    return constraint.firstAttribute == NSLayoutAttributeLeft 
}]; 
+1

你嘗試'(作爲約束NSLayoutConstraint).firstAttribute'醜陋? – Oxcug

+0

是的,我想這是最好的選擇。我意識到在問這個問題後工作。 – ma11hew28

回答

5

您可以使用括號投:

let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in 
    return (constraint as NSLayoutConstraint).firstAttribute == .Left 
} 

...這無疑比只用NSLayoutAttribute.Left

+0

是的,我想我會堅持後者。謝謝! :-) – ma11hew28