在本文中,我们将给您介绍关于无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey的详细内容,此外,我们还
在本文中,我们将给您介绍关于无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey的详细内容,此外,我们还将为您提供关于asp.net-web-api-405使用AttributeRouting.PUTAttribute,除非我还包含HttpPutAttribute、cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径、cocoa – 将NSAttributedString复制到粘贴板、com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码的知识。
本文目录一览:- 无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey
- asp.net-web-api-405使用AttributeRouting.PUTAttribute,除非我还包含HttpPutAttribute
- cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径
- cocoa – 将NSAttributedString复制到粘贴板
- com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码
无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey
我在SO的某处找到了此字符串扩展名,它使我可以将html代码转换为属性字符串:
func html2AttributedString() -> NSAttributedString { return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)}
在Swift 3中工作正常,但在Swift 4中,Xcode抱怨:
无法将类型“ NSAttributedString.DocumentAttributeKey”的值转换为预期的字典密钥类型“
NSAttributedString.DocumentReadingOptionKey”
我该如何解决?
答案1
小编典典您需要传递可用的NSAttributedString
DocumentType选项之一:
超文本标记语言(HTML)文档。
static let html: NSAttributedString.DocumentType
纯文本文档。
static let plain: NSAttributedString.DocumentType
富文本格式的文档。
static let rtf: NSAttributedString.DocumentType
带附件文档的RTF格式。
static let rtfd: NSAttributedString.DocumentType
在这种情况下,您将需要通过第一个(html) NSAttributedString.DocumentType.html
因此,为Swift 4 的扩展应如下所示:
extension NSAttributedString { convenience init(data: Data, documentType: DocumentType, encoding: String.Encoding = .utf8) throws { try self.init(data: data, options: [.documentType: documentType, .characterEncoding: encoding.rawValue], documentAttributes: nil) } convenience init(html data: Data) throws { try self.init(data: data, documentType: .html) } convenience init(txt data: Data) throws { try self.init(data: data, documentType: .plain) } convenience init(rtf data: Data) throws { try self.init(data: data, documentType: .rtf) } convenience init(rtfd data: Data) throws { try self.init(data: data, documentType: .rtfd) }}
extension StringProtocol { var data: Data { return Data(utf8) } var htmlToAttributedString: NSAttributedString? { do { return try .init(html: data) } catch { print("html error:", error) return nil } } var htmlDataToString: String? { return htmlToAttributedString?.string }}
extension Data { var htmlToAttributedString: NSAttributedString? { do { return try .init(html: self) } catch { print("html error:", error) return nil } }}
游乐场测试
let htmlString = "<style type=\"text/css\">#red{color:#F00}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"red\" >Red</span><span id=\"green\" > Green </span><span id=\"blue\">Blue</span>"let htmlData = Data(htmlString.utf8)htmlString.htmlToAttributedStringhtmlData.htmlToAttributedString
讨论不应从后台线程调用HTML导入器(即,选项字典包含值为html的documentType)。它将尝试与主线程同步,失败并超时。从主线程调用它是可行的(但如果HTML包含对外部资源的引用,仍可能会超时,应该不惜一切代价避免这样做)。HTML导入机制用于实现诸如markdown之类的东西(即,文本样式,颜色等),而不是用于常规HTML导入。
asp.net-web-api-405使用AttributeRouting.PUTAttribute,除非我还包含HttpPutAttribute
[RoutePrefix("api/Sites")] public class SitesController : BaseApiController { [POST("")] public bool CreateSite(SiteSignupArgs args) { ... } [GET("Statuses")] public IList<SiteAuditviewmodel> GetStatuses() { ... } [PUT("Statuses/{siteId}")] [HttpPut] // This is required or 405 is returned public HttpResponseMessage UpdateStatus(string siteId,UpdateStatusArgs args) { ... } [DELETE("Statuses/{siteId}")] public HttpResponseMessage Delete(string siteId) { return Request.CreateResponse(HttpStatusCode.OK); } }
AttributeRouting.Core 3.5.6版,AttributeRouting.Core.Http,AttributeRouting.Core.Web,AttributeRouting.WebApi
MVC4
未安装WebDAV.
解决方法
现在,即使您明确指定了[PUT(“Statuses / {siteId}”)]属性,它也无法工作,因为,Action选择器从System.Web.Http命名空间中查找属性,如HttpGetAttribute,HttpPostAttribute,HttpPutAttribute等.
由于AttributeRouting的PUTAttribute不属于上述类型,因此Action选择器不会考虑它,仍然认为它是默认的,即POST.因此,使用HttpPut属性的解决方法是正确的.
cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径
我是NSAttributedString的新手,但我有一个使用enumerateAttributesInRange的循环:options:usingBlock:寻找NSAttachmentAttributeName,这一切都正常.但是更深入,我进入了fileWrapper类,它是apparent inability to give me the path of the item.
我如何获得NSTextAttachment的名称和路径?
相关:是否有更简单的方法来获取它们然后单步执行属性?
非常感谢!
解决方法
您可以继承NSTextView并覆盖NSDraggingDestination Protocol方法draggingEntered:并且您可以在拖动操作期间遍历传递给应用程序的NSPasteboardItem对象.我选择将路径名及其inode编号保存在NSMutableDictionary中,因为NSFileWrapper可以为您提供引用文件的inode.稍后,当我通过NSAttributedString访问NSTextView内容时,我可以使用inode作为索引来获取附件的路径名.
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender { // get pasteboard from dragging operation NSPasteboard *pasteboard = [sender draggingPasteboard]; NSArray *pasteboardItems = [pasteboard pasteboardItems]; for ( NSPasteboardItem *pasteboardItem in pasteboardItems ) { // look for a file url type from the pasteboard item Nsstring *draggedURLString = [pasteboardItem stringForType:@"public.file-url"]; if (draggedURLString != nil) { NSURL *draggedURL = [NSURL URLWithString:draggedURLString]; Nsstring *draggedpath = [draggedURL path]; NSLog(@"pathname: %@",draggedpath); // do something with the path // get file attributes NSDictionary *draggedAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:draggedpath error:nil]; if ( draggedAttributes == nil) continue; // the NSFileWrapper allows access to the absolute file via NSFileSystemFileNumber // put the path and the inode (returned as an NSNumber) into a NSMutableDictionary NSNumber *draggedInode = [draggedAttributes objectForKey:NSFileSystemFileNumber]; [draggedFiles setobject:draggedpath forKey:draggedInode]; } } return [super draggingEntered:sender]; }
我的解决方案的一个问题,即不影响我的应用程序,是将多个文件拖入视图(单独或一起),它们是同一文件的硬链接,只会被索引为添加到字典的最后一个路径名.分享inode.根据应用程序如何使用路径名,这可能是一个问题.
cocoa – 将NSAttributedString复制到粘贴板
这是我必须复制常规Nsstring:
NSPasteboard *pb = [NSPasteboard generalPasteboard]; NSArray *types = [NSArray arrayWithObjects:NsstringPboardType,nil]; [pb declareTypes:types owner:self]; [pb setString:@"asdfasdf" forType:NsstringPboardType];
如何设置NSAttributedString?
谢谢
解决方法
com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码
private static void retrieveMultipleItemsBatchGet() { try { TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes(forumTableName); forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name","Amazon S3","Amazon DynamoDB"); TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName); threadTableKeysAndAttributes.addHashAndRangePrimaryKeys("ForumName","Subject","Amazon DynamoDB","DynamoDB Thread 1","DynamoDB Thread 2","S3 Thread 1"); Map<String,TableKeysAndAttributes> requestItems = new HashMap<String,TableKeysAndAttributes>(); requestItems.put(forumTableName,forumTableKeysAndAttributes); requestItems.put(threadTableName,threadTableKeysAndAttributes); System.out.println("Making the request."); BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes,threadTableKeysAndAttributes); do { for (String tableName : outcome.getTableItems().keySet()) { System.out.println("Items in table " + tableName); List<Item> items = outcome.getTableItems().get(tableName); for (Item item : items) { System.out.println(item.toJSONPretty()); } } // Check for unprocessed keys which Could happen if you exceed provisioned // throughput or reach the limit on response size. Map<String,KeysAndAttributes> unprocessedKeys = outcome.getUnprocessedKeys(); if (outcome.getUnprocessedKeys().size() == 0) { System.out.println("No unprocessed keys found"); } else { System.out.println("Retrieving the unprocessed keys"); outcome = dynamoDB.batchGetItemUnprocessed(unprocessedKeys); } } while (outcome.getUnprocessedKeys().size() > 0); } catch (Exception e) { System.err.println("Failed to retrieve items."); System.err.println(e.getMessage()); } }
public TableKeysAndAttributes decorate(TableKeysAndAttributes tableKeysAndAttributes);
今天的关于无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net-web-api-405使用AttributeRouting.PUTAttribute,除非我还包含HttpPutAttribute、cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径、cocoa – 将NSAttributedString复制到粘贴板、com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码的相关知识,请在本站进行查询。
本文标签: