2015-01-08 6 views
1

이 텍스트에서는 json 파일의 일부 텍스트를 사용했습니다. utf8 인코딩을 적용했지만이 인코더는 비표준 문자를 인식하지 못하고 대문자입니다. 엄마 끈을 정화하는거야? 여기 내 FUNC신속한 UTF8 인코딩 및 비 UTF8 문자

func stringToUTF8String (stringaDaConvertire stringa: String) -> String { 

let encodedData = stringa.dataUsingEncoding(NSUTF8StringEncoding)! 
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] 
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)! 
//println(attributedString.string) 
return attributedString.string 

}

감사의

+0

'String '은 어떤 바이트 출력을 제공합니까? 너는 무엇을 기대 하느냐? 또한 StackOverflow에 비표준 문자를 올바르게 삽입했는지 확신 할 수 없습니다. – Tommy

+0

실제 출력 및 예상 출력과 함께 문제를 설명하는 (짧은) 입력 문자열을 표시하십시오. –

답변

4

내가 찾은 솔루션, 테이블 아스키의 UTF8 걸릴 8 비트 및 UTF16 가지고 16 비트 아스키 테이블이 솔루션은 modifing에 의해 간단하다 기능

func stringToUTF16String (stringaDaConvertire stringa: String) -> String { 

let encodedData = stringa.dataUsingEncoding(NSUTF16StringEncoding)! 
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] 
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)! 
//println(attributedString.string) 
return attributedString.string 
} 

간단한 광고에서 내 FUNC

+1

예,이 작동하지만 dataUsingEncoding이 UTF8StringEncoding을 사용하여 문자를 식별 할 수없는 이유는 여전히 알 수 없습니다. 제 경우에는 파일이 UTF-8로 저장되어 있는지 확인 했으므로'encodedData'에 올바른 내용이 있어야합니다. NSAttributedString이 UTF-16 인코딩을 사용한다는 것은 NSString에서 지원하는 유일한 인코딩이므로 문서가됩니다. 이것에 대해서는 명확하지 않다. –

+0

나는 같은 문제를 겪고 있었고 그것이 'NSAttributedString' 때문일 것입니다. 문서는'data' 매개 변수가 어떤 인코딩을 가져야 하는지를 명시하지 않지만, 반드시 NSUTF16StringEncoding이어야한다는 것을 확인했습니다. 내부적으로 그들은 아마도 그것으로 해독 할 것입니다. – samwize

+0

기초적인 'NSString'은 UTF-16을 사용하여 표현되므로 기본값이 의미가 있습니다. 즉, 들어오는 데이터를 일치시키기 위해'options : [characterEncoding : NSUTF8StringEncoding]'을 지정할 수 있습니다. – ctietze