将NSStrings列表分成段落。甚至可能是双倍间距?

时间:2014-07-24 16:55:06

标签: ios objective-c

我有一个documentInteractionController,可以让我在我的应用中分享信息;比如访问者信息,他们正在使用的操作系统等等。我创建了这个NSString的数组,我希望它们用段落分隔。所以不要像这样出现:

Visitor Location Miami FL currentURL apple.com

而不是:

Visitor Location    
Miami FL

currentURL
apple.com

我该怎么做?使字符串全部用段落分隔?

NSArray *visitorInfoArray = [NSArray arrayWithObjects:@"Visitor Location", _visitor.location, @"currentURL", _visitor.currentURL, nil];
NSString *visitorInfo = [visitorInfoArray componentsJoinedByString:@" "];

NSString *filename = @"User Info";
NSURL *tempURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:filename]];
[visitorInfo writeToURL:tempURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
[MTq main:^{
    [FCIProgress dismiss];
    DLog(@"%@", visitorInfo);
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:tempURL];
    self.documentInteractionController.UTI = @"public.plain-text";
    [self.documentInteractionController presentOptionsMenuFromBarButtonItem:sender animated:YES];
}];

1 个答案:

答案 0 :(得分:0)

如果您需要以特定方式制作字符串,您可能不想使用NSArray的{​​{1}}方法。您想要使用componentsJoinedByString' s NSString。此外,如果您需要在此方法后的任何位置使用您的数组信息,我强烈建议您使用字典。如果您不再使用它,则根本不需要创建它。

制作输出字符串:

stringWithFormat

如果您打算重新使用NSString *visitorInfo = [NSString stringWithFormat:@"Visitor Location\n%@\n\ncurrentURL\n%@", _visitor.location, _visitor.currentURL]; 中的信息,请让它放在NSArray中,以便您可以通过NSDictionary引用值,而不是记住索引

key

您现在可以像这样访问您的值:NSDictionary *visitorInfoDict = [NSDictionary dictionaryWithObjectsAndKeys: _visitor.location, @"visitorLocation", _visitor.currentURL, @"currentURL", nil];