为什么我的 Swift 代码在 playground 中有效,但在 Xcode 项目中无效?
Why does my Swift code work in the playground but not in the Xcode project?
Here is a zip 个包含 playground 的项目文件:
游乐场代码:
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")
这段代码在 playground 中可以运行,但是当我将它添加到项目中时却不起作用。
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
}
关于为什么会发生这种情况有什么想法吗?
看来您在将 playground 移至您的项目时犯了一个小错误,从 convertPinyinNumbersToToneMarks
中删除 var stringToBeConverted = String()
行就可以了。
Here is a zip 个包含 playground 的项目文件:
游乐场代码:
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")
这段代码在 playground 中可以运行,但是当我将它添加到项目中时却不起作用。
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
}
关于为什么会发生这种情况有什么想法吗?
看来您在将 playground 移至您的项目时犯了一个小错误,从 convertPinyinNumbersToToneMarks
中删除 var stringToBeConverted = String()
行就可以了。