0

It might be basic and wrong but after understanding structures I can't understand where to practically put it.

Inside some class call it Main, I would like to encapsulate a set of variables for dimensions.

I know I can just do :

struct Dimensions{
var w:Int
var h:Int
}

class Main
{
   //do things using the structure
}

But since i have a lot of variables and i want it clean , I would like to create a new Swift file and put it inside

So inside a file called Dimensions or else :

import Foundation

struct Dimensions{
var w:Int
var h:Int
}

then the structure is visible to anyone, without even using the Swift file name.

A few questions to ask :

  1. It seems like a bad idea - why ?
  2. How is it different from a Singeltone to share data between classes? (value type?)
  3. What is the right place to put the structure outside the Main class to get some clear code ?
  4. Should I make one file with many not related Structs ?
Curnelious
  • 1
  • 16
  • 76
  • 150
  • 3
    Why does it seem like a bad idea? What's wrong with one file per class or struct? – rmaddy Nov 08 '18 at 06:48
  • It seems bad idea because I am not an expert and it seems too easy to use this architecture while similary to share data among classes you have to create a singletone class (so why the singleton idea exist ?) – Curnelious Nov 08 '18 at 06:56
  • 1
    Huh? Whether a struct is defined in its own file or the file containing other classes and/or structs has absolutely nothing to do with using singletons or sharing data. They are not even remotely related concepts. – rmaddy Nov 08 '18 at 06:59
  • @Curnelious the number of files you create for structs should be based on convenience. I usually keep only one struct or class per file. – Rakesha Shastri Nov 08 '18 at 07:01
  • thanks, I guess I have to learn more. with me it is strange that I don't have to do the hard work of creating an instance, just using the struct name where ever I want (unlike class) – Curnelious Nov 08 '18 at 07:09

1 Answers1

5

then the structure is visible to anyone

That is not true. Since your struct is not marked public, only code in your module can access it. Even if you write it in one single file, it is still accessible anywhere in your module.

without even using the Swift file name.

The reason why you are saying this might be because in other languages, you need to import a header file or something like that if you want to use something from another file (I'm not an expert in "other languages"). But Swift organises its code in units of modules, not files.

It seems like a bad idea - why ?

It is not a bad idea. Putting different types in different files is a good way to organise your code. When I go to Car.swift I wouldn't expect to see the class Game.

How is it different from a Singeltone to share data between classes? (value type?)

Here you are just writing things in different files. As far as the compiler is concerned, this is not much different from writing everything in a single file because Swift organises code in modules, not files. The Singleton pattern is something completely different. It is when you only have one shared instance of a type.

What is the right place to put the structure outside the Main class to get some clear code ?

In another file, because Main should really be in its own file.

Should I make one file with many not related Structs ?

No. That is a bad way of organising your code. When you want to find a particular struct, how do you know which file it is in?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thanks ! just didn't understand "only code in your module can access it. " and if a file is a module, it seems wrong because the file/module Dimensions contain my struct, and the file/module Main can access it and it works just fine. What did I miss? – Curnelious Nov 08 '18 at 07:19
  • @Curnelious A file is not a module. See this [question](https://stackoverflow.com/questions/48426344/what-does-module-mean-in-swift). – Sweeper Nov 08 '18 at 07:21
  • "Swift organises its code in units of modules, not files." so what does it means? when I do File->New File->Swift File , then gives it a name (a class, inherited from UIView or just an empty file), how whould you call it ? – Curnelious Nov 08 '18 at 07:23
  • 1
    @Curnelious I don't mean that Swift files don't exist. I just mean that the Swift compiler don't care about files most of the time - whether you put all your code in one file or 10 files. An exception is the access modifier `fileprivate` which makes the type/function/property only accessible within the same file. – Sweeper Nov 08 '18 at 07:28