iOS中ArrayList的替代选择?

时间:2016-02-16 12:25:07

标签: ios objective-c arrays

我已经解析了一个导致元素数组的JSON。我希望将它存储在一个数组中,就像我们将元素动态添加到arraylist并稍后访问它一样。在iOS的情况下,ArrayList的替代方案是动态添加元素并在以后检索它。 iOS中是否还有其他可用于动态存储和检索元素的选项?

数据1: 参考编号:1241 InvoiceNumber:1421241 InvoiceDate:06-FEB-16

数据2: 参考编号:3241 InvoiceNumber:12421 InvoiceDate:06-FEB-16

以上是我通过解析json得到的具有独特元素的两个数据。

在A类中,我添加以下代码将nsmutablearray存储在nsuserdefaults中,

for (NSUInteger i = 0; i < savecount; i++) {
    NSMutableArray *newArray = [[NSMutableArray alloc] init];
    [myArrays addObject:newArray];
    [newArray addObject:shipmentReferenceNumber];
    [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"shipmentReferenceNumber "];
}

在B类中,我尝试使用以下代码访问它,但它返回null

NSString *referenceNumber = [[NSUserDefaults standardUserDefaults] stringForKey:@"referenceNumber"];

5 个答案:

答案 0 :(得分:1)

如果数组是可编辑的,则可以使用NSMutableArray,否则使用NSArray。

答案 1 :(得分:1)

如果我误解了你想要实现的目标,请原谅我,但我会考虑创建一个数据模型对象:

@interface Data : NSObject
@property (nonatomic) NSInteger referenceNumber;
@property (nonatomic) NSInteger invoiceNumber;
@property (strong, nonatomic) NSString *invoiceDate;
@end

然后,您可以创建一个数据对象数组:

@property (strong, nonatomic) NSMutableArray <Data *> *dataArray;

答案 2 :(得分:1)

Hash MapNSArray一起使用。使用以下代码。

在A类中进行存储:

    NSMutableArray* arrayList = [[NSMutableArray alloc] init];
    for (int i=0; i<savecount; i++) {

        NSDictionary* innerDict = [NSDictionary dictionaryWithObjectsAndKeys:your_referenceNo,@"referenceNo",your_InvoiceNumber,@"invoiceNumber",your_InvoiceDate,@"invoiceDate", nil];

        [arrayList addObject:innerDict];
    }

    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:arrayList];
   [[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:@"data"];

在B级巡回时:

    NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"data"];
    NSMutableArray* arrayList = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"Reference No : %@",arrayList[0][@"referenceNo"]);

修改以动态访问arraylist

   for(int i=0; i<[arrayList count];i++){
     NSLog(@"Reference No : %@",arrayList[i][@"referenceNo"]);
   }

由于

答案 3 :(得分:0)

在objC和swift中,ArrayListNSMutableArray

hash map

提供NSDictionary类功能(商店键值对。键是唯一的)

答案 4 :(得分:0)

这里有一个数据结构,它动态增加并通过索引获取,基本上,这是一个普通的链表类,内部有一个动态数组来提供按索引获取数据,它复制了空间,但是您可以删除链表更新数组后,请参见代码:

package com.sunflower.visitor.data.xml;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import com.sunflower.visitor.data.tempholder.FactureDataHolder;
import com.sunflower.visitor.dto.Article;
import com.sunflower.visitor.dto.FactureArticle;
import com.sunflower.visitor.dto.FactureHeader;
import com.sunflower.visitor.util.FileUtil;
import org.xmlpull.v1.XmlSerializer;

import java.io.*;
import java.util.List;

/**
 * Created by Joe on 13/02/2016.
 */
