为什么我的Swift代码在操场上工作但在Xcode项目中却没有?

时间:2015-02-12 13:43:17

标签: ios xcode swift swift-playground

Here is a zip包含游乐场的项目文件:

游乐场代码:

import Foundation

var substitutions: [String: Array] = Dictionary<String, Array<String>>()
var convertedString = String()
var stringToBeConverted = String()
var convertedPinyinSyllableArray = [String]()

substitutions["a"] = ["ā", "á", "ǎ", "à"]
substitutions["ɑ"] = ["ā", "á", "ǎ", "à"]
substitutions["e"] = ["ē", "é", "ě", "è"]
substitutions["i"] = ["ī", "í", "ǐ", "ì"]
substitutions["o"] = ["ō", "ó", "ǒ", "ò"]
substitutions["u"] = ["ū", "ú", "ǔ", "ù"]
substitutions["ü"] = ["ǖ", "ǘ", "ǚ", "ǜ"]
substitutions["v"] = ["ǖ", "ǘ", "ǚ", "ǜ"]
substitutions["A"] = ["Ā", "Á", "Ǎ", "À"]
substitutions["E"] = ["Ē", "É", "Ě", "È"]
substitutions["I"] = ["Ī", "Í", "Ĭ", "Ì"]
substitutions["O"] = ["Ō", "Ó", "Ǒ", "Ò"]
substitutions["U"] = ["Ū", "Ú", "Ǔ", "Ù"]
substitutions["Ü"] = ["Ǖ", "Ǘ", "Ǚ", "Ǜ"]
substitutions["V"] = ["Ǖ", "Ǘ", "Ǚ", "Ǜ"]

func convertPinyinNumbersToToneMarks(stringToBeConverted: String) -> String{

    let pinyinSyllableArray = splitStringIntoArrayOfSyllables(stringToBeConverted)

    for singleSyllable in pinyinSyllableArray{
        let toneNumber = getTone(singleSyllable)
        var singleSyllable = substituteVowelBasedOnToneNumber(singleSyllable, toneNumber)
        singleSyllable = singleSyllable.substringToIndex(singleSyllable.endIndex.predecessor())
        convertedPinyinSyllableArray.append(singleSyllable)
    }
    convertedString = " ".join(convertedPinyinSyllableArray)
    return convertedString
}

func splitStringIntoArrayOfSyllables(stringToBeConverted: String) -> [String]{
    return stringToBeConverted.componentsSeparatedByString(" ")
}

func getTone(string: String) -> Int{
    let characterArray: [Character] = Array(string)
    var toneNumber: Int = String(characterArray[characterArray.count-1]).toInt()!
    return toneNumber
}

func substituteVowelBasedOnToneNumber(string: String, toneNumber: Int) -> String {
    let vowelToBeReplaced = identifyVowelToBeReplaced(string)
    if toneNumber > 0 && toneNumber < 4 {
        return string.stringByReplacingOccurrencesOfString(vowelToBeReplaced, withString: substitutions[vowelToBeReplaced]![toneNumber - 1]) //will error if there is no toneNumber or the vowelToBeReplaced wasn't identified.
    }
    else {
        return string
    }
}

func identifyVowelToBeReplaced(string: String) -> String {
    var vowelToBeReplaced = String()
    if string.rangeOfString("a") != nil {
        vowelToBeReplaced = "a"
    }
    else if string.rangeOfString("ɑ") != nil {
        vowelToBeReplaced = "ɑ"
    }
    else if string.rangeOfString("e") != nil {
        vowelToBeReplaced = "e"
    }
    else if string.rangeOfString("ou") != nil {
        vowelToBeReplaced = "o"
    }
    else {
        var stringArray = Array(string)//.componentsSeparatedByString("")
        var reversedCharacterArray = stringArray.reverse()
        for character in reversedCharacterArray {
            switch character {
            case "i","o","u","v":
                vowelToBeReplaced = String(character)
                return vowelToBeReplaced
            default: vowelToBeReplaced = ""
            }
        }
    }
    return vowelToBeReplaced
}

let convertedPinyin = convertPinyinNumbersToToneMarks("ni3 hao3")

