Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Also I am creating a fact app that I want to generate strings in my array in the exact order that I have typed them, in

Also I am creating a fact app that I want to generate strings in my array in the exact order that I have typed them, in each time my funFactLabel.text button is pressed I want the new fact to appear in the order it is typed. I have also created a struct to store my array properly. Can someone tell me what is wrong with my code please!

Swift

 import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var funFactLabel: UILabel!
        let factBook = FactBook()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        funFactLabel.text = factBook.factsArray[0]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func showFunFact() {
        funFactLabel.text = sortInPlace factBook.factsArray[1]

    }

1 Answer

Try this, it should start at [1] for you.

funFactLabel.text = factBook.factsArray.sortInPlace()

Hello! Tank you so much for assisting me! These are the errors I am receiving after implenting the code you just suggested.

import UIKit

class ViewControllervarIViewController { @IBOutlet weak var funFactLabel: UILabel! let factBook = FactBook()

! override func viewDidLoad() {  (METHOD DOES NOT OVERRIDE ANY METHOD FROM ITS SUPERCLASS)
  !  super.viewDidLoad()  ('SUPER' METHODS CANNOT BE REFERENCED IN A ROOT CLASS)
    // Do any additional setup after loading the view, typically from a nib.
    funFactLabel.text = factBook.factsArray[0]
}

! override func didReceiveMemoryWarning() { (METHOD DOES NOT OVERRIDE ANY METHOD FROM ITS SUPERCLASS) ! super.didReceiveMemoryWarning() ('SUPER' METHODS CANNOT BE REFERENCED IN A ROOT CLASS) // Dispose of any resources that can be recreated. }

@IBAction func showFunFact() {
    funFactLabel.text = factBook.factsArray.sortInPlace()
                                                                        (CANNOT USE MUTATING MEMBER ON IMMUTABLE VALUE: 'factsArray' is a 'let' constant
}

}

Have you simply tried to do this? I have never used sorting before as I am new to iOS but I am trying to help out with the best of my knowledge.

factBook.factsArray.sort()