GVKun编程网logo

无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey

15

在本文中,我们将给您介绍关于无法将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

无法将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

asp.net-web-api-405使用AttributeRouting.PUTAttribute,除非我还包含HttpPutAttribute

我有一个MVC项目,我试图更新以包括WebApi.为了获得所需的路由,我们使用AttributeRouting.除了返回405的[PUT]之外,所有调用似乎都正确路由.我已经简化了控制器和操作,并且仍然使用[PUT]接收错误,除非我也包含[HttpPut].不确定我错过了什么.

[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.

解决方法

你所看到的是预期的行为.如果操作名称没有带有“Get”,“Post”,“Put”,“Delete”等动词的前缀,则Web API中的Action Selector默认假定操作为动词POST.

现在,即使您明确指定了[PUT(“Statuses / {siteId}”)]属性,它也无法工作,因为,Action选择器从System.Web.Http命名空间中查找属性,如HttpGetAttribute,HttpPostAttribute,HttpPutAttribute等.

由于AttributeRouting的PUTAttribute不属于上述类型,因此Action选择器不会考虑它,仍然认为它是默认的,即POST.因此,使用HttpPut属性的解决方法是正确的.

cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径

cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径

我有一个基本的NSTextView,启用了丰富的文本和图形(在IB中).我想得到的是拖入的任何图像的路径和文件名,所以我可以将它们传递给另一个类.

我是NSAttributedString的新手,但我有一个使用enumerateAttributesInRange的循环:options:usingBlock:寻找NSAttachmentAttributeName,这一切都正常.但是更深入,我进入了fileWrapper类,它是apparent inability to give me the path of the item.

我如何获得NSTextAttachment的名称和路径?

相关:是否有更简单的方法来获取它们然后单步执行属性?

非常感谢!

解决方法

虽然我个人蔑视NSFileWrapper的设计,但如果您只需要每个附件的数据,您可以通过NSFileWrapper的regularFileContents方法将其作为NSData实例访问.但是,我需要一个有效且明确的路径名来指向我的应用程序的附件.获得它比应该做的要多得多:

您可以继承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复制到粘贴板

cocoa – 将NSAttributedString复制到粘贴板

全新的Cocoa,我正在试图弄清楚如何将NSAttributedString复制到粘贴板.我查看了文档并且不确定我是否应该使用NSPasteboardItem.

这是我必须复制常规Nsstring:

NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NsstringPboardType,nil];
[pb declareTypes:types owner:self];

[pb setString:@"asdfasdf" forType:NsstringPboardType];

如何设置NSAttributedString?

谢谢

解决方法

您需要NSRTFPboardType或NSRTFDPboardType以及NSAttributedString的RTFFromrange:documentAttributes:/ RTFDFromrange:documentAttributes:和粘贴板上的setData.

com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码

com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes的实例源码

项目:aws-dynamodb-examples    文件:DocumentAPIBatchGet.java   
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());
    }  

}
项目:java-persistence    文件:DecorateTableKeysAndAttributes.java   
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的实例源码的相关知识,请在本站进行查询。

本文标签: