Ios 更换文字以更改音高/键

Ios 更换文字以更改音高/键,ios,swift,string,textview,Ios,Swift,String,Textview,我对我的代码有很大的困难。我正在开发一个显示歌词和歌曲和弦的应用程序。我使用两个重叠的文本视图分离和弦和歌词 我在这个项目中遇到的问题是音高转换功能。我尽可能地向自己解释: 和弦共有12个:Do Do#-Re Re#-Mi Fa Fa#-Sol Sol#-La La#-Si 我用两个按钮+和-来改变音高 为了包含从一个和弦到另一个和弦的空格,我使用了replacingOccurrences(of:with:options:)instance方法,如下所示: //MARK: - Change pi

我对我的代码有很大的困难。我正在开发一个显示歌词和歌曲和弦的应用程序。我使用两个重叠的文本视图分离和弦和歌词

我在这个项目中遇到的问题是音高转换功能。我尽可能地向自己解释:

和弦共有12个:Do Do#-Re Re#-Mi Fa Fa#-Sol Sol#-La La#-Si

我用两个按钮+和-来改变音高

为了包含从一个和弦到另一个和弦的空格,我使用了
replacingOccurrences(of:with:options:)
instance方法,如下所示:

//MARK: - Change pitch It'll be much easier if you split the notes out into an array of strings (and have a temporary array to make edits to), and then have the raise and lower pitch functions +1 or -1 to each note each time. You can then collapse the chord array into a string to display it. This code here works:

var masterChords = ["Do",  "Do#",  "Re",  "Re#",  "Mi",  "Fa",  "Fa#",  "Sol",  "Sol#",  "La",  "La#",  "Si"]
var chords = ["Do", "Sol", "Mi"]

func raisePitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j < masterChords.count - 1 {
                    chords[i] = masterChords[j + 1]
                    break
                } else {
                    chords[i] = masterChords[0]
                    break
                }
            }
        }
    }
}

func lowerPitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j > 0 {
                    chords[i] = masterChords[j - 1]
                    break
                } else {
                    chords[i] = masterChords[masterChords.count - 1]
                    break
                }
            }
        }
    }
}


//Use the code below to test
print(chords)

raisePitch()

print(chords)

lowerPitch()

print(chords)

lowerPitch()

print(chords)

//标记:-更改音高如果您将音符拆分为一个字符串数组(并有一个临时数组进行编辑),然后每次对每个音符使用加高和降低音高函数+1或-1,将会更容易。然后可以将和弦数组折叠为字符串以显示它。此代码在这里起作用:

var主和弦=[“Do”、“Do”、“Re”、“Re”、“Mi”、“Fa”、“Fa”、“Sol”、“Sol”、“La”、“La”、“Si”]
var和弦=[“Do”,“Sol”,“Mi”]
func raiseptich(){
对于0…和弦中的i。计数-1{
对于0中的j…主和弦。计数-1{
如果和弦[i]==主和弦[j]{
如果j0{
和弦[i]=主和弦[j-1]
打破
}否则{
和弦[i]=主和弦[masterChords.count-1]
打破
}
}
}
}
}
//使用下面的代码进行测试
打印(和弦)
赖斯
打印(和弦)
lowerPitch()
打印(和弦)
lowerPitch()
打印(和弦)

非常感谢你这么快回答我,辛巴。是的,这非常有效,但是在实际情况中,我们需要改变不同的和弦。例如:让和弦=[“Do”,“Sol”,“Mi”]在这种情况下,上升+1应该变成[“Do”;“Sol”;“Fa”]没问题-我已经更新了代码,使它有一个主和弦数组(只包含所有可能的音符)和一个和弦数组(包含要上升或下降的音符)。现在,代码将比较这两个数组,当和弦数组中的音符与主和弦数组中的相应音符匹配时,它会将其升高/降低一个音符。希望这有帮助!