Ios 转换为Swift 3:SwiftValue unsignedIntegerValue

Ios 转换为Swift 3:SwiftValue unsignedIntegerValue,ios,swift,swift3,unsigned-integer,completionhandler,Ios,Swift,Swift3,Unsigned Integer,Completionhandler,我用Swift 2编写了一个代码,并将其转换为Swift 3。不幸的是,它不再工作了 以下是被指控的代码: func loadImage( url: String?, completionHandler: @escaping( UIImage?, NSError? ) -> Void ) { var theSrc: String? if let src = url { theSrc = src } else { theSrc =

我用Swift 2编写了一个代码,并将其转换为Swift 3。不幸的是,它不再工作了

以下是被指控的代码:

func loadImage( url: String?, completionHandler: @escaping( UIImage?, NSError? ) -> Void ) {

    var theSrc: String?

    if let src = url {
        theSrc = src
    } else {
        theSrc = placeHolderImg
    }

    let url = URL( string: theSrc! )!
    let request = NSMutableURLRequest( url: url )

    URLSession.shared.dataTask( with: request as URLRequest ) { data, response, error in

        if error == nil {

            let image = UIImage( data:data! )
            completionHandler( image, nil )

        } else {

            completionHandler( nil, error as NSError? )

        }

    }.resume()

}
此函数的调用方式如下:

WordPressWebServices.sharedInstance.loadImage( url: theImgUrl, completionHandler: { ( image, error ) -> Void in

    DispatchQueue.main.async( execute: {

        //Some code

    } )

} )
let theLastPost = Post( id: postID!, title: String( htmlEncodedString: title! ), content: String( htmlEncodedString: content! ), imageId: imgID!, imageUrl: theImgUrl, image: theImg )
以下是我从调试控制台获得的信息:

2016-10-19 06:40:15.458 tdr-ios-prototype[652:8825] -[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x6000000586f0
2016-10-19 06:40:15.478 tdr-ios-prototype[652:8825] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x6000000586f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010dac634b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010d52721e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010db35f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010da4bc15 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010da4b798 _CF_forwarding_prep_0 + 120
    5   UIFoundation                        0x0000000114e81953 -[NSHTMLReader _loadUsingWebKit] + 1329
    6   UIFoundation                        0x0000000114e82f15 -[NSHTMLReader attributedString] + 22
    7   UIFoundation                        0x0000000114e1945c _NSReadAttributedStringFromURLOrData + 5779
    8   UIFoundation                        0x0000000114e17d35 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 115
    9   tdr-ios-prototype                   0x000000010ceae80d _TTOFE5UIKitCSo18NSAttributedStringcfzT4dataV10Foundation4Data7optionsGVs10DictionarySSP__18documentAttributesGSqGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary____S0_ + 173
    10  tdr-ios-prototype                   0x000000010ceae6c4 _TFE5UIKitCSo18NSAttributedStringCfzT4dataV10Foundation4Data7optionsGVs10DictionarySSP__18documentAttributesGSqGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary____S0_ + 100
    11  tdr-ios-prototype                   0x000000010ceae14f _TFE17tdr_ios_prototypeSSCfT17htmlEncodedStringSS_GSqSS_ + 751
    12  tdr-ios-prototype                   0x000000010cea4191 _TFFFFC17tdr_ios_prototype11PostManager14getTheLastPostFT_T_U_FTGSqGSaGVs10DictionarySSPs9AnyObject____GSqCSo7NSError__T_U_FTGSqCSo7UIImage_GSqS3___T_U_FT_T_ + 545
    13  tdr-ios-prototype                   0x000000010cea55c0 _TPA__TFFFFC17tdr_ios_prototype11PostManager14getTheLastPostFT_T_U_FTGSqGSaGVs10DictionarySSPs9AnyObject____GSqCSo7NSError__T_U_FTGSqCSo7UIImage_GSqS3___T_U_FT_T_ + 512
    14  tdr-ios-prototype                   0x000000010ce7b827 _TTRXFo___XFdCb___ + 39
    15  libdispatch.dylib                   0x00000001119e9980 _dispatch_call_block_and_release + 12
    16  libdispatch.dylib                   0x0000000111a130cd _dispatch_client_callout + 8
    17  libdispatch.dylib                   0x00000001119f38d6 _dispatch_main_queue_callback_4CF + 406
    18  CoreFoundation                      0x000000010da8a4f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    19  CoreFoundation                      0x000000010da4ff8d __CFRunLoopRun + 2205
    20  CoreFoundation                      0x000000010da4f494 CFRunLoopRunSpecific + 420
    21  GraphicsServices                    0x0000000113903a6f GSEventRunModal + 161
    22  UIKit                               0x000000010e8e8f34 UIApplicationMain + 159
    23  tdr-ios-prototype                   0x000000010cea64ff main + 111
    24  libdyld.dylib                       0x0000000111a5f68d start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我在网上搜索,找到了一些关于这种错误的帖子,但我找不到解决办法。这是类型转换的问题还是我使用了错误的完成处理程序

从我发现的情况来看,下面这句话似乎是问题所在。或者至少是应用程序崩溃的时候

completionHandler( image, nil )
方法
loadImage
是名为
LoadTheLastPost
的过程的一部分。我正在使用WordPressRESTAPIv2。基本上,名为
PostManager
的对象是在
AppDelegate
中创建的(didFinishLaunchingWithOptions)。此对象将调用名为
WordPressWebServices
的类中包含的方法
getTheLastPost
,该类是单例。方法
loadImage
通过
getTheLasPost
调用。也许有什么不对劲?你觉得怎么样

也许我可以补充一点,在我收集数据之后,我创建了一个Post对象,如下所示:

WordPressWebServices.sharedInstance.loadImage( url: theImgUrl, completionHandler: { ( image, error ) -> Void in

    DispatchQueue.main.async( execute: {

        //Some code

    } )

} )
let theLastPost = Post( id: postID!, title: String( htmlEncodedString: title! ), content: String( htmlEncodedString: content! ), imageId: imgID!, imageUrl: theImgUrl, image: theImg )
以下是我的博士后课程的快速视图:

class Post: NSObject, NSCoding {

    var id: Int
    var title: String?
    var content: String?
    var imageId: Int
    var imageUrl: String?
    var image: UIImage?

    init?( id: Int, title: String?, content: String?, imageId: Int, imageUrl: String?, image: UIImage? ) {

        self.id = id
        self.title = title
        self.content = content
        self.imageId = imageId
        self.imageUrl = imageUrl
        self.image = image

        super.init()

        if id < 1 {
            return nil
        }

    }

}
字符串(htmlEncodedString:title!)
字符串(htmlEncodedString:content!)
导致错误

这是我的字符串扩展名:

extension String {

    init?( htmlEncodedString: String ) {

        let encodedData = htmlEncodedString.data( using: String.Encoding.utf8 )!
        let attributedOptions: [ String: AnyObject ] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8 as AnyObject ]

        var attributedString: NSAttributedString?

        do {
            attributedString = try NSAttributedString( data: encodedData, options: attributedOptions, documentAttributes: nil )
        } catch {
            print( error )
        }

        self.init( attributedString!.string )

    }

}
解决方案

问题来自我的字符串扩展名。多亏了这篇文章,我从一开始就看到了这篇文章,但我认为这是另外一回事,所以我设法找到了一个解决办法

我不得不编辑这行

let attributedOptions: [ String: AnyObject ] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8 as AnyObject ]
…就像这样:

let attributedOptions: [ String: AnyObject ] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject, NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue) as AnyObject ]

你在哪里调用这个方法?嗨!谢谢你的反馈!我更新了我的问题。你应该将你的答案作为答案发布,即使这是你自己的问题,并标记为正确。你在哪里调用此方法?嗨!谢谢你的反馈!我更新了我的问题。你应该将你的答案作为答案发布,即使这是你自己的问题,并标记为正确。