Response 的 String 和property list 扩展

// 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
    )
}

}

推荐阅读更多精彩内容