Wahib
Wahib

Reputation: 93

In Swift, how to sum values from an array

Here is my code:

import Foundation

struct Movie {
    var title = ""
    var year = 0
    var isImportant: Bool
    var isFinished: Bool
    
    init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
        self.title = title
        self.year = year
        self.isImportant = isImportant
        self.isFinished = isFinished
    }

    static let list1 = [
        Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
        Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
        Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
        Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
        Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
        Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
        ]
    
    static let list2 = [
        Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
        Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
        Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
        Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
        ]
    
    static let list3 = [list1, list2]
    
    static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}


var list2Dim = Movie.list3
var list1 = Movie.list1

I have to sum the years from the "list1" dictionary, but not directly from "list1". I need to go through the "list2dim" dictionary. i mean (1994+1972+ ... +1993)

Upvotes: 0

Views: 412

Answers (2)

Wahib
Wahib

Reputation: 93

I finally found the answer to my question:

struct Movie {
    var title = ""
    var year = 0
    var isImportant: Bool
    var isFinished: Bool
    
    init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
        self.title = title
        self.year = year
        self.isImportant = isImportant
        self.isFinished = isFinished
    }

    static let list1 = [
        Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
        Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
        Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
        Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
        Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
        Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
        ]
    
    static let list2 = [
        Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
        Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
        Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
        Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
        ]
    
    static let list3 = [list1, list2]
    
    static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}

var list2Dim = Movie.list3

func sumSectionCalorie(Liste liste: Int) -> Int {
var total = 0
let number = list2Dim[liste].count
    for xxx in 0 ..< number {
    
        total += list2Dim[liste][xxx].year
}
    return total
}

let total = sumSectionCalorie(Liste: 0)
print(total)

Thank you CRD for giving me the tips.

Upvotes: 0

CRD
CRD

Reputation: 53000

I don't know how to navigate in it! Could you help me please?

Indexing is the same regardless of what is in the array, including other arrays, so "navigating" its just repeated indexing. You know how to index from your question Access a value in a 2D array through programming in Swift.

Try the following in a playground and make sure you understand each output line:

let list1 = [1, 2]
let list2 = [3, 4, 5]
let list3 = [6]

let listlist1 = [list1, list2]
let listlist2 = [list3]

let listlistlist = [listlist1, listlist2]

// print what we have produced
print(listlistlist)

// "navigate" into the structure, one step at a time
let result1 = listlistlist[0]
print(result1)
let result2 = result1[1]
print(result2)
let result3 = result2[2]
print(result3)

// indexing is left-associative, navigate in one step
let result012 = listlistlist[0][1][2]
print(result012)

If you are unsure what left-associative is read Operator associativity.

In the above example the final result is a number, from your previous question if the final result is an object you know you can access its properties.

In the answers to your question Multiply one dictionary by another in Swift you learnt how to use map to produce a new array by applying a function to each element of an existing array.

If a function such as map, which processes whole arrays following some pattern, exists in the library might there not be other functions in the library which also process whole arrays following some other pattern? So go find the documentation for map and then read through that area of the documentation to see what you find – you probably won't be disappointed and be a step closer to your goal of summing your year values.

If part way through your journey you get stuck ask a new question, include the code you've designed up to that point, describe what error you are getting or the step you can't figure out, and include a link back to this question so readers can read the trail through your Q&A's. Someone will undoubtedly help you with the next step.

Keep learning, reading and doing! HTH

Upvotes: 1

Related Questions