从类方法中查找类的实例

时间:2016-01-07 20:46:33

标签: swift

我正在尝试创建一个类方法,该方法可选地返回该类的(已经实例化的)实例。

我正在考虑将实例化类的内存地址写入用户默认密钥,然后尝试返回该地址的对象,但我不确定这是否正确方法或如何做到这一点。

class MyClass {

    let myProperty: String

    required init(myProperty: String) {
        self.myProperty = myProperty
    }

    class func currentClass() -> MyClass? {
        return nil
    }
}

let aNewClass = MyClass(myProperty: "Hi")

// Should return the aNewClass instance:
MyClass.currentClass()

1 个答案:

答案 0 :(得分:3)

您需要创建一个静态属性,将已创建的成员分配给:

Results.aspx

这有点类似于单例模式,您始终可以从类中检索相同的对象。您也可以删除作业周围的protected void Page_Load(object sender, EventArgs e) { if(!String.IsNullOrEmpty(Request.QueryString["srch"])) { String srch = Request.QueryString["srch"]; var myControl = (TextBox)Master.FindControl("search"); myControl.Text = srch; //perform search and display results String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString(); conn = new MySql.Data.MySqlClient.MySqlConnection(connString); conn.Open(); queryStr = ""; queryStr = "SELECT g.*,CONCAT(firstname,' ',lastname) as name FROM app_groups.groups g LEFT JOIN users u ON u.id = g.id_user WHERE group_name LIKE '%" + srch + "%' OR group_type LIKE '%" + srch + "%'"; cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn); reader = cmd.ExecuteReader(); gname = "";//group name gtype = "";//group type uname = "";//user name id = ""; warning = ""; id_owner = ""; if (reader.HasRows) { while (reader.HasRows && reader.Read()) { uname = reader.GetString(reader.GetOrdinal("name")); gname = reader.GetString(reader.GetOrdinal("group_name")); gtype = reader.GetString(reader.GetOrdinal("group_type")); id = reader.GetString(reader.GetOrdinal("id")); id_owner = reader.GetString(reader.GetOrdinal("id_user")); warning = reader.GetString(reader.GetOrdinal("warning")); Panel pan = new Panel(); pan.CssClass = "col-sm-4"; Panel subpan = new Panel(); subpan.CssClass = "group"; if (Session["uid"] != null) { int sess_id = Convert.ToInt32(Session["uid"]); if ((Session["uid"] != null && Session["uadmin"].Equals("1")) || sess_id.Equals(Int32.Parse(id_owner))) { adminCtrls = new Panel(); adminCtrls.CssClass = "adminCtrls"; btn = new Button();//delete group button for admins and owners only btn.Click += new EventHandler(deleteGroup); btn.ID = "sterge"; btn.CssClass = "btn btn-danger btn-xs"; btn.Text = "Sterge grup"; btn.CommandArgument = id; //btn.Attributes.Add("style", "float:right;"); adminCtrls.Controls.Add(btn); if (Session["uadmin"].Equals("1")) { if (!warning.Equals("1")) { btn = new Button();//warn owner button for admins only btn.Click += new EventHandler(warnOwner); btn.ID = "warn"; btn.CssClass = "btn btn-warning btn-xs"; btn.Text = "Avertizeaza owner grup"; btn.CommandArgument = id; adminCtrls.Controls.Add(btn); } else { btn = new Button();//warn owner button for admins only btn.ID = "warn"; btn.Enabled = false; btn.CssClass = "btn btn-warning btn-xs"; btn.Text = "A fost avertizat"; adminCtrls.Controls.Add(btn); } } } else { adminCtrls = null; } if (adminCtrls != null) { subpan.Controls.Add(adminCtrls); } } subpan.Controls.Add(new LiteralControl("<span class='title text-center'>" + gname + "</span><br/>")); subpan.Controls.Add(new LiteralControl("<span>Owner: <span class='owner'>" + uname + "</span></span><br/>")); subpan.Controls.Add(new LiteralControl("<span>Categorie: <span class='type'>" + gtype + "</span></span><br/>")); pan.Controls.Add(subpan); groupsShow.Controls.Add(pan); } } reader.Close(); conn.Close(); } } ,使Class始终返回最新的实例,如果最适合您的需要。