[ad_1]
I adopted an youtube tutorial(since 2020) and I received an issue that I am unable to discover a answer. I simply need to present my a quote, randomized, in a label. Everytime person press the button, the textual content label is modified with one other quote from json.
That is my VC the place I need to show information
class ViewController: UIViewController {
@IBOutlet weak var quoteView: UITextView!
var consequence: Consequence?
override func viewDidLoad() {
tremendous.viewDidLoad()
quoteView.textual content = "You solely stay as soon as, however should you do it proper, as soon as is sufficient. — Mae West"
parsejson()
}
@IBAction func quoteTriggered(_ sender: Any) {
quoteView.textual content = consequence?.information //Can't assign worth of kind '[ResultItem]?' to kind 'String?'
}
personal func parsejson() {
guard let path = Bundle.important.path(forResource: "information", ofType: "json") else { return }
let url = URL(fileURLWithPath: path)
var consequence: Consequence?
do {
let jsonData = attempt! Knowledge(contentsOf: url)
consequence = attempt JSONDecoder().decode(Consequence.self, from: jsonData)
if let consequence = consequence {
print(consequence)
} else {
print("Didn't pase")
}
}
catch {
print("Error (error)")
}
}
}
struct Consequence: Codable {
let information: [ResultItem]
}
struct ResultItem: Codable {
let quote: String?
}
And my JSON file is that this
{
"quote" : ""Thegreatest glory in residing lies not in by no means falling, however in rising each time we fall. -Nelson Mandela"": {},
"quote" : ""Theway to get began is to stop speaking and start doing. -Walt Disney"": {},
"quote" : ""Ifyou have a look at what you have got in life, you may at all times have extra. In the event you have a look at what you do not have in life, you may by no means have sufficient. -Oprah Winfrey"": {},
"quote" : ""Ifyou set your targets ridiculously excessive and it is a failure, you'll fail above everybody else's success. -James Cameron"": {},
"quote" : ""Lifeis what occurs once you're busy making different plans. -John Lennon"": {},
"quote" : ""Spreadlove all over the place you go. Let nobody ever come to you with out leaving happier. -Mom Teresa"": {},
"quote" : ""Don'tjudge every day by the harvest you reap however by the seeds that you simply plant. -Robert Louis Stevenson"": {},
"quote" : ""Tellme and I neglect. Train me and I keep in mind. Contain me and I study. -Benjamin Franklin"": {},
"quote" : ""Thefuture belongs to those that consider in the fantastic thing about their goals." -Eleanor Roosevelt"": {},
"quote" : ""Iflife had been predictable it might stop to be life and be with out taste." -Eleanor Roosevelt"": {},
"quote" : """Lifeis a succession of classes which should be lived to be understood." -Ralph Waldo Emerson"": {},
"quote" : """Youwill face many defeats in life, however by no means let your self be defeated." -Maya Angelou"": {},
"quote" : """Theonly inconceivable journey is the one you by no means start." -Tony Robbins"": {},
"quote" : """Onlya life lived for others is a life worthwhile." -Albert Einstein"": {},
"quote" : """Thegreatest glory in residing lies not in by no means falling, however in rising each time we fall." -Nelson Mandela"": {},
"quote" : """Lifeis actually easy, however we insist on making it sophisticated." -Confucius"": {},
"quote" : ""Lifeis a protracted lesson in humility. -James M. Barrie"": {},
"quote" : ""Lovethe life you reside. Reside the life you like. -Bob Marley"": {},
"quote" : ""Manyof life's failures are individuals who didn't notice how shut they had been to success once they gave up. -Thomas A. Edison"": {},
"quote" : """Life is made from ever so many partings welded collectively." -Charles Dickens"": {}
}
[ad_2]
