0

In my code I have an NSMutableArray. I want to get all values from that array into String and I want to send to the server. I used a for loop but it doesn't work. I don't know what I did wrong. Please help to fix the issue.

Here the code I tried.

var newString1 = String()
for i in (0..<self.idMutableArray.count)
{
    newString1 = (idMutableArray.object(at: i) as! NSString) as String
    print("newString1 is ",newString1)
}
print("newString1 is ",newString1)

The mutable array Format is:

idMutableArray is  (
        31,
        30,
        29,
        42,
        50
    )

When I am tried I get this format:

newString1 is  29
newString1 is  30
newString1 is  31
newString1 is  42
newString1 is  50

But I want to send this format to the server.

newString1 = 31,30,29,42,50

I don't know how to change that array to string format. Please help me.

7
  • 6
    Why are you using NSMutableArray instead of a Swift array? Why NSString instead of String? Commented May 15, 2017 at 15:05
  • Why dont you iterate with the object instead? I mean: for i in idMutableArray { newString = i as! NSString } Commented May 15, 2017 at 15:09
  • I am new for Swift and don't know for how to use swift array? Commented May 15, 2017 at 15:09
  • 1
    You don't need a loop for this, just use componentsJoined (or joined for a Swift array). Commented May 15, 2017 at 15:12
  • 1
    If you don't know how to use Swift arrays then you need to read the Collection Types section of the "The Swift Programming Language" book (along with the rest of the book). Commented May 15, 2017 at 15:13

4 Answers 4

2

Avoid using NS collection types in favor of standart Swift counterparts Array and Dictionary. You can start reading this if you want to find out more about them.

let idMutableArray = ["31", "30", "29", "42", "50"]
let newString1 = idMutableArray.joined(separator: ",")

However, if you have to use NSMutableArray, here is what you can do:

let idMutableArray = NSMutableArray(objects: "31", "30", "29", "42", "50")
let newString1 = idMutableArray.idMutableArray.componentsJoined(by: ",")

print(newString1) # will print "31,30,29,42,50"
Sign up to request clarification or add additional context in comments.

3 Comments

While this is the most direct answer, the OP should not be using NSMutableArray.
let newString1 = idMutableArray.joined(separator: ",") it is got some validation problem in next functionality.
what do you mean by validation problem?
1

If you want all the values in one string, you should add them to that string and not to override it in each iteration.

var newString1 = ""
for i in (0..<idMutableArray.count)
{
    newString1 += String(describing: idMutableArray[i])
    if i < idMutableArray.count - 1 {
        newString1 += ","
    }
    print("newString1 is ",newString1)
}
print("newString1 is ",newString1)

Comments

1

First of all you don't need NSMutableArray. Use Swift Array

let idMutableArray = ["31", "30", "29", "42", "50"]

Second of all you don't need a repeat loop, just join the strings.

let newString1 = idMutableArray.joined(separator: ",")
print("newString1 is ",newString1)

4 Comments

Given the code in the question, it appears to be an array of NSString, not an array of numbers.
Thanks, I was confused about the array dump (missing double quotes)
let newString1 = idMutableArray.joined(separator: ",") it is got some validation problem in next functionality.
Change all Foundation types to Swift types. It's worth it.
1

You're assigning a new value to your string each time your for loop runs:

newString1 = (idMutableArray.object(at: i) as! NSString) as String

You can use joined(separator:) to get the desired output:

let array = ["1", "2", "3", "4", "5"]
let string = array.joined(separator: ",")
print("\(string)") // Prints "1,2,3,4,5"

1 Comment

This is wrong. It prints 1,2,3,4,5,. Note the trailing comma.

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.