Accessing a Struct that is placed in a separate swift file

This probably should have been placed in the Swift Discussion Area because I didn’t make clear that is language I’m using.

When creating a Struct inside it’s own .swift file, what do I need to do have total access to it inside of a function in my ViewController.swift file? I assume I have to initialize it somehow inside the ViewController.swift file? I keep getting an “Expected Declaration Error” and when I click on the error it jumps me up to the “class ViewController: UIViewController {” line and a "gray colored error appears that reads, “1. In declaration of ‘ViewController’” Thank you!!!

Hi @dukeobass, generally I just call the struct name inside the function I want it in. For example, if I create a .swift file and call it Name. Then I can create a struct called Name.

import Foundation

struct Name {

var myName: String

}

Then in my other file, I can declare the struct. One way could be creating a variable within the function then calling Name. Ex:

func myFunction() {

var myNameIs = Name(myName: “Duke”)

}

Hope this helps! Happy coding,
Gina

Hi Gina,

I just solved it! I looked over the function where I return the struct with value embedded in it and the call to the struct was outside of the functions last curly brace.

I feel so dumb!!!

Thank you for your fast reply! I REALLY appreciated hearing back from someone so soon.

Don

Your welcome, glad you were able to figure it out!

Best,
Gina

Happy Thanksgiving and thank you very much, again!

Don

This topic was automatically closed after 166 days. New replies are no longer allowed.