gpt4 book ai didi

ios - UIPasteboard:NSString 拒绝复制到剪贴板

转载 作者:行者123 更新时间:2023-11-29 02:30:58 27 4
gpt4 key购买 nike

我有以下代码使用 bit.ly API 来缩短 URL。

NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.bit.ly/v3/shorten?login=%@&apikey=%@&longUrl=%@&format=txt", login, key, self.link.text]] encoding:NSUTF8StringEncoding error:nil];

我还有以下代码将缩短的 URL 复制到粘贴板:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = shortenedURL;

但是,这不起作用。在输出日志中,显示的是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setString:]: Argument is not an object of type NSString [(null)]'

如果参数不是对象,那它是什么?我试着假设这是一个 URL:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.URL = shortenedURL;

产生了相同类型的错误,只是说参数不是 NSURL 对象,而不是之前的错误说参数不是 NSString 对象。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setURL:]: Argument is not an object of type NSURL [(null)]'

有人知道怎么办吗?

最佳答案

不幸的是 string UIPasteboard 上的属性(property)object 实际上不是对字符串的直接引用,而是您关心的给定类型的 Objective-C 数组的 getter 和 setter 接口(interface)。

Swift 提供了 optional ,但不幸的是 UIPasteboard底层是 Objective-C,它并不能很好地支持选项。这是其中一个案例。

如果您分配 nil (空)到 string属性,执行会将 null 作为数组括在方括号中 [(null)]然后尝试将其添加到 [NSURL] 类型的空数组中因为[(null)][NSURL] 的类型不匹配你会得到:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setURL:]: Argument is not an object of type NSURL [(null)]'

😤

UIPasteboard GitHub doc

unsafe initializer used

关于ios - UIPasteboard:NSString 拒绝复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881920/

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