在数组中声明类型?

时间:2015-09-30 14:30:22

标签: arrays class swift2

我想在数组声明中创建一个类实例。怎么做的?

类似于以下内容:

s

数组类型为:

        <div class="col-lg-10">
            <div class="radio">
                <label>
                    <asp:RadioButtonList ID="category" runat="server">
                        <asp:ListItem Text="Student" Value="student"></asp:ListItem>
                        <asp:ListItem Text="Staff" Value="staff"></asp:ListItem>
                    </asp:RadioButtonList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server" ControlToValidate="category" ErrorMessage="Category is Requried" Style="color: #58CB00"></asp:RequiredFieldValidator>
                </label>
            </div>
        </div>
    </div>

    <div class="Department">
        <asp:Label ID="Label3" runat="server" Text="Department*" CssClass="col-lg-2 control-label" Font-Size="Larger"></asp:Label><br />
        <div class="col-lg-10">
            <asp:DropDownList ID="DropDownList1" runat="server" CssClass="form-control ddl">
                <asp:ListItem>Select your department</asp:ListItem>
                <asp:ListItem>Administration</asp:ListItem>
                <asp:ListItem>IT Solutions</asp:ListItem>
                <asp:ListItem>B.Tech(CSE)</asp:ListItem>
                <asp:ListItem>MCA</asp:ListItem>
                <asp:ListItem>iMBA</asp:ListItem>
                <asp:ListItem>English</asp:ListItem>
                <asp:ListItem>Library</asp:ListItem>
                <asp:ListItem>Others</asp:ListItem>

            </asp:DropDownList>
        </div>
    </div>
    <br />

    <div class="Designation">
        <asp:Label ID="Label4" runat="server" Text="Designation*" CssClass="col-lg-2 control-label" Font-Size="Larger"></asp:Label>
        <div class="col-lg-10">
            <asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox2" ErrorMessage="Designation is Requried" Style="color: #58CB00"> </asp:RequiredFieldValidator>

        </div>
    </div>

    <div class="Course">
        <asp:Label ID="Label5" runat="server" Text="Course*" CssClass="col-lg-2 control-label" Font-Size="Larger"></asp:Label>
        <div class="col-lg-10">
            <asp:DropDownList ID="DropDownList2" runat="server" CssClass="form-control ddl">
                <asp:ListItem>B.Tech(CSE)</asp:ListItem>
                <asp:ListItem>MCA</asp:ListItem>
                <asp:ListItem>iMBA</asp:ListItem>
                <asp:ListItem>English</asp:ListItem>
            </asp:DropDownList>
        </div>
    </div>
    <br />
    <div class="Semester">
        <asp:Label ID="Label6" runat="server" Text="Semester*" CssClass="col-lg-2 control-label" Font-Size="Larger"></asp:Label>
        <div class="col-lg-10">
            <asp:DropDownList ID="DropDownList3" runat="server" CssClass="form-control ddl">
                <asp:ListItem>I</asp:ListItem>
                <asp:ListItem>II</asp:ListItem>
                <asp:ListItem>III</asp:ListItem>
                <asp:ListItem>IV</asp:ListItem>
                <asp:ListItem>V</asp:ListItem>
                <asp:ListItem>VI</asp:ListItem>
                <asp:ListItem>VII</asp:ListItem>
                <asp:ListItem>VIII</asp:ListItem>
                <asp:ListItem>IX</asp:ListItem>
                <asp:ListItem>X</asp:ListItem>
            </asp:DropDownList>
        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

您的数组类型为ClassB。您还必须为ClassB创建初始值设定项。例如:

class ClassB: NSObject {
    var name:String?
    var deptId:Int?
    init(name: String, deptId: Int) {
        self.name = name
        self.deptId = deptId
    }
}

class ClassA {
    var myarray:[ClassB] = []
}

let myclassA = ClassA()

myclassA.myarray = [ClassB(name: "Cleaning", deptId: 12), ClassB(name: "Repairs", deptId: 30)]