访问元组数组Swift 4

时间:2018-07-18 10:59:48

标签: arrays swift tuples core-location

我正在尝试为自定义集合视图单元格的某些组件分配值。该数组与自定义集合视图单元格位于同一类中,但是我无法缝隙访问该数组。任何意见,将不胜感激。

import UIKit
import CoreLocation
class eventCell: UICollectionViewCell {

    @IBOutlet private weak var eventTitle: UILabel!
    @IBOutlet private weak var descriptionLabel:UILabel!
    @IBOutlet private weak var eventImage: UIImageView!

    var eventArray = [(title:String, location:String, lat:CLLocationDegrees, long:CLLocationDegrees)]()   

    override func prepareForReuse() {
        eventImage.image = nil
    }

    func lool() {
        eventTitle.text = eventArray.title
    }
}

但是,当我尝试添加标题时,仍然出现此错误:

  

类型'[((标题:字符串,位置:字符串,纬度:CLLocationDegrees),长:CLLocationDegrees)]]'的值(又名'Array <(title:字符串,位置:字符串,lat:双精度,长:双精度)> ')没有成员“标题”

任何帮助都会很棒,在此先感谢!

2 个答案:

答案 0 :(得分:1)

您应该执行以下操作:

class eventCell: UICollectionViewCell {
    @IBOutlet private weak var eventTitle: UILabel!
    @IBOutlet private weak var descriptionLabel:UILabel!
    @IBOutlet private weak var eventImage: UIImageView!

    typealias Event = (title:String, location:String, lat:CLLocationDegrees, long:CLLocationDegrees)

    var eventArray = [Event]()


    override func prepareForReuse() {
        eventImage.image = nil
    }

    func lool() {
        var event = Event(title: "a", location:"b", lat:5, long:4)
        eventArray.append(event)
        eventTitle.text = eventArray[0].title
    }
}

答案 1 :(得分:0)

为什么不创建一些Struct? 像这样简单:

struct Event {
   var title: String
   var location: String
   var lat: CLLocationDegrees
   var long: CLLocationDegrees
}

然后执行以下操作:

var eventArray = [Event]()

并这样称呼它:

for event in eventArray{
  event.title = eventTitle.text
}