Как вернуться к родительской функции во вложении вложенных функций, которое также имеет тип возврата в Swift - PullRequest
1 голос
/ 26 января 2020

Когда я создал заголовок вопроса, я понял, что этот вопрос трудно кому-то правильно передать. Итак, позвольте мне объяснить, пожалуйста.

В основном у меня есть функция, которая требует оператора возврата, в данном случае это Bool. Теперь внутри этой функции. У меня есть другая функция, которая также требует отдельного возврата. Я хочу иметь возможность вызывать оператор возврата для главной / родительской функции внутри Nest.

Код:

func ParentFunction() -> Bool {

  // This function represents a real function where you have a to have a return type. 
  // So in other word you can not take off the fact that you have to have a -> String.
  func MyNestedFunction() -> String {

    // Here is where you would return the nested functions statement
    // but I'd rather not return the string and just end everything and return the Bool from the parent function
    return "Hello"

    // This is the parent functions return here I want to be able to stop any other process that the ParentFunction might do and just return here.
    return true

  }

    // This is the parent functions return as well.
    return true
}

** Больше из актуальный код здесь: **

   func MyFunction() -> Bool {
    tagger.enumerateTags(in: tagger.string!.startIndex..<tagger.string!.endIndex, unit: .paragraph, scheme: .nameTypeOrLexicalClass) { (tagResult, tokenRange) -> Bool in
        if let tag = tagResult, tag == "nil" {
            print("Found")
        }
        return true
    }
    // I would like it If we have already printed found in the function above then don't continue on to this function below
    tagger.enumerateTags(in: tagger.string!.startIndex..<tagger.string!.endIndex, unit: .word, scheme: .nameTypeOrLexicalClass) { (tagResult, tokenRange) -> Bool in
        if tagResult != nil {
            print("Found")
        }
        return true
    }

    return true
}

1 Ответ

2 голосов
/ 26 января 2020

Я не знаю деталей вашей функции enumerateTags, но, поскольку она имеет completion handler, вы должны добавить ее к своей функции и вызывать ее из completion handler из функции enumerateTags.

       func MyFunction(completion: @escaping (Bool) -> Void) {
        tagger.enumerateTags(in: tagger.string!.startIndex..<tagger.string!.endIndex, unit: .paragraph, scheme: .nameTypeOrLexicalClass) { (tagResult, tokenRange) -> Bool in
            if let tag = tagResult, tag == "nil" {
                print("Found")
                completion(true)
            }
            return true
        }
        // I would like it If we have already printed found in the function above then don't continue on to this function below
        tagger.enumerateTags(in: tagger.string!.startIndex..<tagger.string!.endIndex, unit: .word, scheme: .nameTypeOrLexicalClass) { (tagResult, tokenRange) -> Bool in
            if tagResult != nil {
                print("Found")
                completion(true)
            }
            return true
        }
      }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...