当我将元素追加到全局数组时,将其替换为先前的元素(奇怪的行为)?

时间:2018-11-27 10:03:26

标签: ios arrays swift

    if products == nil {
        products = [Product]()
        products!.append(product!)

        arr.append(product)
    }else{
        products!.append(product!)

        arr.append(product)
    }

例如,如果我想在购物车中复制具有不同属性的商品,例如更改大小或数量,请按按钮...但是我得到的是添加正确但具有数量的商品所有产品的最终产品属性相同!任何迪亚?

1 个答案:

答案 0 :(得分:0)

您似乎使用了相同的对象{ SqlConnection myADONETConnection = new SqlConnection(); myADONETConnection = (SqlConnection)(Dts.Connections["xxxxxxxxx"].AcquireConnection(Dts.Transaction) as SqlConnection); // MessageBox.Show(myADONETConnection.ConnectionString, "xxxxxxxxxx"); string line1 = ""; //Reading file names one by one string SourceDirectory = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // TODO: Add your code here string[] fileEntries = Directory.GetFiles(SourceDirectory); foreach (string fileName in fileEntries) { // do something with fileName // MessageBox.Show(fileName); string columname = ""; //Reading first line of each file and assign to variable System.IO.StreamReader file2 = new System.IO.StreamReader(fileName); string filenameonly = ((((fileName.Replace(SourceDirectory, "")).Replace(".txt", "")).Replace("\\", "")).Replace("-", "_")); line1 = (" IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]." + filenameonly + "') AND type in (N'U'))DROP TABLE [dbo]." + filenameonly + " Create Table dbo." + filenameonly + "([" + file2.ReadLine().Replace("|", "] NVARCHAR(500),[") + "] NVARCHAR(500))").Replace(".txt", ""); file2.Close(); // MessageBox.Show(line1.ToString()); SqlCommand myCommand = new SqlCommand(line1, myADONETConnection); myCommand.ExecuteNonQuery(); // MessageBox.Show("TABLE IS CREATED"); //Writing Data of File Into Table int counter = 0; string line; System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName); while ((line = SourceFile.ReadLine()) != null) { if (counter == 0) { columname = line.ToString(); columname = "[" + columname.Replace("|", "],[") + "]"; // MessageBox.Show(columname); // MessageBox.Show("INside IF"); } else { // MessageBox.Show("Inside ELSE"); while ((line = SourceFile.ReadLine()) != null) { line = line.Trim(); if (line.Length > 0) { string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','") + "')"; SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection); myCommand1.ExecuteNonQuery(); } } } counter++; } SourceFile.Close(); } Dts.TaskResult = (int)ScriptResults.Success; } 并更改了它的属性,然后重新添加它,因为它是引用类型,所以不正确,您需要创建产品

product!