Learn Courses My Dashboard

Using an array from a different file

Please help.
I just want to separate a long array into a different file to clean up my code but I keep getting errors.
So this the array in my code import SwiftUI

class ContentViewModel: ObservableObject {

var x:ROSList

@Published var originalItems1 = x.List

var words: [String] {

Array(originalItems1. prefix (wordCount))

}

And this is the file I created to hold the array

import Foundation

struct ROSList {

var List = [“well-nurished”, “Agitated”, “equal”, “thin”, “ones”, “the”, “misfits”, “the”, “rebels”, “the”, “troublemakers”, “the”, “round”, “pegs”, “in”, “the”, “square”, “holes”, “the”, “ones”, “who”, “see”, “things”, “differently”, “they’re”, “not”, “fond”, “of”, “rules”, “You”, “can”, “quote”, “them”, “disagree”, “with”, “them”, “glorify”, “or”, “vilify”, “them”, “but”, “the”, “only”, “thing”, “you”, “can’t”, “do”, “is”, “ignore”, “them”, “because”, “they”, “change”, “things”, “they”, “push”, “the”, “human”, “race”, “forward”, “and”, “while”, “some”, “may”, “see”, “them”, “as”, “the”, “crazy”, “ones”, “we”, “see”, “genius”, “because”, “the”, “ones”, “who”, “are”, “crazy”, “enough”, “to”, “think”, “that”, “they”, “can”, “change”, “the”, “world”, “are”, “the”, “ones”, “who”, “do”]

}

I get the error “Cannot use instance member ‘x’ within property initializer; property initializers run before ‘self’ is available”

and also “Class ‘ContentViewModel’ has no initializers” above this

Why? Todd

First things first:

When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

class ContentViewModel: ObservableObject {
    
    var x:ROSList
    
    @Published var originalItems1 = x.List
    var words: [String] {
        
        Array(originalItems1. prefix (wordCount))
        
    }
}
import Foundation

struct ROSList {
    
    var List = [“well-nurished”, “Agitated”, “equal”, “thin”, “ones”, “the”, “misfits”, “the”, “rebels”, “the”, “troublemakers”, “the”, “round”, “pegs”, “in”, “the”, “square”, “holes”, “the”, “ones”, “who”, “see”, “things”, “differently”, “they’re”, “not”, “fond”, “of”, “rules”, “You”, “can”, “quote”, “them”, “disagree”, “with”, “them”, “glorify”, “or”, “vilify”, “them”, “but”, “the”, “only”, “thing”, “you”, “can’t”, “do”, “is”, “ignore”, “them”, “because”, “they”, “change”, “things”, “they”, “push”, “the”, “human”, “race”, “forward”, “and”, “while”, “some”, “may”, “see”, “them”, “as”, “the”, “crazy”, “ones”, “we”, “see”, “genius”, “because”, “the”, “ones”, “who”, “are”, “crazy”, “enough”, “to”, “think”, “that”, “they”, “can”, “change”, “the”, “world”, “are”, “the”, “ones”, “who”, “do”]
    
}

Much nicer, even with the array trailing off screen (which can be fixed by putting in some linebreaks).


Now, to your problems…

  1. Error “Cannot use instance member ‘x’ within property initializer; property initializers run before ‘self’ is available”

This is because of these two lines:

var x:ROSList
@Published var originalItems1 = x.List

You are trying to initialize originalItems1 using a property of your class. That won’t work. All of your class properties have to be initialized before you can use the class, and x has not been initialized yet. Even if you add code to initialize x, though, it still won’t work because you can’t use the value of x until originalItems1 has been initialized. It’s a chicken and egg situation.

  1. Error “Class ‘ContentViewModel’ has no initializers”

Every property has to be initialized before a class (or struct) is ready to use. To give everything a value, you either have to assign one in an initializer or give the property a default value. In your code, x isn’t given a default value so it would need to be initialized in an init method. But, as the error is telling you, you have no init method(s).

Here’s how I would solve your dilemma…

struct ROSList {
    static let list = [...] //fill in with your list items
}
class ContentViewModel: ObservableObject {
    //leaving out everything else to make this example smaller
    @Published var originalItems1: [String] = ROSList.list
}

Doing it this way means originalItems1 doesn’t depend on x being initialized in order to get its initial value. Instead, it relies on a static property of the ROSList struct to get its value.

Of course, this may or may not work for your larger project since I have no idea what you are doing elsewhere in your code, but hopefully it provides some guidance.

It worked! And even better, I understand why. Thank you! Todd