让函数等待响应 - Swift

时间:2015-07-31 12:47:41

标签: swift response wait adventure

我正在尝试编程文本冒险。我有一个NSTextView,用户可以在其中编写命令并获得响应,但我找不到“暂停”函数等待用户响应的方法。

template <class Item>
class IMyPriorityQueue {
private:
    /* Implementattion dependent code
       Here we can implement priority queue functions mentioned in public section of class using
       ordered array, unordered array, ordered list, unordered list, heap data structure, 
       bionomial heap and fibonacci heap.  (here list means double linked list).

       Heap means is a tree is heap-ordered if the key in each node is larger than or equal to the keys in all of that node's children (if any).
       Equivalently, the key in each node of a heap-ordered tree is smaller than or equal to the key in that node's parent (if any).

       Read Robert Sedwick on various implementation have performance on Table 9.1 on page .

    */

public:
    // Implementation-dependent handle definition
    IMyPriorityQueue(int);
    virtual ~IMyPriorityQueue();
    virtual int empty() const            = 0;
    virtual handle insert(Item )         = 0;
    virtual int getmax()                 = 0;
    virtual void change(handle, Item)    = 0;
    virtual void remove(handle)          = 0;
    virtual void join(IMyPriorityQueue&) = 0;
};


template <class Item>
class CMyLinkedListPriorityQueue : public IMyPriorityQueue {
private:

    // Implementing using unordered double linked list.
    struct sPQNode {
        Item     pqItem;
        sPQNode* pPrevPQNode;
        sPQNode* pNextPQNode;

        sPQNode(Item itm) {
            pqItem      = itm; 
            pPrevPQNode = 0; 
            pNextPQNode = 0; 
        }
    };

    // Note: here head and tail are it self sPQNode* rather than void* pointer which points to first node.
    sPQNode* m_pPQhead;
    sPQNode* m_pPQTail;

    std::vector<sPQNode*> m_vecPQNodePointer;
    int                   m_iVecContainerIndex;

    CMyLinkedListPriorityQueue(int = 0) {
        m_pPQhead = new sPQNode(0);
        m_pPQTail = new sPQNode(0);
        m_pPQhead->pPrevPQNode = m_pPQTail; m_pPQhead->pNextPQNode = m_pPQTail;
        m_pPQTail->pPrevPQNode = m_pPQhead; m_pPQTail->pNextPQNode = m_pPQhead;
        m_iVecContainerIndex  = 0;
    }

    int empty() const {
        return m_pPQhead->pNextPQNode->pNextPQNode == m_pPQhead;
        m_iVecContainerIndex = 0;
    }

    handle insert(Item v) {
        handle insPQNode = new node(v);
        insPQNode->pNextPQNode = m_pPQhead->pNextPQNode; insPQNode->pNextPQNode->pPrevPQNode = insPQNode;
        insPQNode->pPrevPQNode = m_pPQhead; m_pPQhead->pNextPQNode = insPQNode;
        m_vecPQNodePointer[m_iVecContainerIndex++] = insPQNode
        return (m_iVecContainerIndex - 1);
    }

    Item getmax() {
        Item max; sPQNode* pHeadNext = head->next;
        for (link t = x; t->next != head; t = t->next) {
            if (x->item < t->item) x = t;
        }
        max = x->item;
        remove(x);
        return max;
    }

    void change(handle hItem, Item itm) {
        m_iVecContainerIndex[hItem]->item = itm;
    }

    void remove(handle hItem) {
                // **Question how to handle this and join operation**
                 }

    void join(PQ<Item>& p) {
        tail->prev->next = p.head->next;
        p.head->next->prev = tail->prev;
        head->prev = p.tail;
        p.tail->next = head;
        delete tail; delete p.head;
        tail = p.tail;
     }
};

感谢您的回复:D

3 个答案:

答案 0 :(得分:1)

如果您使用的是Swift 2,则可以使用readLine()等待用户在命令行中按Enter键。它返回String?因为用户可以在没有输入任何内容的情况下点击输入。

答案 1 :(得分:0)

我建议等待NSTextView中的输入不是一个好策略。

如果您的方法知道您的应用所处的状态,该怎么办?然后,每当您获得要使用的文本时,将其发送到该方法并让它根据状态决定如何使用它(名称,命令......)。同时,您的应用程序刚刚返回运行循环,让用户按照自己的意愿行事。

答案 2 :(得分:0)

为用户输入添加UITextField委托和UITextField。将UITextField委托设置为self并添加方法:'textFieldShouldReturn'。

在此方法中,只要用户输入了内容,就会处理逻辑。 这是一个简短的片段:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var userInputTextField: UITextField?

    override func viewDidLoad() {
        super.viewDidLoad()

        userInputTextField = UITextField(frame: CGRectMake(0, 100, view.frame.width, 100))
        userInputTextField?.delegate = self
        userInputTextField?.returnKeyType = UIReturnKeyType.Done
        view.addSubview(userInputTextField!)
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        println(textField.text)
        return true
    }
}