2017-02-23 4 views
-1

도와주세요. 나는 그것을 해결하기 위해 많은 시간을 보냈지 만 나는 여전히 그렇게 할 수 없다.Swift 3 Json을 로컬에서 읽음 : 인수 레이블 '(contentsOfFile :)'이 (가) 사용 가능한 오버로드와 일치하지 않습니다.

은 내가 아래의 이미지처럼 오류가 발생했습니다 3.

신속한 사용하여 로컬에서 JSON 파일을 읽고 싶어. Error Image

당신은

func getContentsFromJsonFile() { 
    let path = Bundle.main.path(forResource: "fileName", ofType: "json") 
    let jsonData = try! Data(contentsOf: URL(fileURLWithPath: path!), options: Data.ReadingOptions.dataReadingMapped) 
    let dicJSON = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]   
} 
+0

이미지를 게시하지 마십시오. 우편 번호 (텍스트)! – vadian

답변

0

전에를

init(contentsOf url: URL, options: Data.ReadingOptions = default) 
감사합니다

그 차이가 있습니까? 누락 된 대신 contentsOfFile

    • contentsOf '옵션을'매개 변수입니다.
  • +0

    고맙습니다. Parth Adroja. 이제 제대로 작동합니다. 나는 너무 행복해. –

    +0

    let 경로 = Bundle.main.path (forResource : "data", ofType : "json") let jsonData = try! 데이터 (contentsOf : URL (fileURLWithPath : path!), 옵션 : Data.ReadingOptions.dataReadingMapped) dicJSON = try! JSONSerialization.jsonObject (with : jsonData, options : []) print (dicJSON) –

    1

    이 방법은 파일의 내용과 데이터 객체를 초기화하기 위해서 아래 같은 지역에서 JSON 파일을 읽을 수있는 손

    +0

    이제 제게 적합합니다. D. Mika –

    +0

    고맙습니다. path = Bundle.main.path (forResource : "data", ofType : "json") let jsonData = try! 데이터 (contentsOf : URL (fileURLWithPath : path!), 옵션 : Data.ReadingOptions.dataReadingMapped) dicJSON = try! JSONSerialization.jsonObject (with : jsonData, options : []) 인쇄 (dicJSON) –