此代码适用于游乐场,但在将其添加到项目时无效。

functions.swift

import Foundation

func convertPinyinNumbersToToneMarks(stringToBeConverted: String) -> String{

    var convertedString = String()
    var stringToBeConverted = String()
    var convertedPinyinSyllableArray = [String]()

    let pinyinSyllableArray = splitStringIntoArrayOfSyllables(stringToBeConverted)

    for singleSyllable in pinyinSyllableArray{
        let toneNumber = getTone(singleSyllable)
        var singleSyllable = substituteVowelBasedOnToneNumber(singleSyllable, toneNumber)
        singleSyllable = singleSyllable.substringToIndex(singleSyllable.endIndex.predecessor())
        convertedPinyinSyllableArray.append(singleSyllable)
    }
    convertedString = " ".join(convertedPinyinSyllableArray)
    return convertedString
}

func splitStringIntoArrayOfSyllables(stringToBeConverted: String) -> [String]{
    return stringToBeConverted.componentsSeparatedByString(" ")
}

func getTone(string: String) -> Int{
    let characterArray: [Character] = Array(string)
    var toneNumber: Int = String(characterArray[characterArray.count-1]).toInt()!
    return toneNumber
}

func substituteVowelBasedOnToneNumber(string: String, toneNumber: Int) -> String {

    var substitutions: [String: Array] = Dictionary<String, Array<String>>()
    substitutions["a"] = ["ā", "á", "ǎ", "à"]
    substitutions["ɑ"] = ["ā", "á", "ǎ", "à"]
    substitutions["e"] = ["ē", "é", "ě", "è"]
    substitutions["i"] = ["ī", "í", "ǐ", "ì"]
    substitutions["o"] = ["ō", "ó", "ǒ", "ò"]
    substitutions["u"] = ["ū", "ú", "ǔ", "ù"]
    substitutions["ü"] = ["ǖ", "ǘ", "ǚ", "ǜ"]
    substitutions["v"] = ["ǖ", "ǘ", "ǚ", "ǜ"]
    substitutions["A"] = ["Ā", "Á", "Ǎ", "À"]
    substitutions["E"] = ["Ē", "É", "Ě", "È"]
    substitutions["I"] = ["Ī", "Í", "Ĭ", "Ì"]
    substitutions["O"] = ["Ō", "Ó", "Ǒ", "Ò"]
    substitutions["U"] = ["Ū", "Ú", "Ǔ", "Ù"]
    substitutions["Ü"] = ["Ǖ", "Ǘ", "Ǚ", "Ǜ"]
    substitutions["V"] = ["Ǖ", "Ǘ", "Ǚ", "Ǜ"]

    let vowelToBeReplaced = identifyVowelToBeReplaced(string)
    if toneNumber > 1 && toneNumber < 4 {
        return string.stringByReplacingOccurrencesOfString(vowelToBeReplaced, withString: substitutions[vowelToBeReplaced]![toneNumber - 1]) //will error if there is no toneNumber or the vowelToBeReplaced wasn't identified.
    }
    else {
        return string
    }
}

func identifyVowelToBeReplaced(string: String) -> String {
    var vowelToBeReplaced = String()
    if string.rangeOfString("a") != nil {
        vowelToBeReplaced = "a"
    }
    else if string.rangeOfString("ɑ") != nil {
        vowelToBeReplaced = "ɑ"
    }
    else if string.rangeOfString("e") != nil {
        vowelToBeReplaced = "e"
    }
    else if string.rangeOfString("ou") != nil {
        vowelToBeReplaced = "o"
    }
    else {
        var stringArray = Array(string)//.componentsSeparatedByString("")
        var reversedCharacterArray = stringArray.reverse()
        for character in reversedCharacterArray {
            switch character {
            case "i","o","u","v":
                vowelToBeReplaced = String(character)
                return vowelToBeReplaced
            default: vowelToBeReplaced = ""
            }
        }
    }
    return vowelToBeReplaced
}

关于为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:1)

在将游乐场移动到您的项目时,您似乎犯了一个小错误,从var stringToBeConverted = String()删除convertPinyinNumbersToToneMarks行,它就可以了。