简体   繁体   中英

How to import a custom class which is within its own folder?

Salutations,

So I was looking around the web and none of the answers really gave me what I need, I am still quite a novice to swift and as such I am used to java's rather straightforward import rules. I digress.

So my file structure is as follows: I have my project folder, in it i have a models folder which contains a file called CityData.swift. Outside of this folder all of my other view controllers sit within the projectFolder as is standard for Xcode's new project layout.

My issue is that I want to create an array of type CityData as follows:

var data:[CityData] = [CityData(cityName:"Moscow", cityHighLight:"Capital of Russia")].

However, I am getting an error saying use of undeclared type. How can I declare this type, and also am I calling the constructor correctly inside of my array. As far as I am aware, init() is how you make a constructor, and swift does not have the concept of "new".

Edit: figured it out, no need for imports, just accidentally capitalized my c is cityData. Now I am getting a new error though cannot use instance member 'cityData' within properly initalizer; property initializer run before self is available. cannot use instance member 'cityData' within properly initalizer; property initializer run before self is available.

What does this mean, and how would I call my constructor within the cityData array then?

Edit 2: Code sample:

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutler wear var cityPicker :UIPickerView!; 
    var data = [cityData]();

    override func viewDidLoad() {
         data = [cityData(cityName: "Moscow", cityHighLight:"Russian Capital")
    }

And for how the cityData class looks like:

cityData {
   var cityName:String;
   var cityHighLight:String;

   init(cityName:String, cityHighLight:String) {
      self.cityName = cityName;
      self.cityHighLight = cityHighLight;
   }

   //getter methods below since swifts get/set within a variable make 0 sense.

}

Edit: 3

VC:

import UIKit


class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{


    var lat:Double = 0.0;
    var long:Double = 0.0;

    @IBOutlet weak var cityPicker: UIPickerView!;
    @IBOutlet weak var dataFromPicker: UILabel!;


    var data = [cityData]();
    var cityData:[String] = ["Waycross", "Plains", "Athens", "Milledgeville", "Wrightsville"];
    var cityHighlights:[String] = ["Alligators", "Peanuts", "Heaven on Earth", "Mental Hospital", "Home of Herschel"];



    //number of columns
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1;
    }

    //number of rows
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return cityData.count;
    }

    //title for row
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        dataFromPicker.text = cityHighlights[row];
        return cityData[row];

    }

    override func viewDidLoad() {

        data = [
            cityData(cityName:"Moscow", cityHighLight:"Russians", cityLat: 200.0, cityLong:200.0)
        ];


        super.viewDidLoad()
        dataFromPicker.textAlignment = .center;
        self.cityPicker.dataSource = self;
        self.cityPicker.delegate = self;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

cityData.swift:

import Foundation

class cityData {
    var cityName:String;
    var cityHighLight:String;
    var cityLat:Double;
    var cityLong:Double;

    init(cityName: String, cityHighLight:String, cityLat:Double, cityLong:Double) {
        self.cityName = cityName;
        self.cityHighLight = cityHighLight;
        self.cityLat = cityLat;
        self.cityLong = cityLong;
    }
}

And for how the cityData class looks like

The problem is that cityData and CityData are two different names. Your class needs to be called CityData , and it needs to be properly declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM