2017-08-04 72 views
-1

以下是我的自定義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.") 
      } 
+0

只是好奇,是否有一個原因,你必須使用'CGFloat's的元組而不是'CGFloat'的數組? – RPatel99

+0

@ RPatel99是的。從我昨天發佈的https://stackoverflow.com/questions/45494236/how-do-i-check-the-number-of-values-in-an-array-that-is-a-udf-parameter。我想它可以檢查編譯過程中的錯誤,我猜可能會降低崩潰應用程序的可能性。 – ProgrammingEnthusiast

+0

[可以在swift中迭代元組的任何方法](https://stackoverflow.com/questions/24299045/any-way-to-iterate-a-tuple-in-swift) – ProgrammingEnthusiast

回答

1

退房this link爲靈感,這個答案。首先找個地方訪問這個功能在你的代碼:

func iterate<Tuple>(_ tuple:Tuple, body:(_ label:String?,_ value:Any)->Void) { 
    for child in Mirror(reflecting: tuple).children { 
     body(child.label, child.value) 
    } 
} 

然後使用你的代碼的更新版本:

if objectType === UIButton.self { 
     if xOriginCgFloat.2 != nil || yOriginCgFloat.2 != nil || width.2 != nil || height.2 != nil { 
      var buttonOneData:[CGFloat] = [CGFloat]() 
      var buttonTwoData:[CGFloat] = [CGFloat]() 
      var buttonThreeData:[CGFloat] = [CGFloat]() 
      var buttonsData:[[CGFloat]] = [buttonOneData,buttonTwoData,buttonThreeData] 
      var tuples = [xOriginCgFloat,yOriginCgFloat,width,height] 

      for tuple in tuples { 
       iterate(tuple) { 
        var indexStr = $0! //index = .0,.1,.2, etc 
        indexStr.remove(at:indexStr.startIndex) //remove the . from .0,.1,.2,etc. 
        let index = Int(indexStr) 
        buttonsData[index].append(CGFloat($1 as! Int)) //$1 = the value of tuple.0, tuple.1, tuple.2, etc. 
       } 
      } 

      for (index,buttonData) in buttonsData.enumerated() { 
       var button = UIButton(frame: CGRect.init(origin: CGPoint.init(x: buttonData[index], y: buttonData[index]), size: CGSize.init(width: buttonData[index], height: buttonData[index]))) 
       buttonArray.append(button) 
      } 

     } else { 
      fatalError("Each tuple for a button object must have exactly three values.") 
     } 
} 

這是非常令人費解,我誠實地而不是它的忠實粉絲,但如果你需要使用元組,那麼這可能會(可能)爲你工作。我基本上使用了這個答案頂部列表中的迭代函數來遍歷你的元組,並將它們添加到將數據從元組中分離出來的數組中,然後我創建了一個數組,以便迭代並製作實際的按鈕。

+0

謝謝!隨着我經歷同樣的事情的次數,它確實看起來很複雜。也就是說,我從另一個評論意識到,我的錯誤信息基本上只是爲了調試我的代碼,因爲'func'是在一個自定義的類中。另外,你知道爲什麼當我嘗試在同一個類中調用它時,func沒有給出參數嗎?它給'self.initializeStuff(self:customCollectionViewCell)' – ProgrammingEnthusiast

+0

你是否從單獨的靜態函數調用'initializeStuff'?我需要查看更多的代碼才能給出更具體的答案。 – RPatel99

+0

不,我從外部函數調用它,但是在同一個類中 – ProgrammingEnthusiast

1

爲什麼不使用這樣的函數,而不是:

func makeButtons(frames: [CGRect]) { 
    for frame in frames { 
     let button = UIButton(frame: frame) 
     buttonArray.append(button) 
    } 
} 
+0

我有UIButton,但我也有UIView和UILabel,所以對於其他兩個我會做兩個額外的,完全相似的功能? – ProgrammingEnthusiast

+0

實際上,目前尚不清楚爲什麼你需要這個功能。主要的一點是你應該使用'CGRect'數組,它保證是一致的,而不是'CGFloat'的四個數組/元組,這可能會有不一致的長度。 –

+0

謝謝!我做了函數,因爲我不想用手來初始化3個UIButtons,2個UIViews和2個UILabels。我認爲必須有一條捷徑。 – ProgrammingEnthusiast