让returnTrip = Page(故事:.ReturnTrip)?

时间:2016-10-16 11:12:43

标签: ios swift uiimage

在let returnTrip = Page(story:.ReturnTrip)中它给我们错误。我真的不知道为什么 1

enum Story {

case ReturnTrip (String)
case TouchDown
case Homeward
case Rover (String)
case Cave
case Crate
case Monster
case Droid (String)
case Home

var rawValue: String {
    switch self {
    case .ReturnTrip: return "ReturnTrip"
    case .TouchDown: return "TouchDown"
    case .Homeward: return "Homeward"
    case .Rover: return "Rover"
    case .Cave: return "Cave"
    case .Crate: return "Crate"
    case .Monster: return "Monster"
    case .Droid: return "Droid"
    case .Home: return "Home"
    }
}

}

2

extension Story {

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

var soundEffectURL : NSURL {
    let fileName: String

    switch self {
    case .Droid, .Home: fileName = "HappyEnding"
    case .Monster: fileName = "Ominous"
    default: fileName = "PageTurn"

    }

    let path = Bundle.main.path( forResource: fileName, ofType: "wav")!

    return NSURL(fileURLWithPath: path)


}

3

 var text: String {
    switch self {
    case .ReturnTrip(let name):
        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, \(name), you're my only hope.\""

然后

class Page {

let story: Story

typealias Choice = (title: String, page: Page)

var firstChoice: Choice?
var secondChoice: Choice?

init(story:Story) {
    self.story = story
}

}

extension Page {

func addChoice(title: String, story: Story) ->  Page {
    let page = Page (story: story)
    return addChoice (title:title, page:page)
}

然后

func addChoice(title: String, page: Page) -> Page {

    switch (firstChoice, secondChoice) {
    case (.some, .some): break
    case (.none, .none), (.none, .some):
    firstChoice = (title, page)
    case (.some, .none):
        secondChoice = (title, page)

    }
    return page
}

}

最后

struct Adventure {
static var story: Page {
    let returnTrip = Page(story: .ReturnTrip)
    let touchdown = returnTrip.addChoice("Stop and Investigate", story: .TouchDown)
    let homeward = returnTrip.addChoice("Continue Home to Earth", story: .Homeward)
    let rover = touchdown.addChoice("Explore the Rover", story: .Rover)
    let crate = touchdown.addChoice("Open the Crate", story: .Crate)

    homeward.addChoice("Head back to Mars", page: touchdown)
    let home = homeward.addChoice("Continue Home to Earth", story: .Home)

    let cave = rover.addChoice("Explore the Coordinates", story: .Cave)
    rover.addChoice("Return to Earth", page: home)

    cave.addChoice("Continue towards faint light", story: .Droid)
    cave.addChoice("Refill the ship and explore the rover", page: rover)

    crate.addChoice("Explore the Rover", page: rover)
    crate.addChoice("Use the key", story: .Monster)

    return returnTrip

}

}

上面的问题是什么

中的问题是什么?
let returnTrip = Page(story: .ReturnTrip)

它表示.ReturnTrip的上下文成员

1 个答案:

答案 0 :(得分:0)

您对.ReturnTrip(String)的声明告诉系统您需要将String值与enum相关联。但是在您的实现中,您没有将任何值与您的枚举相关联,这就是您收到错误消息的原因。

正确使用.ReturnTrip (String)将类似于:

let returnTrip = Page(story: .ReturnTrip("Midhun"))

详细了解Associated enum values

注意:这同样适用于.Rover(String).Droid(String)枚举案件。