以下是我的自定義UICollectionViewCell類的代碼的一部分,並且我的for
循環中出現錯誤,表示函數定義中的元組類型沒有下標成員。有誰知道是否有辦法檢索元組的成員,就好像他們是數組?謝謝!我是否可以像索引循環中的數組中的項目一樣從元組中檢索項目?
func initializeStuff(objectType: AnyObject, xOriginCgFloat: (CGFloat, CGFloat, CGFloat?), yOriginCgFloat: (CGFloat, CGFloat, CGFloat?), width: (CGFloat, CGFloat, CGFloat?), height: (CGFloat, CGFloat, CGFloat?)) {
if objectType === UIButton.self {
if xOriginCgFloat.2 != nil || yOriginCgFloat.2 != nil || width.2 != nil || height.2 != nil {
for index in 0..<3 {
var button = UIButton(frame: CGRect.init(origin: CGPoint.init(x: xOriginCgFloat[index], y: yOriginCgFloat[index]), size: CGSize.init(width: width[index], height: height[index])))
buttonArray.append(button)
}
} else {
fatalError("Each tuple for a button object must have exactly three values.")
}
只是好奇,是否有一個原因,你必須使用'CGFloat's的元組而不是'CGFloat'的數組? – RPatel99
@ RPatel99是的。從我昨天發佈的https://stackoverflow.com/questions/45494236/how-do-i-check-the-number-of-values-in-an-array-that-is-a-udf-parameter。我想它可以檢查編譯過程中的錯誤,我猜可能會降低崩潰應用程序的可能性。 – ProgrammingEnthusiast
[可以在swift中迭代元組的任何方法](https://stackoverflow.com/questions/24299045/any-way-to-iterate-a-tuple-in-swift) – ProgrammingEnthusiast