Swift : multi-dimensional structure organization and init

Viewed 39

There are two simple problems here in initializing this structure.

  1. One is the enunumerated value TS (I get error : Cannot convert value of type 'TournNames' to expected argument type 'TournamentName')
  2. the other is initializing an array of strings (I get the error : Cannot convert value of type '[String]' to expected argument type 'TouramentScores'

Suppose I am trying to set up a structure to model the scores of tennis players and all of their matches in each of the major tournaments (just for fun). Each tournament has a name (e.g. Wimbledon) and a series of scores for that player (for example, in the opening match, their score might be "4-6, 6-4, 7-6, 6-2")... upto 7 matches in each tournament. Each player should have an array of four tournaments (names and array of scores), and eventually there should be an array of players. I am also trying to use enums not too successfully. Ideally, if I want to find how Roger Federer did in his third match of wimbledon this year, I would access something like player.tournament.wim.Roundof32 or something roughly like that. But before I can even get to playing with that, I can't seem to init dummy data for even a single tournament.

Any ideas? I don't think this is that hard of question but I just don't know each. See "*** this line" below for two lines that are problematic

    // tournament name enum 
    
    enum TournNames : String {
       case wim = "Wimbledom"
       case fo = "French Open"
       case ao = "Australian Open"
       case uo = "US Open"
    }
    
        // 
    struct TournamentName  {
      var Tname  : TournNames  // = .wim
    }

    
    // This is the structure for a tournament score array with some dummy values.
    
    struct TouramentScores  {
       var Match1  : String = "7-6, 6-4, 3-6, 7-6"
       var Match2  : String = "7-6, 6-4, 3-6, 7-6"
 
    }
    


    // This is one entire Tournament record for one player = tournament name + array of scores ... the next goal but not used here until I get over these hurdles
    
    struct TournamentResult  {
       var TournamentName  : TournNames = .wim
       var Scores : TouramentScores
    }
    
   // ... finally the structure of a player ...

struct DummyTennisPlayer  {
   var LastName   : String  // last name
   var FirstName  : String  //first name
   var TN   :  TournamentName
   var TS   :  TouramentScores
 //  var WimRes   :  TournamentResult  // to start a single tournament
 //  var SeasonResults : [TournamentResult]   // ultimately should be an array of 4 tournaments



}
// trying to initialize some dummy data without success after trying several things

   extension DummyTennisPlayer {
      static var dummyResults : [DummyTennisPlayer] {
         [
            DummyTennisPlayer.init(
               LastName : "Federer",
               FirstName: "Roger",
               TN : TournNames.wim      // **** this line
              ,
              TS : ["XX", "yy"]        /// *** this line 
               )
         ]
      }
   }
2 Answers

As I think you're discovering, a simple series of nested types is unlikely to cut it here. As soon as you get to entities like players, tournaments, matches and lookups like "how Roger Federer did in his third match of wimbledon this year", you've become a candidate for using a database where you can manipulate one-to-many and many-to-many relationships. I can't tell you what database to use, and anyway that's a matter of opinion; from what you've said so far, SQLite would be sufficient (and I am personally not a fan of Core Data just for this kind of thing).

I guess your code is a kind of exercise, so before you go on later to Core Data or SQLite,

extension DummyTennisPlayer {
    static var dummyResults: [DummyTennisPlayer] = [
        DummyTennisPlayer(LastName: "Federer", FirstName: "Roger", WimbledomResult: TournamentResult(Scores: TouramentScores()))
    ]
}

should answer your question.

1 - To initialize a Swift struct, use the following syntax:

MyStruct(property1: "my property1 value", property2: "my property2 value")

2 - the tournament name property in TournamentResult is already set to .wim so you just need to initialize the Scores. As your TournamentScores properties are already all set, you just need to pass an instance of TournamentScores() to TournamentResult(Scores:). By the way, only use lowercases for the first letter of the name of your variables or struct properties: var lastName or TournamentResult(scores:).

I think you are confusing the term "multi-dimensional (array) structures" (which are just arrays nested inside other arrays, like that: [ [1, 2, 3], [2, 3, 4], [3, 4, 5]]) with the struct objects. You are probably not supposed to use structs so extensively here.

Don't hesitate to review the way you decide to use enums, structs, or arrays. Your code may work but will be difficult to read and use (example: how would you access a specific set score if you put all of the set scores in a single String? Why not use an array?)

Related