Response 的 String 和property list 擴(kuò)展

// MARK: - String

extension Request {

/**
    Creates a response serializer that returns a string initialized from the response data with the specified 
    string encoding.

    - parameter encoding: The string encoding. If `nil`, the string encoding will be determined from the server 
                          response, falling back to the default HTTP default character set, ISO-8859-1.

    - returns: A string response serializer.
*/
public static func stringResponseSerializer(
    encoding encoding: NSStringEncoding? = nil)
    -> ResponseSerializer<String, NSError>
{
    return ResponseSerializer { _, response, data, error in
        guard error == nil else { return .Failure(error!) }

        if let response = response where response.statusCode == 204 { return .Success("") }

        guard let validData = data else {
            let failureReason = "String could not be serialized. Input data was nil."
            let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }
        
        var convertedEncoding = encoding
        
        if let encodingName = response?.textEncodingName where convertedEncoding == nil {
            convertedEncoding = CFStringConvertEncodingToNSStringEncoding(
                CFStringConvertIANACharSetNameToEncoding(encodingName)
            )
        }

        let actualEncoding = convertedEncoding ?? NSISOLatin1StringEncoding

        if let string = String(data: validData, encoding: actualEncoding) {
            return .Success(string)
        } else {
            let failureReason = "String could not be serialized with encoding: \(actualEncoding)"
            let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }
    }
}

/**
    Adds a handler to be called once the request has finished.

    - parameter encoding:          The string encoding. If `nil`, the string encoding will be determined from the 
                                   server response, falling back to the default HTTP default character set, 
                                   ISO-8859-1.
    - parameter completionHandler: A closure to be executed once the request has finished.

    - returns: The request.
*/
public func responseString(
    encoding encoding: NSStringEncoding? = nil,
    completionHandler: Response<String, NSError> -> Void)
    -> Self
{
    return response(
        responseSerializer: Request.stringResponseSerializer(encoding: encoding),
        completionHandler: completionHandler
    )
}
}

// MARK PropertyList
extension Request {

/**
    Creates a response serializer that returns an object constructed from the response data using 
    `NSPropertyListSerialization` with the specified reading options.

    - parameter options: The property list reading options. `NSPropertyListReadOptions()` by default.

    - returns: A property list object response serializer.
*/
public static func propertyListResponseSerializer(
    options options: NSPropertyListReadOptions = NSPropertyListReadOptions())
    -> ResponseSerializer<AnyObject, NSError>
{
    return ResponseSerializer { _, response, data, error in
        guard error == nil else { return .Failure(error!) }

        if let response = response where response.statusCode == 204 { return .Success(NSNull()) }

        guard let validData = data where validData.length > 0 else {
            let failureReason = "Property list could not be serialized. Input data was nil or zero length."
            let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }

        do {
            let plist = try NSPropertyListSerialization.propertyListWithData(validData, options: options, format: nil)
            return .Success(plist)
        } catch {
            return .Failure(error as NSError)
        }
    }
}

/**
    Adds a handler to be called once the request has finished.

    - parameter options:           The property list reading options. `0` by default.
    - parameter completionHandler: A closure to be executed once the request has finished. The closure takes 3
                                   arguments: the URL request, the URL response, the server data and the result 
                                   produced while creating the property list.

    - returns: The request.
*/
public func responsePropertyList(
    options options: NSPropertyListReadOptions = NSPropertyListReadOptions(),
    completionHandler: Response<AnyObject, NSError> -> Void)
    -> Self
{
    return response(
        responseSerializer: Request.propertyListResponseSerializer(options: options),
        completionHandler: completionHandler
    )
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市庄呈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖移宅,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鲫售,死亡現(xiàn)場(chǎng)離奇詭異硝拧,居然都是意外死亡径筏,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門障陶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)滋恬,“玉大人,你說(shuō)我怎么就攤上這事抱究』致龋” “怎么了?”我有些...
    開封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵媳维,是天一觀的道長(zhǎng)酿雪。 經(jīng)常有香客問我,道長(zhǎng)侄刽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任朋凉,我火速辦了婚禮州丹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己墓毒,他們只是感情好吓揪,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著所计,像睡著了一般柠辞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上主胧,一...
    開封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天叭首,我揣著相機(jī)與錄音,去河邊找鬼踪栋。 笑死焙格,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的夷都。 我是一名探鬼主播眷唉,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼囤官!你這毒婦竟也來(lái)了冬阳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤党饮,失蹤者是張志新(化名)和其女友劉穎摩泪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體劫谅,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡见坑,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捏检。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荞驴。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖贯城,靈堂內(nèi)的尸體忽然破棺而出熊楼,到底是詐尸還是另有隱情,我是刑警寧澤能犯,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布鲫骗,位于F島的核電站,受9級(jí)特大地震影響踩晶,放射性物質(zhì)發(fā)生泄漏执泰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一渡蜻、第九天 我趴在偏房一處隱蔽的房頂上張望术吝。 院中可真熱鬧计济,春花似錦、人聲如沸排苍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)淘衙。三九已至传藏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間彤守,已是汗流浹背毯侦。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留遗增,地道東北人叫惊。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像做修,于是被迫代替她去往敵國(guó)和親霍狰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容