public class XmlStorage {
    public static String saveXmlFacturePdfReportArticle(Activity activity, FactureHeader factureHeader, List<Article> factureArticles){

        final String xmlFile = activity.getExternalFilesDir(null) + "/reports/resources" + "/facture.xml";
        try {
            FileOutputStream fileos = new FileOutputStream (new File(xmlFile));
            XmlSerializer xmlSerializer = Xml.newSerializer();
            StringWriter writer = new StringWriter();
            xmlSerializer.setOutput(writer);
            xmlSerializer.startDocument("UTF-8", true);
            xmlSerializer.startTag(null, "facture");

            for(int i=0; i<factureArticles.size(); i++) {

                xmlSerializer.startTag(null, "article");

                xmlSerializer.startTag(null, "date");
                xmlSerializer.text(factureHeader.getFactureDate());
                xmlSerializer.endTag(null, "date");
                Log.d("article", "factureHeader.getFactureDate() : " + factureHeader.getFactureDate());

                xmlSerializer.startTag(null, "facture_number");
                xmlSerializer.text("\u202E" + factureHeader.getFactureNumber());
                xmlSerializer.endTag(null, "facture_number");

                xmlSerializer.startTag(null, "customer");
                xmlSerializer.text(factureHeader.getCustomerName());
                xmlSerializer.endTag(null, "customer");

                xmlSerializer.startTag(null, "comment");
                xmlSerializer.text(factureHeader.getComment());
                xmlSerializer.endTag(null, "comment");

                xmlSerializer.startTag(null, "row");
                xmlSerializer.text("" + (i + 1));
                xmlSerializer.endTag(null, "row");

                xmlSerializer.startTag(null, "article_name");
                xmlSerializer.text(factureArticles.get(i).getArticleName());
                xmlSerializer.endTag(null, "article_name");

                xmlSerializer.startTag(null, "orders");
                xmlSerializer.text(factureArticles.get(i).getOrders() + "");
                xmlSerializer.endTag(null, "orders");

                xmlSerializer.startTag(null, "unit");
                xmlSerializer.text(factureArticles.get(i).getUnitName());
                xmlSerializer.endTag(null, "unit");

                xmlSerializer.startTag(null, "unit_number");
                xmlSerializer.text(factureArticles.get(i).getUnitNumberInDouble() + "");
                xmlSerializer.endTag(null, "unit_number");

                xmlSerializer.startTag(null, "fee");
                xmlSerializer.text(factureArticles.get(i).getSelectedPrice() + "");
                xmlSerializer.endTag(null, "fee");

                xmlSerializer.startTag(null, "sum_fee");
                xmlSerializer.text("" + (factureArticles.get(i).getOrders() * factureArticles.get(i).getUnitNumberInDouble() * factureArticles.get(i).getSelectedPrice()));
                xmlSerializer.endTag(null, "sum_fee");

                xmlSerializer.startTag(null, "row_off");
                xmlSerializer.text(factureArticles.get(i).getOffPercent());
                xmlSerializer.endTag(null, "row_off");

                xmlSerializer.startTag(null, "levy");
                xmlSerializer.text(factureArticles.get(i).getConfigurableLevy());
                xmlSerializer.endTag(null, "levy");

                xmlSerializer.startTag(null, "jogs");
                xmlSerializer.text(factureArticles.get(i).getConfigurableJogs());
                xmlSerializer.endTag(null, "jogs");

                xmlSerializer.startTag(null, "factureprice");
                xmlSerializer.text(FactureDataHolder.getFacturePrice() + "");
                xmlSerializer.endTag(null, "factureprice");

                xmlSerializer.endTag(null, "article");
            }
            xmlSerializer.endTag(null, "facture");
            xmlSerializer.endDocument();

            xmlSerializer.flush();
            String dataWrite = writer.toString();
            fileos.write(dataWrite.getBytes());
            fileos.close();

            return xmlFile;
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

        if (null != extraFolder) {
            repo.addExtraReportFolder(resourceFolder + RepositoryManager.PATH_DELIMITER + extraFolder);
        }
        repo.addExtraReportFolder(resourceFolder);

        PdfReporter reporter = new PdfReporter(jrxmlPath, getOuputPdfFolder(), getFilenameFromJrxml(jrxmlPath));

        return  reporter;
    }

如果您使用它,请参阅我的著作https://www.liebertpub.com/doi/10.1089/big.2018.0064