0

I've two cases where I need to convert strings to different formats.

for ex:

case 1:
  string inputs: abc, xyz, mno, & llr   // All Strings from a dictionary
  output: ["abc","xyz", "mno", "llr"]  //I need to get the String array like this.

But when I use this code:

 var stringBuilder:[String] = [];
 for i in 0..<4 {
   stringBuilder.append("abc"); //Appends all four Strings from a Dictionary
 }

print(stringBuilder); //Output is 0: abc, 1:xyz like that, how to get desired format of that array like ["abc", "xyz"];

Real usage:

let arr = Array(stringReturn.values);
//print(arr)  // Great, it prints ["abc","xyz"];
let context = JSContext()
context?.evaluateScript(stringBuilder)   
let testFunction = context?.objectForKeyedSubscript("KK")
let result = testFunction?.call(withArguments:arr); // Here when I debugger enabled array is passed to call() like 0:"abc" 1:"xyz". where as it should be passed as above print.

Secondly how to replace escape chars in swift: I used "\" in replaceOccurances(of:"\\'" with:"'"); but its unchanged. why and how to escape that sequnce.

case 2:
  string input: \'abc\'
  output: 'abc'
3
  • Can you edit your question to contain the definition of your dictionary? Commented Mar 5, 2018 at 10:41
  • var stringReturn:Dictionary = Dictionary<String,Any>(); is the definition. Commented Mar 5, 2018 at 10:42
  • even [String:Any] is also printing the same na Commented Mar 5, 2018 at 10:51

3 Answers 3

1

To get all values of your dictionary as an array you can use the values property of the dictionary:

let dictionary: Dictionary<String, Any> = [
    "key_a": "value_a",
    "key_b": "value_b",
    "key_c": "value_c",
    "key_d": "value_d",
    "key_e": 3
]

let values = Array(dictionary.values)
// values: ["value_a", "value_b", "value_c", "value_d", 3]

With filter you can ignore all values of your dictionary that are not of type String:

let stringValues = values.filter({ $0 is String }) as! [String]
// stringValues: ["value_a", "value_b", "value_c", "value_d"]

With map you can transform the values of stringValues and apply your replacingOccurrences function:

let adjustedValues = stringValues.map({ $0.replacingOccurrences(of: "value_", with: "") })
// adjustedValues: ["a", "b", "c", "d"]
Sign up to request clarification or add additional context in comments.

6 Comments

You output is like this: LazyMapCollection<Dictionary<String, Any>, Any>(_base: ["key_a": "value_a", "key_b": "value_b"]
Well strange. I tested my code against Swift 3.2 and Swift 4 before posting. Maybe you are missing some information from your setup?
When I print the values its printed ["abc", "xyz"] but when I pass it like find(values) then after hitting debug point, it shows like 0:abc, 1:xyz
That is because the value at index 0 has the value abc and the value at index 1 has xyz I don't see the problem here. Maybe you can tell us more what you are trying to accomplish by extending your question.
see updated question. There call() need an array like ["xyz","abc"]
|
0

Why not try something like this? For part 1 of the question that is:

var stringReturn: Dictionary = Dictionary<String,Any>()
stringReturn = ["0": "abc","1": "def","2": "ghi"]
print(stringReturn)

var stringBuilder = [String]()
for i in stringReturn {
  stringBuilder.append(String(describing: i.value))
}
print(stringBuilder)

Also, part 2 seems to be trivial unless I'm not mistaken

var escaped: String = "\'abc\'"
print(escaped)

Comments

0

case 1: I have Implemented this solutions, Hope this will solve your problem

   let dict: [String: String] = ["0": "Abc", "1": "CDF", "2": "GHJ"]                
   var array: [String] = []

for (k, v) in dict.enumerated() {
    print(k)
    print(v.value)
    array.append(v.value)  
}
print(array)

case 2:

    var str = "\'abc\'"
    print(str.replacingOccurrences(of: "\'", with: ""))

2 Comments

I need a array of format ["abc","xyz","mno"] where in your code will produce this.
Please read my question carefully. I already did what you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.