In iOS 8 and OS X 10.10 there is a new API on NSString
:
Objective-C
+ (NSStringEncoding)stringEncodingForData:(NSData *)data
encodingOptions:(NSDictionary *)opts
convertedString:(NSString **)string
usedLossyConversion:(BOOL *)usedLossyConversion;
Swift
open class func stringEncoding(for data: Data,
encodingOptions opts: [StringEncodingDetectionOptionsKey : Any]? = nil,
convertedString string: AutoreleasingUnsafeMutablePointer<NSString?>?,
usedLossyConversion: UnsafeMutablePointer<ObjCBool>?) -> UInt
Now you can let the framework do the guess and in my experience that works really well!
From the header (the documentation does not state the method at the moment but it was officially mentioned in WWDC Session 204 (page 270):
- an array of suggested string encodings (without specifying the 3rd option in this list, all string encodings are considered but the ones in the array will have a higher preference; moreover, the order of the encodings in the array is important: the first encoding has a higher preference than the second one in the array)
- an array of string encodings not to use (the string encodings in this list will not be considered at all)
- a boolean option indicating whether only the suggested string encodings are considered
- a boolean option indicating whether lossy is allowed
- an option that gives a specific string to substitude for mystery bytes
- the current user's language
- a boolean option indicating whether the data is generated by Windows
If the values in the dictionary have wrong types (for example, the value of NSStringEncodingDetectionSuggestedEncodingsKey is not an array), an exception is thrown.
If the values in the dictionary are unknown (for example, the value in the array of suggested string encodings is not a valid encoding), the values will be ignored.
Example (Swift):
var convertedString: NSString?
let encoding = NSString.stringEncoding(for: data, encodingOptions: nil, convertedString: &convertedString, usedLossyConversion: nil)
If you just want the decoded string and don't care about the encoding you can remove the let encoding =
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…