0

In a class I created I have a struct called Image:

public struct Image {


 private(set) var url: String
 private(set) var crop: CGRect

    init(url: String, crop: CGRect) {
        self.url = url
        self.crop = crop
    }
}

I also have a function that usesImage as a parameter.

public func detectBoxes(image: Image) {
    //some code
}

What I want to do is access Image in another file to use it in a function. So, I create an instance of the class and try typing

instance.detectBoxes(image: ...)

BUT it won't let me create an instance of Image so I can use it, it just wants me to type Class.Image which doesn't make sense.

Does anyone know how to solve my issue? Any help would be much appreciated. Cheers, Theo

2
  • Is your struct declared inside the class declaration (i.e. a nested declaration)? Or are they just defined in the same file? Commented Jul 17, 2017 at 16:30
  • @paulvs inside the class Commented Jul 17, 2017 at 16:30

1 Answer 1

1

You defined a nested struct inside of a class, meaning it's available from within the class context.

If you wish to just use an instance of type Image and not <ClassName>.Image you must move the declaration of the struct out side the scope of the class.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks so much! I did what you suggested but when I try to make an instance of it in my other file by doing var imageInstance = Image( it doesn't autocomplete to be Image(url: String, crop: CGRect). Do you know what's up with that. Once I get that working I'm golden. I'll definitely give you the check mark if you can fix that
i realized that my init() function wasn't public, so I made it so, but it still doesn't work. I'm getting the error when I type var imageInstace = Image(url: "blah", crop: "blah"), 'Image' initializer is inaccessible due to 'internal' protection level
Just an observation, you don't have to declare "default" init for structs, only classes. If you writestruct A { var x = 0 } it automatically has a A(x: Int) init.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.