How/where do I declare global variables in the same file as "main" location?
Variables declared outside of any class, struct or function, i.e. at the zero-indentation level, are ”global”.
But what, exactly, do you mean by “main”? The Swift equivalent of the “main function” in the C sense is partly explain by eoonline above, but I don’t think that’s really what you mean. I think you mean something more like “constructor”, or similar.
On a large project, there will be multiple associated files that contain related functions. How do make calls to these functions from the "main" file?
Do you understand concepts like classes, objects and methods?
In C# for example, I can't simply call the function name, it has to be associated the "class" that the function is stored in first.
OK, so you have heard of classes…
Do I have to declare these related functions in the "main" form ahead of usage?
Generally, the Swift compiler finds declarations in other files without any extra work on your part (unlike C).
But looking at your example, I think what you may be doing is creating instances of classes in order to call their methods. In that case, the answer is “yes, of course you need to create objects before you can call their (non-static) methods”.
Eoonline wrote:
Judging from your question, the problem might be less about the Swift language, itself (which I wager that you will quickly grok) than the SwiftUI framework
I agree that you should absolutely learn non-UI Swift first, and then learn SwiftUI, taking care to understand that its body declarative parts are subtly different from other Swift code.
I used "print(hi)" just to ensure that my function call was being called.
Crucially, the calls 1 and 2 are not within a function, they’re within a SwiftUI body declaration.
I’m not sure I agree with eoonline that you will “quickly grok” Swift, though. I have the impression that your first language was probably something without classes - maybe Fortran or Pascal - and you continue to think of everything in terms of those semantics.
Topic:
Programming Languages
SubTopic:
Swift