Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何将一个数组中的每个字母与另一个数组中的所有字母进行比较敏捷的_Arrays_String_Swift_Swift2_Character - Fatal编程技术网

Arrays 如何将一个数组中的每个字母与另一个数组中的所有字母进行比较敏捷的

Arrays 如何将一个数组中的每个字母与另一个数组中的所有字母进行比较敏捷的,arrays,string,swift,swift2,character,Arrays,String,Swift,Swift2,Character,我有两个不同的数组:scriptedchar和alphabet。我需要检查ScriptedChar中的第一个字符(所以是“ScriptedChar[0]),并检查字母表中的对应关系。 例如: scriptedchar/[“d”、“e”、“c”、“b”] 字母表//[“a”、“b”、“c”等] 我想从ScriptedChar[0]中提取d,检查所有字母表中是否都有“d”,然后保存“d”在第二个数组中的位置。 我还需要增加ScriptedChar括号内的数字。我将从用户处获取数字。 你能帮帮我吗?谢

我有两个不同的数组:scriptedchar和alphabet。我需要检查ScriptedChar中的第一个字符(所以是“ScriptedChar[0]),并检查字母表中的对应关系。 例如: scriptedchar/[“d”、“e”、“c”、“b”] 字母表//[“a”、“b”、“c”等] 我想从ScriptedChar[0]中提取d,检查所有字母表中是否都有“d”,然后保存“d”在第二个数组中的位置。 我还需要增加ScriptedChar括号内的数字。我将从用户处获取数字。 你能帮帮我吗?谢谢

func decript() {
    var criptedText = incriptedText.text!   //get text from uiTextField
    var criptedChar = Array<Character>(criptedText.characters)  //from text to char & all in array :D
    var alfabeto: Array<Character> = ["a","b", "c", "d", "e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    var capacityCriptedCharArray = criptedChar.capacity

    for (var i = 0; i < 26; i++) {
        if criptedChar[0] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }


    for (var i = 0; i < 26; i++) {
        if criptedChar[1] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }

    for (var i = 0; i < 26; i++) {
        if criptedChar[2] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }   
}
func decript(){
var scriptedText=incriptedText.text!//从uiTextField获取文本
var scriptedchar=Array(scriptedtext.characters)//从文本到字符&数组中的所有字符:D
变量alfabeto:Array=[“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”]
var capacityCriptedCharArray=criptedChar.capacity
对于(变量i=0;i<26;i++){
如果scriptedchar[0]==alfabeto[i]{
decriptedText.text=decriptedText.text!+“\(来自SecondViewController[i]的新邮件)”
}
}
对于(变量i=0;i<26;i++){
如果scriptedchar[1]==alfabeto[i]{
decriptedText.text=decriptedText.text!+“\(来自SecondViewController[i]的新邮件)”
}
}
对于(变量i=0;i<26;i++){
如果scriptedchar[2]==alfabeto[i]{
decriptedText.text=decriptedText.text!+“\(来自SecondViewController[i]的新邮件)”
}
}   
}

这段代码可以工作,但它很愚蠢,我无法控制用户输入

如果我正确理解了您的问题,您正在寻找 类似这样的解释(内联解释):

或者,您可以从中创建查找词典 两个阵列:

var mapping = [ Character : Character ]()
zip(alphabet, newLetters).forEach {
    mapping[$0] = $1
}
然后将输入的每个字符映射到 字典:

let decryptedText = Array(cryptedText.characters
        .map { mapping[$0] }
        .flatMap { $0 }
    )

(此处
flatMap
用于从输入数组中不存在的字符中过滤出
nil
s。)

以下是一些简单的密码逻辑示例,您可以根据应用程序进行调整:

let readableText = "the quick brown fox jumped over the lazy dog"

// letters: letters in readable text that will be encoded
// cypher : corresponding encoded letters
//
// note: letters and cypher must have the same number of elements
let letters:[Character] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let cypher:[Character]  = ["o","p","q","r","a","b","c","d","e","f","g","h","i","u","v","w","x","y","z","j","k","l","m","n","s","t"]

// build a mapping disctionary from readable to encoded
var encode:[Character:Character] = [:]
for (index, letter) in letters.enumerate() { encode[letter] = cypher[index] } 

// encrypt the readble text  gives: "jda xkeqg pyvmu bvn fkiwar vlay jda hots rvc"
let cryptedText = String(readableText.characters.map({ encode[$0] ?? $0 })) 


// build a mapping disctionary from encoded to readable
var decode:[Character:Character] = [:]
for (index, letter) in cypher.enumerate() { decode[letter] = letters[index] }

// decrypted the encrypted text  gives: "the quick brown fox jumped over the lazy dog"
let decryptedText = String(cryptedText.characters.map({ decode[$0] ?? $0 })) 

非常感谢!我使用了您写的第一种方法:)谢谢!我没有在这个项目中使用它们,但它们可能对其他项目有用:))
let readableText = "the quick brown fox jumped over the lazy dog"

// letters: letters in readable text that will be encoded
// cypher : corresponding encoded letters
//
// note: letters and cypher must have the same number of elements
let letters:[Character] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let cypher:[Character]  = ["o","p","q","r","a","b","c","d","e","f","g","h","i","u","v","w","x","y","z","j","k","l","m","n","s","t"]

// build a mapping disctionary from readable to encoded
var encode:[Character:Character] = [:]
for (index, letter) in letters.enumerate() { encode[letter] = cypher[index] } 

// encrypt the readble text  gives: "jda xkeqg pyvmu bvn fkiwar vlay jda hots rvc"
let cryptedText = String(readableText.characters.map({ encode[$0] ?? $0 })) 


// build a mapping disctionary from encoded to readable
var decode:[Character:Character] = [:]
for (index, letter) in cypher.enumerate() { decode[letter] = letters[index] }

// decrypted the encrypted text  gives: "the quick brown fox jumped over the lazy dog"
let decryptedText = String(cryptedText.characters.map({ decode[$0] ?? $0 }))