在UIPasteboard中存储NSArray [英] Storing NSArray in UIPasteboard
问题描述
我有几个文本文件,我想在2个应用程序之间传输. (即同一应用的免费和付费版本).
I have several text files which I want to transfer between 2 Apps. (ie. free and paid versions of the same App).
我正在使用UIPasteboard来执行此操作.这些文件的内容作为NSArray保存在内存中,因此我想将这些NSArray复制到粘贴板(精简版),然后从粘贴板读取它们(完整版).
I'm using UIPasteboard to do this. The contents of the files are held in memory as NSArrays, and so I want to copy these NSArrays to the pasteboard (lite version), and read them from the pasteboard (full version).
由于某些原因,无法从粘贴板中读取数据.数据将作为NSData对象而不是NSArray返回,我认为这意味着它不是我正在使用的粘贴板类型的必需格式,即"public.utf8-plain-text".
For some reason the data cannot be read back from the pasteboard. The data is being returned as a NSData object, rather than NSArray, which I think means that it is not in the required format for the pasteboard type I am using, which is "public.utf8-plain-text".
当我使用这种粘贴板类型读取/写入NSString时,效果很好.
When I read/write NSStrings with this pasteboard type, it works fine.
我搜索了Apple文档等,以查看是否应该为NSArrays(或其他属性列表对象)使用其他类型,但画了一个空白.
I searched through Apple docs, etc, to see if there is a different type I should be using for NSArrays, (or other property list objects), but drew a blank.
写到粘贴板:(在下面的pDataOutput中是一个字符串数组,文件内容):
Writing to the pasteboard: (In the following pDataOutput is an array of strings, file contents) :
NSMutableArray *lArrayCopy = [gGlobalData.cPasteBoard.items mutableCopy];
[lArrayCopy replaceObjectAtIndex:pDataFileIdx
withObject:[NSDictionary dictionaryWithObject:pDataOutput
forKey:@"public.utf8-plain-text"]];
gGlobalData.cPasteBoard.items = lArrayCopy;
[lArrayCopy release];
从粘贴板上读取:
NSArray *lPBItems = [pPasteBoard valuesForPasteboardType:@"public.utf8-plain-text"
inItemSet:nil];
NSLog(@"PB Items = NSArray of count %d", lPBItems.count);
以上返回:
PB Items = NSArray of count 0
如上所述,如果以NSString形式编写,它将以NSString形式正确返回数据.
As mentioned above, it returns the data correctly as NSStrings if written as NSStrings.
任何帮助将不胜感激. 谢谢 斯蒂芬·C
Any help would be very much appreciated. Thanks Stephen C
推荐答案
我遇到了同样的问题,我认为valueForPasteboardType系列方法已损坏,总是返回NSData. 这是我的解决方案:
I ran into the same issue and I think the valueForPasteboardType family of methods are broken and always return NSData. Here is my solution:
NSArray * lArrayFromPasteBoard = [pPasteBoard valueForPasteboardType:@"com.my.custom.type"];
if ([lArrayFromPasteBoard isKindOf:[NSData class]])
{
lArrayFromPasteBoard = [[NSPropertyListSerialization propertyListWithData:(NSData*)lArrayFromPasteBoard options:0 format:0 error:0];
}
希望这会成功,以便一旦苹果修复了他们的错误后,if中的代码将不再被调用
hopefully this will make it so the code in the if won't get called anymore once apple fixes their bug
这篇关于在UIPasteboard中存储NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!