通过解析在 UITextview 中设置文本 - swift 2

setting text inside UITextview from parse - swift 2

我正在尝试将字符串从 Parse 传递到我的文本视图,它适用于 uitextfield 而不适用于 uitextview。

这是代码,

self.companyBackgroundTextview.text = (employerProfileObject.valueForKey("companyBackground")?.capitalizedString)! as String   

上面的代码返回错误。这就是错误,

    2015-10-29 13:36:38.592 TestView[5033:290374] *** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation_Sim/UIFoundation-432/UIFoundation/TextSystem/NSLayoutManager_Private.m:1551
    2015-10-29 13:36:38.605 TestView[5033:290374] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010d70ef45 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x000000010d186deb objc_exception_throw + 48
        2   CoreFoundation                      0x000000010d70edaa +[NSException raise:format:arguments:] + 106
        3   Foundation                          0x000000010cdd26b2 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 169
        4   UIFoundation                        0x0000000113ffaa7f -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 409
        5   UIFoundation                        0x0000000113ffa7a1 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2397
        6   UIFoundation                        0x00000001140331fa -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 747
        7   UIFoundation                        0x00000001140332d2 -[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 47
        8   UIFoundation                        0x000000011405aa76 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 152
        9   UIFoundation                        0x000000011405a591 -[NSTextStorage processEditing] + 349
        10  UIFoundation                        0x000000011405a1eb -[NSTextStorage endEditing] + 82
        11  UIKit                               0x000000010e5f4f2c -[UITextView setAttributedText:] + 250
        12  UIKit                               0x000000010e5fcb31 -[UITextView setText:] + 188
        13  TestView                           0x000000010b843333 _TFFC8Occupost18ProfileEmployerTVC9fetchDataFS0_FT_T_U_FT_T_ + 4467
        14  TestView                            0x000000010b7e0997 _TTRXFo__dT__XFdCb__dT__ + 39
        15  libdispatch.dylib                   0x000000010f7ece5d _dispatch_call_block_and_release + 12
        16  libdispatch.dylib                   0x000000010f80d49b _dispatch_client_callout + 8
        17  libdispatch.dylib                   0x000000010f7f5bef _dispatch_root_queue_drain + 1829
        18  libdispatch.dylib                   0x000000010f7f54c5 _dispatch_worker_thread3 + 111
        19  libsystem_pthread.dylib             0x000000010fb554f2 _pthread_wqthread + 1129
        20  libsystem_pthread.dylib             0x000000010fb53375 start_wqthread + 13
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 

看起来您正在尝试在后台线程(可能是 Web 请求的完成处理程序)上设置文本。当您更新 UI 元素时,您只能在主线程上进行。因此,请确保在更新 UITextView:

时处于主线程中
dispatch_sync(dispatch_get_main_queue(), ^{
    self.companyBackgroundTextview.text = (employerProfileObject.valueForKey("companyBackground")?.capitalizedString)! as String   
});