gpt4 book ai didi

ios - 尽管 "string"为真,但 UIPasteBoard "hasStrings"属性返回 nil

转载 作者:行者123 更新时间:2023-12-04 08:09:49 24 4
gpt4 key购买 nike

我有以下代码用于抓取用户从应用外部复制到剪贴板的文本,以便他们可以将其粘贴到应用内:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

if ([pasteboard hasStrings])
{
NSString *text = pasteboard.string;
}

这一直运行良好,直到 iOS14 我注意到我一直在崩溃,因为 pasteboard.stringnil 尽管 hasStrings 为真。

我查看了文档,发现 pasteboard.string 确实有可能是 nil:

The value stored in this property is an NSString object. Theassociated array of representation types isUIPasteboardTypeListString, which includes type kUTTypeUTF8PlainText.Setting this property replaces all current items in the pasteboardwith the new item. If the first item has no value of the indicatedtype, nil is returned.

我的意思是剪贴板中有某种不是 kUTTypeUTF8PlainText 的字符串,这就是为什么 pasteboard.stringnil , 但这是正确的解释吗?

我只是对这里到底发生了什么感到困惑,并且不确定如果我遇到 pasteboard.stringnil 的情况应该告诉我的用户什么?

最佳答案

-[UIPasteboard hasStrings] == YES 仅表示粘贴板中的项目具有 public.utf8-plain-text 类型或任何其他表明它是字符串的类型.

但是 -[UIPasteboard string] 仍然可以返回 nil 如果类 NSString 的对象不能从 提供的任何数据构造itemProviders.

这是一个重现您所处情况的示例:

先实现一个符合NSItemProviderWriting的测试类

#import <Foundation/Foundation.h>

static NSString *const UTTypeUTF8PlainText = @"public.utf8-plain-text";

@interface TestObject : NSObject <NSItemProviderWriting>

@end

@implementation TestObject

- (NSData *)randomDataWithLength:(NSUInteger)length {
NSMutableData *data = [NSMutableData dataWithLength:length];
SecRandomCopyBytes(kSecRandomDefault, length, data.mutableBytes);
return data;
}

#pragma mark - NSItemProviderWriting

+ (NSArray<NSString *> *)writableTypeIdentifiersForItemProvider {
return @[UTTypeUTF8PlainText];
}

- (nullable NSProgress *)loadDataWithTypeIdentifier:(nonnull NSString *)typeIdentifier forItemProviderCompletionHandler:(nonnull void (^)(NSData * _Nullable, NSError * _Nullable))completionHandler {
// random data that an utf8 string may not be constructed from
NSData *randomData = [self randomDataWithLength:1];
completionHandler(randomData, nil);
return nil;
}

@end

然后将测试对象放入粘贴板

if (@available(iOS 11.0, *)) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
TestObject *item = [TestObject new];
[pasteboard setObjects:@[item]];

if ([pasteboard hasStrings]) {
// text may be nil
NSString *text = pasteboard.string;
}
}

关于ios - 尽管 "string"为真,但 UIPasteBoard "hasStrings"属性返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66038304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com