2016-10-03 7 views
-1

에서 구문 분석 할 어떻게 다음과 같은 코드가 있습니다내가 JSON 파일을 만들어 스위프트

enum Story: String { 
case ReturnTrip 
case TouchDown 
case Homeward 
case Rover 
case Cave 
case Crate 
case Monster 
case Droid 
case Home 
} 

extension Story { 
var artwork: UIImage { 
    return UIImage(named: self.rawValue)! 
} 

var text: String { 
    switch self { 
    case .ReturnTrip: 
     return "On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. \"Help me, you're my only hope.\"" 
    case .TouchDown: 
     return "You deftly land your ship near where the distress signal originated. You didn't notice anything strange on your fly-by, behind you is an abandoned rover from the early 21st century and a small crate." 
    case .Homeward: 
     return "You continue your course to Earth. Two days later, you receive a transmission from HQ saing that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low." 
    case .Rover: 
     return "The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen. \"Come to 28.2342, -81.08273\". These coordinates aren't far but you don't know if your oxygen will last there and back." 
    case .Cave: 
     return "Your EVA suit is equipped with a headlamp which you use to navigate to a cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there's a faint light up ahead." 
    case .Crate: 
     return "Unlike everything else around you the crate seems new and...alien. As you examine the create you notice something glinting on the ground beside it. Aha, a key! It must be for the crate..." 
    case .Monster: 
     return "You pick up the key and try to unlock the crate, but the key breaks off in the keyhole.You scream out in frustration! Your scream alerts a creature that captures you and takes you away..." 
    case .Droid: 
     return "After a long walk slightly uphill, you end up at the top of a small crater. You look around and are overjoyed to see your robot friend, Droid-S1124. It had been lost on a previous mission to Mars. You take it back to your ship and fly back to Earth." 
    case .Home: 
     return "You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate." 
    } 
    } 
} 

스위치 문에서 문자열은 JSON 파일에 있어야합니다. 코드는 똑같이 작동해야합니다. 내가 어떻게하면 도움이 될 수 있는지 모른다.

+0

https://developer.apple.com/swift/blog/?id=37 –

답변

0

JSON은 파일 형식이 아니라 형식의 데이터에 대한 수단 : https://github.com/SwiftyJSON/SwiftyJSON

당신에게 : 지금까지 JSON으로 텍스트를 변환으로 다시 https://simple.wikipedia.org/wiki/JSON

, 당신은 SwiftyJSON 라이브러리를 사용하는 것을 고려한다고 텍스트를 JSON 객체로 변환하는 방법의 예를 보려면 here을 더 읽을 수 있습니다.

+0

자세한 내용은 나에게 어떻게 설명 할 수 있습니까? EDIT1 : 내가 넣어야하는 코드를 작성하고 json을 만들어야합니까? –

+0

가장 쉬운 방법은 [cocoapods] (https://cocoapods.org/)를 이용하는 것입니다. [여기] (https://www.raywenderlich.com/97014/use-cocoapods-with-swift)는 cocoapods 사용법에 대한 자습서입니다. 왜 데이터가 JSON으로 필요합니까? – TWOF

+0

학교 숙제 .. 나에게 내가해야 할 일이 뭔지 모르겠다. –

관련 문제