如何处理任务:查询选择和响应(引擎)

时间:2016-07-15 13:11:39

标签: ios swift

  

ANSWERED!我发布了一个代码,我从Arbitur修改了代码。感谢Phillip以及引导我重定向搜索的关键建议。

TLDR:

  

有没有办法从字符串运行函数?像RunFunction一样(" myfunction _" + number +"()"),或者,是一种像myfunction _ \& number()这样的硬编码方式?

我是贾斯汀,这是我的第一篇文章。在过去的2个月里,我一直在努力没有提出问题,但我终于坏了。我会尽量简短,但我非常罗嗦(这个长篇介绍并没有帮助)。提前感谢您的耐心。

基本上,我正在制作一个将简单的单词和短语转换为Swift代码的引擎;这样做的目的是快速制作文本游戏,教育应用程序以及任何涉及非线性问题/答案的内容,这些问题/答案会根据您的回复方式而变化。

现在,我正在研究我认为很容易的部分:存储标签和按钮的实际文本(我在Xcode / iOS atm中工作)。我决定像电影制片人/小说家一样对它进行建模:电影由由剪辑组成的场景组成。您也可以将其视为章节/段落

所以(不是实际代码):

App = (Scene 1 -> Scene 2 -> Scene 3, etc)
Scene 1 = (Clip 1 -> Clip 2 -> Clip 3, etc)
Scene 2 =  Clip 2 -> Clip 5 -> Clip 1, based on responses                        

....等等。

我认为(和程序)的方式,我倾向于尽可能地避免类,除了严格的数据或严格的函数结构。

所以,我的第一个解决方案是使用嵌套函数创建一个struct(或类):

struct SceneList {

    //fScene calls, and passes the clip# from the button click
    func fPlayClip(clipNUM: Int)
    {
        //Do logic stuff, then play the correct clip
    }

    //Called from button clicks on main screen; passes in clip#
    func fScene1(play_clip clipNumber: Int){

        //sub functions(S1)
        func fClip1(){ print("I'm scene 1, clip 1")}
        func fClip2(){ print ("I'm scene 1, clip 2")}

        fPlayClip(clipNumber)
    }

    func fScene2(play_clip clipNumber: Int){

        //sub functions(S2)
        func fClip1(){ print("Yo, scene 2, clip 1")}
        func fClip2(){ print ("Yo, scene 2, clip 2")}

        fPlayClip(clipNumber)
    }
}

不幸的是,这个设计失败了,因为我没办法从fPlayClip()调用子函数,所以我采取了另一种方法:

  //////////////////////////
 /////SceneList.swift////// 
//////////////////////////  

struct Scene1{
    func fClip1(){ print("I'm scene 1, clip 1")}
    func fClip2(){ print ("I'm scene 1, clip 2")}}

struct Scene2{
    func fClip1(){ print("Yo, scene 2, clip 1")}
    func fClip2(){ print ("Yo, scene 2, clip 2")}}



  //////////////////////////
 ////////Main.swift//////// 
//////////////////////////  

// Implemention of the structs / clips.
// (playScene is called in button click method.)
func playScene(/*button prams go here*/){

    // Switch(){ case desired:          // Solve for X,
        var oScenePlayer = SceneX()     // where X is the desired scene

    // Switch(){ case desired:          // Solve for Z,
        oScenePlayer.fClipZ()           // where Z is the desired clip
}

同样,这失败了,因为我不能只使用一个对象[oScenePlayer],因为每个结构都是不同的类型。

然后我走了一段时间试图想出一种方法来摧毁物体,然后重新创造它,但我无法做到这一点。然后,我四处寻找一种方法将一个对象重新分配给另一个类型,但是不能。然后,我深入研究扩展,协议,泛型类型等,尝试找出扩展/固有的方法来使其工作,但失败了。

所以现在,我的最后两个/工作/解决方案,只是在场景变化上创建一个新对象,等待ARC或其他什么来破坏旧的;或者,让我回到第一个例子,并简单地将[fPlayScene]嵌入到每个函数中:

//SOLUTION 1:
struct SceneList {

