iOS5 NSMutableDictionary 有效转换为逗号分隔的值列表

发布于 2024-12-11 05:15:49 字数 543 浏览 0 评论 0原文

我在一个应用程序中获得了大约 30000 行数据,我想将其添加到 CSV 文件中并通过电子邮件发送给用户。当前每个数据点都有大约 20 个属性,我将添加大约 20 个值作为扩展属性,存储在 NSMutableDictionary 中。这是大量数据,创建文件并将其附加到电子邮件已经需要约 10 秒的时间。

我可以使用常规属性构建一串 CSV 值,

[NSString stringWithFormat:@"%f,%f,%f",prop1,prop2,prop3 ];

我可以像这样迭代字典。我可以为每个键重复构建一个字符串,但这每次都会重新创建一个新字符串。 iOS 中是否有 StringBuilder 或 StringBuffer 的等效项? 将

for(NSString *aKey in myDictionary){

    NSLog(aKey);
   //append string

}

NSMutableDictionary 中的现有属性和扩展属性组合起来以创建单个逗号分隔值字符串的最佳方法是什么?

I got ~30000 rows of data in an app that I would like to add to a CSV file and email to the user. Each data point currently has ~20 properties and I'm adding ~20 more values as extended attributes, stored in a NSMutableDictionary. That's a lot of data, and it already takes ~10 seconds to create the file and attach it to an email.

I can build a string of CSV values from regular properties using

[NSString stringWithFormat:@"%f,%f,%f",prop1,prop2,prop3 ];

I can iterate the dictionary like this. I can repeatedly build a string for each key, but this would re-create a new string each time. Is there an equivalent of StringBuilder or StringBuffer in iOS?

for(NSString *aKey in myDictionary){

    NSLog(aKey);
   //append string

}

What's the best way to combine existing properties and extended attributes from an NSMutableDictionary to create a single string of comma separated values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-12-18 05:15:49

在 for 循环外部初始化一个可变字符串并根据需要附加它。

NSMutableString *large_CSV_String = [[NSMutableString alloc] init];
for(NSString *aKey in myDictionary){

    // Add something from the key?? Your format here.
    [large_CSV_String appendFormat:@",%@",aKey];

}
NSLog(@"%@",large_CSV_String);

Initialize a mutable string outside the for loop and append it however you want.

NSMutableString *large_CSV_String = [[NSMutableString alloc] init];
for(NSString *aKey in myDictionary){

    // Add something from the key?? Your format here.
    [large_CSV_String appendFormat:@",%@",aKey];

}
NSLog(@"%@",large_CSV_String);
何必那么矫情 2024-12-18 05:15:49

这篇文章很旧,但对于任何寻找更清洁解决方案的人来说,你可以这样做:

NSArray *keys= [myDictionary allKeys];
NSString *csv = [keys componentsJoinedByString:@", "];

This post is old but for anyone looking for a cleaner solution you could do this:

NSArray *keys= [myDictionary allKeys];
NSString *csv = [keys componentsJoinedByString:@", "];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文