0

I have swift file has this codes

Data.swift

class Data {
    class Entry {
        let filename: String
        let heading: String
        let dates: String
        init(fname: String, heading: String, dates: String) {
            self.heading = heading
            self.filename = fname
            self.dates = dates
        }
    }
    let places = [
        Entry(fname: "bridge.jpeg", heading: "Heading 1", dates: "26/04/2016"),
        Entry(fname: "mountain.jpeg", heading: "Heading 2", dates: "26/04/2016"),
        Entry(fname: "snow.jpeg", heading: "Heading 3", dates: "26/04/2016"),
        Entry(fname: "sunset.jpeg", heading: "Heading 4", dates: "26/04/2016")
    ]
}

I managed to access it on TableView like this

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PlacesTableViewCell

    let entry = data.places[indexPath.row]
    let image = UIImage(named: entry.filename)
    cell.bkImageView.image = image
    cell.headingLabel.text = entry.heading
    cell.dateLabel.text = entry.dates
    return cell
}

but Outside the TableView in another file "ViewController" I didn't know how to access it "dates" and convert it to NSDate ?!

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Rioodi
  • 9
  • 1
  • 5
  • 1
    The Entry object you have shown doesn't have a property called Dates, can you update your code. – Ben Sullivan Feb 23 '16 at 20:26
  • are you really asking how to share data from one controller to another? Is it just `dates` that you can't access, or your array `data`? If it **is** about sharing data, there are loads of SO answers on passing data between controllers - either through segues, or singletons, or even through CoreData or Realm – Russell Feb 23 '16 at 21:14
  • You're right segues I don't know how I missed it – Rioodi Feb 25 '16 at 19:25

1 Answers1

0

Here's a great NSDate extension which allows you to save your dates as NSDates. You would need to change the 'dates' property to NSDate.

Paste the following code in it's own file or outside of any class then anytime you write NSDate(, it will allow you to enter a string, just make sure the format is yyyy-mm-dd

extension NSDate {
  convenience
  init(dateString:String) {
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateFormat = "yyyy-MM-dd"
dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let d = dateStringFormatter.dateFromString(dateString)!
self.init(timeInterval:0, sinceDate:d)
  }
}

I will link to the original post if I can find it.

In regards to accessing the date from other controllers you could use the answer here:

Swift nested class properties

I would actually recommend having the Entry class in its own file and putting the places array in a struct, this makes it easily accessible from anywhere in your project:

class Entry {
let filename: String
let heading: String
let dates: String

init(fname: String, heading: String, dates: String) {

  self.heading = heading
  self.filename = fname
  self.dates = dates
  }
 }

struct Data {

static let places = [
 Entry(fname: "bridge.jpeg", heading: "Heading 1", dates: "26/04/2016")]
}

Then access with the following:

let data = Data()
let entry = data.places[0].dates
Community
  • 1
  • 1
Ben Sullivan
  • 2,134
  • 1
  • 18
  • 32