Swift中的struct数组初始化

时间:2014-06-24 03:47:22

标签: swift

继续在Swift中玩游戏并尝试从C世界转换某些东西并且一直在试图尝试各种语法。我有一些我想要初始化为结构数组的固定数据。这是我在C中如何做到这一点,但我无法在Swift中弄清楚,所以不要再猜测我会问那些了解更多的人。这是我的C代码。

#include <stdio.h>

typedef struct my_data {
  const char *company;
  const char *city;
  const char *state;
  float latitude;
  float longitude;  
} my_data;

int main() {

    my_data data[2]={
        { "Joes Crab Shack", "Miami", "FL", 30.316599, -119.050254},
        { "Jims Crab Shack", "Los Angeles", "CA", 35.316599, -112.050254}
    };
}

在Swift中我可以创建一个类似的结构......

struct my_data {
    var company = String();
    var city = String();
    var state = String();
    var latitude:Float;
    var longitude:Float;
}

现在我陷入了如何声明和初始化固定数据的过程中,就像我在C中所做的那样。猜测它很简单并且语法正确让我感到困惑。我想将初始化样式保持为与C类似的格式,因为我可以轻松地从文件中提取和格式化这些数据。

6 个答案:

答案 0 :(得分:19)

一个选项可能是您使用元组数组:

var data = Array<(company: String, city: String, state: String, latitude: Float, longitude: Float)>()

现在,元组的各个元素都被标记,并且可以被这些标签访问,即使它们不是用于创建元组实例的第一个:

var datumOne = ("MyCompany", "MyCity", "MyState", 40.2, 139.45)
data += datumOne
println(data[0].state)      // MyState

data = [
    ( "Joes Crab Shack", "Miami", "FL", 30.316599, -119.050254),
    ( "Jims Crab Shack", "Los Angeles", "CA", 35.316599, -112.050254)
]
println(data[1].company)    // Jims Crab Shack

然而,这样做并没有给你一个结构中的类型优点...在Swift中所有结构都会自动获得所谓的“成员初始化器”,这需要你像这样初始化它们,使用成员名称作为参数标签(以及在结构中声明它们的顺序):

var myDatum = MyData(company: "The Company", city: "The City", state: "The State", latitude: 90.2, longitude: 140.44)

您可以为结构定义自己的init()方法,但是,如果您想使用更适合您目的的初始化程序。

所以,使用默认的成员初始化程序给自己一个结构数组,你只需要做:

let allMyData = [
    MyData(company: "Jims Crab Shack", city: "Los Angeles", state: "CA", latitude: 35.316599, longitude: -112.050254),
    MyData(company: "Joes Crab Shack", city: "Miami", state: "FL", latitude: 30.316599, longitude: -119.050254)
]

答案 1 :(得分:15)

像这样(注意我改变了你的结构名称以匹配Swift风格指南):

struct MyData {

  var company = String()
  var city    = String()
  var state   = String()

  var latitude:  Float
  var longitude: Float
}

let data = [
  MyData(company: "Joes Crab Shack", city: "Miami", state: "FL", latitude: 30.316599, longitude: -119.050254),
  MyData(company: "Jims Crab Shack", city: "Los Angeles", state: "CA", latitude: 35.316599, longitude: -112.050254)
]

答案 2 :(得分:5)

你写的

j.s.com:

  

var myStruct:TheStruct = TheStruct

但我已经尝试过但不起作用。 对我来说这个工作很好:

  

var myStruct:[TheStruct] = []

答案 3 :(得分:3)

struct MyData {

    var company = String()
    var city = String()
    var state = String()
    var latitude:Float
    var longitude:Float

}

var dataArray = [MyData]()

var data = MyData(company: "Joes Crab Shack", city: "Miami", state: "FL", latitude: 30.316599, longitude: -119.050254)

//可以像这样添加结构

dataArray.append(data)

//或者喜欢这个

dataArray.append(MyData(company: "Jims Crab Shack", city: "Los Angeles", state: "CA", latitude: 35.316599, longitude: -112.050254))

//获取元素

var thisCompany = dataArray[0].company // Joes Crab Shack

//更改元素

dataArray[0].city = "New York"

var thisCity = dataArray[0].city // New York

答案 4 :(得分:1)

这里有一个小例子如何处理Swift中的struct数组:

首先是结构的定义:

struct TheStruct
{
    var Index: Int = 0
    var Name: String = ""
    var Sample: [String] = [String]()
}
var myStruct: TheStruct = [TheStruct]()

然后将一个元素附加到结构

func Append1Element()
{
    myStruct.append(TheStruct(Index: 0, Name: "", Sample: []))
}

更改现有元素:

func ChangeName(theName: String)
{
    myStruct[0].Name: theName
}

答案 5 :(得分:0)

struct _data {

    var company = String();
    var city = String();
    var state = String();
    var latitude:Float;
    var longitude:Float;

}

//To initialize
var MyData = [_data()];
//To access
MyData[index].company or .city etc..
//To Add new element
MyData.append(_data(company: theCompany, city: theCity, state: theState, latitude: theLatitude, longitude: theLongitude));
//To count
MyData.count

干杯