    func fScene1(play_clip clipNumber: Int){

        //sub functions(S1)
        func fClip1(){ print("I'm scene 1, clip 1")}
        func fClip2(){ print ("I'm scene 1, clip 2")}

        //Do logic stuff below, solving for Y
        case desired:
        fClipY()
    }
}
//SOLUTION 2:  
  //////////////////////////
 /////SceneList.swift////// 
//////////////////////////  
struct Scene1{

    func fClip1(){ print("I'm scene 1, clip 1")}
    func fClip2(){ print ("I'm scene 1, clip 2")}}
}

//And so on...


  //////////////////////////
 ////////Main.swift//////// 
//////////////////////////  

//////////////////////////
// Inside the globalish area:
let oScene1: Scene1,
    oScene2: Scene2
    //And so on...

var global_next_scene = 1
var global_next_clip  = 1


/////////////////////////////////
// Inside the button call method:
func playScene(next_scene: Int, next_clip: Int){


    switch(next_scene){                 //Find the scene
        case 1:
            oScene1 = Scene1()

            switch(next_clip){          //Play the clip
                case 1:
                    oScene1.fClip1()
    }
}

基本上,我觉得我使用WAY太多的切换语句,并且使用太多的地方(可能有数百个场景和数千个剪辑),当像{{1}这样简单的事情时会工作,但我不知道任何'从字符串执行快速命令'函数:[,或者像RunFunction("fClip" + next_clip + "()")那样我可以硬编码的东西,我认为15年前我可以在c ++中编程,当时我上次编程了什么

我提出了一些其他疯狂的想法来实现这一点,我甚至考虑过OOP(不寒而栗)嵌入式类,并且成员变量包含子类的实例,但是凭借我目前的知识/资源,我无法找到比最后两个片段更简单的方法。

所有结构,成员函数,开关语句等都将通过我的引擎自动生成 - 所以我并不是在寻找更快的方法来做到这一点,这似乎是一种效率低下/征税的方式。

任何指导都将不胜感激。非常感谢提前,我很惊讶我这么长时间需要在这里问一个问题^ - ^()

和平与祝福

2 个答案:

答案 0 :(得分:2)

在我看来,您正在将自然数据混合到您的代码中。也就是说,您正在创建功能,知道他们负责哪个元素......这就是导致您想知道的所有重复的原因。

也许有些问题我不明白,但为什么没有这样的结构:

class Clip {
    func play() {
        print("Playing \(self)")
    }
}

class Scene {
    var clips = [Clip]()
    func play() {
        print("Playing \(self)")
        for aClip in clips {
            aClip.play()
        }
    }
    func playClip(number: Int) {
        if number >= 0 && number < clips.count {
            clips[number].play()
        }
    }
}

class SceneList {
    var scenes = [Scene]()
    func play() {
        print("Playing \(self)")
        for aScene in scenes {
            aScene.play()
        }
    }
    func playScene(number: Int) {
        if number >= 0 && number < scenes.count {
            scenes[number].play()
        }
    }
}

var list = SceneList()
for _ in 0..<2 {
    var scene = Scene()
    list.scenes.append(scene)
    for _ in 0..<3 {
        scene.clips.append(Clip())
    }
}
list.play()

添加您喜欢的任何其他属性和逻辑,但重点是将这些编号的函数转换为带有数据引用的泛型函数。

答案 1 :(得分:1)

Phillip的回答激发了我更多地研究数组并将数据与函数分离,而我确实想要的是可用的闭包 - 我不知道你可以简单地通过声明来调用闭包(展开)一个变量:)多么简洁的语言!

这很完美,现在我不需要任何类或结构^ - ^()(更好!!)

//initializer
var clips_for_scene = [
    1: { print("initialized") }
]

//SceneList
var scenes = [
    1: {  clips_for_scene = [
        1: { print("scene 1, clip 1") },
        2: { print("s1, c2") }]},
    2: { clips_for_scene = [
        1: { print("scene 2, clip 1") },
        2: { print("s2, c2") }]}]


func playClip(which_scene: Int, _ which_clip:Int){

    scenes[which_scene]?()
    clips_for_scene[which_clip]?()
}


playClip(1, 1)
playClip(1, 2)
playClip(2, 1)
playClip(2, 2)

//output:
//scene 1, clip 1
//s1, c2
//scene 2, clip 1
//s2, c2

感谢Arbitur,作为他们对另一个问题的回答: https://stackoverflow.com/a/30286375/6593818