Hey all,
Regarding the challenge at the end of Project 1 of UIKit,
Attempting to sort the array my files in ascending order but Swift fails to build, can anyone see where I'm going wrong?
//
// ViewController.swift
// hws_project1
//
// Created by mojo on 15/04/2020.
// Copyright © 2020 m. All rights reserved.
//
import UIKit
class ViewController: UITableViewController {
var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Storm Viewer"
navigationController?.navigationBar.prefersLargeTitles = true
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items {
if item.hasPrefix("nssl"){
// this is a pic to load
pictures.append(item)
}
}
//this should order the pics in ascending order but doesnt work
picturesArray.sort(by: > )
}
// How many rows should appear in the table
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
// What each row should look like
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
cell.textLabel?.text = pictures[indexPath.row]
return cell
}
// 1. try loading the vc
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
// 2. set its selectedImage property
vc.selectedImage = pictures[indexPath.row]
// 3. push it on the navigation controller
navigationController?.pushViewController(vc, animated: true)
}
}
// Array of pics listed in the Content folder
var picturesArray = ["nssl0049.jpg", "nssl0046.jpg", "nssl0091.jpg", "nssl0045.jpg", "nssl0051.jpg", "nssl0041.jpg", "nssl0042.jpg", "nssl0043.jpg", "nssl0033.jpg", "nssl0034.jpg"]
}
The code in comments where it's not working is for my own benefit so I don't lose track. it builds successfully but when you run the app in the sim, it's still displaying the files in the order I've said!