Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:在ViewController的初始化例程中使用数据模型类结构_Ios_Swift_Google Maps_Class_Structure - Fatal编程技术网

Ios Swift:在ViewController的初始化例程中使用数据模型类结构

Ios Swift:在ViewController的初始化例程中使用数据模型类结构,ios,swift,google-maps,class,structure,Ios,Swift,Google Maps,Class,Structure,我使用的是Xcode 8.3和Swift 3.0。这是我在阅读了Apple提供的教程后首次尝试使用Swift 我有一个定义类的数据模型文件: class Location: NSObject, NSCoding { //MARK: Properties var name: String var number: Int var item: String var visited: Bool var coords: [Coordinates]

我使用的是Xcode 8.3和Swift 3.0。这是我在阅读了Apple提供的教程后首次尝试使用Swift

我有一个定义类的数据模型文件:

class Location: NSObject, NSCoding {

    //MARK: Properties

    var name: String
    var number: Int
    var item: String
    var visited: Bool
    var coords: [Coordinates]

    //MARK: Types

    struct Coordinates {
        // default values are for a location
        let latitude: Double = 35.0
        let longitude: Double = -120.0
    }

    //MARK: Initialization

    init(name: String, number: Int, item: String, visited: Bool, coords: [Coordinates]) {

        // Initialize stored properties
        self.name = name
        self.number = number
        self.item = item
        self.visited = visited
        self.coords = coords
    }
}
我还有一个ViewController,我想在其中使用数据。现在,我仍在写作/学习,所以我让它加载我手工输入的数据样本:

class ViewController: UIViewController {

    //MARK: Properties
    // an array of the locations
    var locations = [Location]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load a sample location.
        locations[0] = Location(name: "foo", number: 1, item: "bar", visited: false, coords: [Location.Coordinates(latitude: 36.0, longitude: -121.0)])
        os_log("Locations successfully loaded on map screen.", log: OSLog.default, type: .debug)
    }
}
问题显然是coords:[Location.coordinates纬度:36.0,经度:-121.0],这是我在代码正常运行后添加的。Xcode给我的错误是:

无法使用参数列表“纬度:双精度,经度:双精度”调用类型“Location.Coordinates.type”的值

当我搜索这个错误时,我发现,它看起来与我非常相似。所以,我按照公认的答案做了:

我在Location类中创建了一个坐标类型的实例,在坐标定义之后添加var instanceOfCoordinates=Coordinates

我还在ViewController类中创建Location类的实例,在声明locations属性后添加let InstanceFlocation=Location.self

我将有问题的代码更新为coords:[InstanceFlocation.InstanceOffCoordinations纬度:36.0,经度:-121.0]

我得到一个不同的错误:

实例成员“InstanceOffCoordinates”不能用于类型“Location”

编辑:在coords:之后但在最后一个之前的代码部分周围添加[]不会更改错误

我想要的是能够将坐标作为一个结构保留在位置中,以便它们始终在一起,并且我可以使用点引用来获取位置[0].coords[index].纬度和位置[25].coords[index].经度等形式的值。最终,这将用于在谷歌地图上填充PIN

我确信斯威夫特有很多微妙之处,但这一次并不是这样,但通常搜索和修补都会奏效


有人能证明我做错了什么吗?

您不能简单地将空位置数组的元素0赋值;可以使用.append将新项追加到数组中

我还建议进行一些其他更改:

不要用默认值初始化坐标结构。初始化器将为您进行初始化。 坐标结构应该是坐标奇异的。
您的初始化器需要一个坐标数组,因此将[]环绕Location.coordinates,并在数值末尾添加.0,以便它们是双精度的或使用双精度36。仅供参考,将该结构设置为类的子结构将不允许您使用问题最后一部分中指定的语法。它仍然是位置[0].coords[someIndex].latitudeAlso,使用默认值初始化结构成员没有意义,您可以使用现有的CLLocationCoordinate2D,而不是重新设计轮子。感谢@Paulw11的响应,我已经做了您提到的更改。大多数是因为我编辑了文本,使其更紧凑,以便在这里显示,但它们仍然改进了我的问题。不幸的是,它们不会更改任何一个错误。删除结构中的默认值似乎解决了问题。为什么会出现这个问题?另外,你想让我把坐标换成坐标作为一种偏好,还是说这是一个敏捷的人希望看到的?它们是let常量,所以一旦它们有了一个值,你就不能再赋值了。坐标在语法上是正确的,所以读起来更好。该结构表示单个坐标,而不是多个坐标。坐标在语法上是正确的。纬度是坐标,经度是坐标,包含纬度和经度的集合是坐标集合。在15年的科学生涯中,我从来没有听说过有人用坐标来表示一组两个或更多的数字。至于let,我认为当您实例化一个结构实例时,它会忽略默认值。我不确定这是从哪里来的,但显然是错的!
class Location: NSObject, NSCoding {

    //MARK: Properties

    var name: String
    var number: Int
    var item: String
    var visited: Bool
    var coords: [Coordinate]

    //MARK: Types

    struct Coordinate {
        // default values are for a location
        let latitude: Double 
        let longitude: Double = 
    }

    //MARK: Initialization

    init(name: String, number: Int, item: String, visited: Bool, coords: [Coordinates]) {

        // Initialize stored properties
        self.name = name
        self.number = number
        self.item = item
        self.visited = visited
        self.coords = coords
    }
}

class ViewController: UIViewController {

    //MARK: Properties
    // an array of the locations
    var locations = [Location]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load a sample location.

        locations.append(Location(name: "foo", number: 1, item: "bar", visited: false, coords: [Location.Coordinates(latitude: 36.0, longitude: -121.0)]))
    }
}