最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]iOS 8.1中出错:NSAttributedStrin初始化出错:EXC_BAD_ACCESS code=1 address 0xbbadbeef

iOS crifan 1400浏览 0评论

代码:

    var decodedHtml:String {

        var decodedHtmlStr = self

        //print("decodedHtmlStr=\(decodedHtmlStr)")

        do {

            if let encodedData = decodedHtmlStr.dataUsingEncoding(NSUTF8StringEncoding) {

                let attributedOptions : [String: AnyObject] = [

                    NSDocumentTypeDocumentAttribute        : NSHTMLTextDocumentType,

                    NSCharacterEncodingDocumentAttribute    : NSUTF8StringEncoding

                ]

                //print("attributedOptions=\(attributedOptions)") //["DocumentType": NSHTML, "CharacterEncoding": 4]

                let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

出错:

2016-04-22 10:52:10.643 [Debug] [NSOperationQueue 0x7b785470 :: NSOperation 0x7b71fd00] [LoginViewController.swift:1508] getMessageFromContactIdMsgJsonDict(_:contactIdMsgJsonDict:) > messageJson=Optional({

  "_id" : "message-d02698f5-7ffd-41f0-9f98-b4528801eb9d",

  "text" : "你是谁?",

  "receiver" : "user-00000000-0000-0000-0000-000000000002",

  "sendTime" : "1461239682753",

  "sender" : "user-08da3990-9708-4338-ba5e-cff0586e0af8",

  "subtype" : "text",

  "pinned" : false,

  "icon" : "#f6b37f",

  "created" : 1461239668392,

  "type" : "chat",

  "senderName" : "李茂",

  "seq" : 39

})

1   0x6a38159 StartWebThread()

2   0x47629b7 __pthread_once_handler

3   0x47515eb _os_once

4   0x4762966 pthread_once

5   0x6a37eee WebThreadEnable

6   0x590d32a +[WebView(WebPrivate) enableWebThread]

7   0x590a090 WebKitInitialize

8   0xacf230 ___UIApplicationLoadWebKit_block_invoke

9   0x441ce2f _dispatch_client_callout

10  0x440776c dispatch_once_f

11  0x44075fc dispatch_once

12  0xacf140 _UIApplicationLoadWebKit

13  0x392b461 _class_initialize

14  0x3933fe5 lookUpImpOrForward

15  0x3933e8d _class_lookupMethodAndLoadCache3

16  0x393e12f objc_msgSend

17  0x4995a84 _NSReadAttributedStringFromURLOrData

18  0x4992e4c -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:]

19  0x14bb2e _TTOFE5UIKitCSo18NSAttributedStringcfzT4dataCSo6NSData7optionsGVs10DictionarySSPs9AnyObject__18documentAttributesGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary___S0_

20  0x14a88c _TFE5UIKitCSo18NSAttributedStringCfzT4dataCSo6NSData7optionsGVs10DictionarySSPs9AnyObject__18documentAttributesGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary___S0_

21  0x1430d4 _TFE7JianDaoSSg11decodedHtmlSS

22  0x1a40e9 _TF7JianDao19filterUnsupportCharFSSSS

23  0x1a3d82 _TF7JianDao25parseMessageJsonToMessageFVS_4JSONCS_7Message

24  0x18eb48 _TFC7JianDao19LoginViewController34getMessageFromContactIdMsgJsonDictfTSS20contactIdMsgJsonDictGVs10DictionarySSVS_4JSON__GSqCS_7Message_

25  0x18ad61 _TFC7JianDao19LoginViewController17getReadMsgHandlerfTSb10extraParasGSqPs9AnyObject__12respJsonDictVS_4JSON_T_

26  0x190630 _TPA

27  0x284087 _TFF7JianDao24getUrlRespJsonDict_asyncFTSS10extraParasGSqPs9AnyObject__19respJsonDictHandlerFTSbGSqPS0___VS_4JSON_T__T_U_FCS_8ResponseT_

28  0x27f070 _TPA__TFF7JianDao24getUrlRespJsonDict_asyncFTSS10extraParasGSqPs9AnyObject__19respJsonDictHandlerFTSbGSqPS0___VS_4JSON_T__T_U_FCS_8ResponseT_

29  0x213cf2 _TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__

30  0x21275f _TPA__TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__

31  0x213a24 _TTRXFo_iC7JianDao8Response_iT__XFo_oS0__dT__

如图:

iOS 8.1 NSAttributedString init  EXC_BAD_ACCESS code=1 address 0xbbadbeef

iOS 8.1 NSAttributedString  EXC_BAD_ACCESS

ios5 – NSAttributedString in CATextLayer causes EXC_BAD_ACCESS (ios 5.0) – Stack Overflow

NSAttributedString alloc] initWithData子线程中触发EXC_BAD_ACCESS | iOS开发 – CocoaChina CocoaChina_让移动开发更简单

Understanding EXC_BAD_ACCESS | Lou Franco: code, apps, and writings

iOS Free App: NSAttributedString strange error in iPhone and not iPad

Stack Mobile – Convert HTML to NSAttributedString in iOS

iOS 8.1/8.2/8.3 都会挂

在一个队列里运行就不会挂-》可以放在主线程中。。

NSAttributedString

NSAttributedString ios 8

NSAttributedString ios 8 crash

ios – NSAttributedString initWithData and NSHTMLTextDocumentType crash if not on main thread – Stack Overflow

NSAttributedString Class Reference

“- initWithData:options:documentAttributes:error:

Initializes and returns a new attributed string object from the data contained in the given data object.

Declaration

SWIFT

          init(data data: NSData,

           options options: [String : AnyObject],

documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>) throws

OBJECTIVE-C

– (instancetype)initWithData:(NSData *)data

                     options:(NSDictionary<NSString *,

                                      id> *)options

          documentAttributes:(NSDictionary<NSString *,

                                      id> * _Nullable *)dict

                       error:(NSError * _Nullable *)error

Parameters

data    

The data from which to create the string.

options    

Document attributes for interpreting the document contents. NSDocumentTypeDocumentAttribute, NSCharacterEncodingDocumentAttribute, and NSDefaultAttributesDocumentAttribute are supported option keys. If you pass an empty dictionary, the method examines the data to attempt to determine the appropriate attributes.

dict    

An in-out dictionary containing document-level attributes described in Document Attributes. May be NULL, in which case no document attributes are returned.

error    

An in-out variable containing an encountered error, if any.

Return Value

Returns an initialized attributed string object, or nil if the data can’t be decoded.

Discussion

The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import."

->如果设置了NSDocumentTypeDocumentAttribute为NSHTMLTextDocumentType

-》那么就要在主线程中运行

-》会和主线程同步(更新对应的内容?)

-》那么就去想办法,去设置NSDocumentTypeDocumentAttribute为其他值?

-》但是此处,就是为了HTML解码,所以才设置为NSHTMLTextDocumentType,没法改变为别的值:

// Supported document types for the NSDocumentTypeDocumentAttribute key in the document attributes dictionary.

/************************ Document formats ************************/

@available(iOS 7.0, *)

public let NSPlainTextDocumentType: String

@available(iOS 7.0, *)

public let NSRTFTextDocumentType: String

@available(iOS 7.0, *)

public let NSRTFDTextDocumentType: String

@available(iOS 7.0, *)

public let NSHTMLTextDocumentType: String

-》要么去:加上主线程,但是很明显,此处的函数中,为了立刻返回html解码后的值,不方便调用异步去调用main线程

->只能想办法,同步的调用主线程

-》不知道大量的调用此处html解码的话,会不会对于主线程界面影响很大?用户操作受影响?

-》要么去:想办法,换个,html entity decode的办法

先去试试第二种:

详见:

[已解决]swift去除nbsp的乱码

转载请注明:在路上 » [已解决]iOS 8.1中出错:NSAttributedStrin初始化出错:EXC_BAD_ACCESS code=1 address 0xbbadbeef

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.086 seconds, using 18.90MB memory