快速获取字符串的每个字符

时间:2016-12-09 22:49:44

标签: ios swift swift3 character

我正在接受60个字符的用户输入,所有十六进制字符,并将其分成4个段,因此给我15个4段,即15个单词。然后我取其中一个单词,将其转换为16个字符的二进制数。这16个字符每个映射到它们自己的特定错误代码,如果它是0,没有错误,如果它是1,错误。我试图找出是否有办法获取该二进制字符串的每个字符并将其映射到它自己的特定错误代码,如此。

    var error_str = "Not used:\(decNumb_str[0]) \nNot used:\(decNumb_str[1])"...... 

因此,每个单词给我16条错误消息。

我已经看过了:

Get nth character of a string in Swift programming language

但是,当我尝试完成此操作时,我不认为链接中显示的代码会对我有所帮助:

    let wordOne = UIAlertAction(title: "Word 1", style: .default)
  {
    (alert) -> Void in
    var str_start = 0
    var str_end = 56
    if self.textView.text == "" || self.textView.text == "No input value"
    {
      self.textView.text = "No input value"
    }
    else
    {
      let start_index = orig_hex_str.index(orig_hex_str.startIndex, offsetBy: str_start)
      let end_index = orig_hex_str.index(orig_hex_str.endIndex, offsetBy: -str_end)
      let sub_str = orig_hex_str[start_index..<end_index]
      let hexNumb_int = Int(sub_str, radix:16)
      let decNumb_str = String(hexNumb_int!, radix:2)
      let padded_str = String(self.pad(string: decNumb_str, toSize: 16))
      //Attempting to put error statement here
      //var error_str = "Not used:\(padded_str?.characters[0]) \nNot used:\(padded_str?.characters[1]) \nNot used:\(padded_str?.characters[2])"
      self.textView.text = padded_str
    }
  }
  wordSelection.addAction(wordOne)

以下是用户样本输入和样本输出的图像(如果用户要选择单词1,即用户输入的前4位数字):

enter image description here enter image description here

这是一个最小但可编译的示例,它获取用户输入的前4个字符并将其转换为二进制。在我输出它之前,我想获取该二进制文件的每个字符并将其传递给它自己的错误代码:

import UIKit
import Foundation
var initial_str = "0123456789abcdef"
// WORD 1
func pad(string : String, toSize: Int) -> String
{
    var padded = string
    for _ in 0..<toSize - string.characters.count
    {
        padded = "0" + padded
    }
    return padded
}

let str_start = 0
let str_end = 12
let start_index = initial_str.index(initial_str.startIndex, offsetBy: str_start)
let end_index = initial_str.index(initial_str.endIndex, offsetBy: -str_end)
let sub_str = initial_str  [start_index..<end_index]
let hexNumb_int = Int(sub_str, radix:16)
let decNumb_str = String(hexNumb_int!, radix:2)
let padded_str = pad(string: decNumb_str, toSize: 16)
//The commented segments below are what breaks the code above. I would like to put a string that
//Accesses each character of the binary and puts a message for each string
//let error_str = "Not used:\(padded_str.characters[0]) \nNot used:\(padded_str.characters[1]) \nNot used:\(padded_str.characters[2])"
//print(error_str)
print(padded_str)

提前感谢您的任何帮助

1 个答案:

答案 0 :(得分:1)

目前还不清楚你真正想做什么,但是你可以 字符串的每个字符map应用到characters属性,或者使用for-in

使用map

let error_str = padded_str.characters.map {"Not used:\($0)"}.joined(separator: " \n")
print(error_str)
/*↓
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:1
Not used:0
Not used:0
Not used:1
Not used:0
Not used:0
Not used:0
Not used:1
Not used:1
 */

或者以下for-in示例几乎相同:

var error_str = ""
for ch in padded_str.characters {
    if !error_str.isEmpty {
        error_str.append(" \n")
    }
    error_str.append("Not used:\(ch)")
}
print(error_str)