• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Difficulties with recovering NSAttributedString attachments from saved RTFD
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Difficulties with recovering NSAttributedString attachments from saved RTFD


  • Subject: Re: Difficulties with recovering NSAttributedString attachments from saved RTFD
  • From: "Gary L. Wade via Cocoa-dev" <email@hidden>
  • Date: Fri, 29 Nov 2019 17:36:23 -0800

While it may seem redundant, what if you specify the document type in the
document attributes?  Also, you’re storing UTF8 data in the contents but
specifying plain text; what if you specify it as UTF8 plain text?

Also, the source code for TextEdit is available somewhere; maybe try seeing
what it’s doing.
--
Gary L. Wade
http://www.garywade.com/ <http://www.garywade.com/>

> On Nov 27, 2019, at 11:35 AM, Gary L. Wade via Cocoa-dev
> <email@hidden> wrote:
>
> You want to use a file wrapper rather than data and specify the document type
> in the attributes as RTFD.
> --
> Gary L. Wade
> http://www.garywade.com/ <http://www.garywade.com/>
>
>> On Nov 27, 2019, at 10:18 AM, Jeff Younker via Cocoa-dev
>> <email@hidden> wrote:
>>
>> I am having some difficulty with saving NSAttributedStrings as RTFD and
>> then recovering
>> them with attachments intact.  I generate a string containing an attachment
>> and turn that into
>> RTFD. When I turn that RTFD back into an NSAttributedString, I get the
>> .string back, and I
>> get an attachment, but the attachment's .contents is empty.
>>
>> This is the smallest example I can come up with:
>>
>> func testSaveAndRestoreAttachment() {
>> // Build up the string "deadbeef<attachment foobar>deadbeef"
>> let originalContent = "foobar".data(using: .utf8)
>> let originalAttachment = NSTextAttachment(
>>   data: originalContent, ofType: "public.plain-text")
>> let originalString = NSMutableAttributedString(string: "deadbeef")
>> originalString.append(NSMutableAttributedString(attachment:
>> originalAttachment))
>> originalString.append(NSMutableAttributedString(string: "deadbeef")
>>
>> // save string as RTFD (note that generated RTFD contains "foobar"
>> inside.)
>> let savedRtfd = originalString.rtfd(from:
>> NSRange(0..<originalString.length))!
>>
>> // Recover string
>> let recoveredString = NSAttributedString(rtfd: savedRtfd,
>> documentAttributes: nil)!
>> // Implementation of attachments() can be found below.
>> let recoveredAttachments = attachments(from: recoveredString)
>> // There *is* an attachment!
>> let recoveredAttachment = recoveredAttachments[0]
>> // Want to get Data("foobar") but actually get nil :(
>> XCTAssertNotNil(recoveredAttachment.contents)
>> }
>>
>> When I print out the RTFD I can see the document includes the attachment
>> "foobar".
>>
>> I'm assuming that I need to pass some additional information when I
>> call NSAttributedString(rtfd:,
>> documentAttributes:)
>> but I'm at a loss here.
>>
>> Am I missing something simple? Perhaps a magical setting to be passed in
>> documentAttributes?
>>
>> -jeff
>>
>>
>>
>> ** This is the attachments() function used above:
>>
>> func attachments(from s: NSAttributedString) -> Array<NSTextAttachment> {
>>   var attachments: Array<NSTextAttachment> = []
>>   s.enumerateAttribute(.attachment, in: NSRange(0..<s.length)) {
>>       value, range, stop in
>>       guard let a = value else {
>>          return
>>       }
>>       attachments.append(a as! NSTextAttachment)
>>   }
>>   return attachments
>> }
>

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Difficulties with recovering NSAttributedString attachments from saved RTFD (From: Jeff Younker via Cocoa-dev <email@hidden>)
 >Re: Difficulties with recovering NSAttributedString attachments from saved RTFD (From: "Gary L. Wade via Cocoa-dev" <email@hidden>)

  • Prev by Date: Re: Difficulties with recovering NSAttributedString attachments from saved RTFD
  • Previous by thread: Re: Difficulties with recovering NSAttributedString attachments from saved RTFD
  • Index(es):
    • Date
    • Thread