How to convert Characters to Int in Swift 4? - ios

I have readLine() which gives me the character. So, I have this:
223 345 567 and so on. So, when I convert them to Int first thing says these are character so, I researched and found this solution and when I use this:
let size = readLine()
var array = [Int]()
let numbers = readLine()
for number in numbers!
{
if let integer = Int(String(number))
{
array.append(integer)
}
}
print(array)
So, when I am printing the array, I am getting these as [2,2,3,3,4,5,5,6,7] instead of [223,345,567]. Can anyone help?
Here is the extract of new code.
let numbers = readLine()
var array = [Int]()
guard let numberStrings = numbers?.components(separatedBy: " ") else{
fatalError()
}
for number in numberStrings {
if let validNumber = Int(number) {
array.append(validNumber)
}
}
print(array)

You need to split the string and find every number string and then convert them to Int.
let numbers = readLine()
var numberArray: [Int] = []
guard let numberStrings = numbers?.components(separatedBy: " ") else {
fatalError()
}
for number in numberStrings {
if let validNumber = Int(number) {
numberArray.append(validNumber)
}
}

for number in numbers!
This line of code extracts each character in the string (223 345 567) and tries to convert it to int. That's why it converts each valid number in string and left the spaces.
You need to first split the string in to array of string numbers then iterate through the array to convert them.
Split it, further iterate through strNumArray and convert them to integers
var array = [Int]()
if let strNumArray = numbers?.components(separatedBy: " ") {
for number in strNumArray {
if let integer = Int(String(number)) {
array.append(integer)
}
}
}
print(array)
And in more swifty way
var arrayInt = numbers.split(separator: " ").map{Int($0)}

Related

iOS Swift - print from Set to Label [duplicate]

I know how to programmatically do it, but I'm sure there's a built-in way...
Every language I've used has some sort of default textual representation for a collection of objects that it will spit out when you try to concatenate the Array with a string, or pass it to a print() function, etc. Does Apple's Swift language have a built-in way of easily turning an Array into a String, or do we always have to be explicit when stringifying an array?
If the array contains strings, you can use the String's join method:
var array = ["1", "2", "3"]
let stringRepresentation = "-".join(array) // "1-2-3"
In Swift 2:
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
This can be useful if you want to use a specific separator (hypen, blank, comma, etc).
Otherwise you can simply use the description property, which returns a string representation of the array:
let stringRepresentation = [1, 2, 3].description // "[1, 2, 3]"
Hint: any object implementing the Printable protocol has a description property. If you adopt that protocol in your own classes/structs, you make them print friendly as well
In Swift 3
join becomes joined, example [nil, "1", "2"].flatMap({$0}).joined()
joinWithSeparator becomes joined(separator:) (only available to Array of Strings)
In Swift 4
var array = ["1", "2", "3"]
array.joined(separator:"-")
With Swift 5, according to your needs, you may choose one of the following Playground sample codes in order to solve your problem.
Turning an array of Characters into a String with no separator:
let characterArray: [Character] = ["J", "o", "h", "n"]
let string = String(characterArray)
print(string)
// prints "John"
Turning an array of Strings into a String with no separator:
let stringArray = ["Bob", "Dan", "Bryan"]
let string = stringArray.joined(separator: "")
print(string) // prints: "BobDanBryan"
Turning an array of Strings into a String with a separator between words:
let stringArray = ["Bob", "Dan", "Bryan"]
let string = stringArray.joined(separator: " ")
print(string) // prints: "Bob Dan Bryan"
Turning an array of Strings into a String with a separator between characters:
let stringArray = ["car", "bike", "boat"]
let characterArray = stringArray.flatMap { $0 }
let stringArray2 = characterArray.map { String($0) }
let string = stringArray2.joined(separator: ", ")
print(string) // prints: "c, a, r, b, i, k, e, b, o, a, t"
Turning an array of Floats into a String with a separator between numbers:
let floatArray = [12, 14.6, 35]
let stringArray = floatArray.map { String($0) }
let string = stringArray.joined(separator: "-")
print(string)
// prints "12.0-14.6-35.0"
Swift 2.0 Xcode 7.0 beta 6 onwards uses joinWithSeparator() instead of join():
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
joinWithSeparator is defined as an extension on SequenceType
extension SequenceType where Generator.Element == String {
/// Interpose the `separator` between elements of `self`, then concatenate
/// the result. For example:
///
/// ["foo", "bar", "baz"].joinWithSeparator("-|-") // "foo-|-bar-|-baz"
#warn_unused_result
public func joinWithSeparator(separator: String) -> String
}
Swift 3
["I Love","Swift"].joined(separator:" ") // previously joinWithSeparator(" ")
Since no one has mentioned reduce, here it is:
[0, 1, 1, 0].map {"\($0)"}.reduce("") { $0 + $1 } // "0110"
In the spirit of functional programming 🤖
In Swift 4
let array:[String] = ["Apple", "Pear ","Orange"]
array.joined(separator: " ")
To change an array of Optional/Non-Optional Strings
//Array of optional Strings
let array : [String?] = ["1",nil,"2","3","4"]
//Separator String
let separator = ","
//flatMap skips the nil values and then joined combines the non nil elements with the separator
let joinedString = array.flatMap{ $0 }.joined(separator: separator)
//Use Compact map in case of **Swift 4**
let joinedString = array.compactMap{ $0 }.joined(separator: separator
print(joinedString)
Here flatMap, compactMap skips the nil values in the array and appends the other values to give a joined string.
Nowadays, in iOS 13+ and macOS 10.15+, we might use ListFormatter:
let formatter = ListFormatter()
let names = ["Moe", "Larry", "Curly"]
if let string = formatter.string(from: names) {
print(string)
}
That will produce a nice, natural language string representation of the list. A US user will see:
Moe, Larry, and Curly
It will support any languages for which (a) your app has been localized; and (b) the user’s device is configured. For example, a German user with an app supporting German localization, would see:
Moe, Larry und Curly
Mine works on NSMutableArray with componentsJoinedByString
var array = ["1", "2", "3"]
let stringRepresentation = array.componentsJoinedByString("-") // "1-2-3"
In Swift 2.2 you may have to cast your array to NSArray to use componentsJoinedByString(",")
let stringWithCommas = (yourArray as NSArray).componentsJoinedByString(",")
let arrayTemp :[String] = ["Mani","Singh","iOS Developer"]
let stringAfterCombining = arrayTemp.componentsJoinedByString(" ")
print("Result will be >>> \(stringAfterCombining)")
Result will be >>> Mani Singh iOS Developer
If you want to ditch empty strings in the array.
["Jet", "Fire"].filter { !$0.isEmpty }.joined(separator: "-")
If you want to filter nil values as well:
["Jet", nil, "", "Fire"].flatMap { $0 }.filter { !$0.isEmpty }.joined(separator: "-")
you can use joined() to get a single String when you have array of struct also.
struct Person{
let name:String
let contact:String
}
You can easily produce string using map() & joined()
PersonList.map({"\($0.name) - \($0.contact)"}).joined(separator: " | ")
output:
Jhon - 123 | Mark - 456 | Ben - 789
if you want convert custom object array to string or comma separated string (csv) you can use
var stringIds = (self.mylist.map{$0.id ?? 0}).map{String($0)}.joined(separator: ",")
credit to : urvish modi
post: Convert an array of Ints to a comma separated string
A separator can be a bad idea for some languages like Hebrew or Japanese.
Try this:
// Array of Strings
let array: [String] = ["red", "green", "blue"]
let arrayAsString: String = array.description
let stringAsData = arrayAsString.data(using: String.Encoding.utf16)
let arrayBack: [String] = try! JSONDecoder().decode([String].self, from: stringAsData!)
For other data types respectively:
// Set of Doubles
let set: Set<Double> = [1, 2.0, 3]
let setAsString: String = set.description
let setStringAsData = setAsString.data(using: String.Encoding.utf16)
let setBack: Set<Double> = try! JSONDecoder().decode(Set<Double>.self, from: setStringAsData!)
You can either use loops for getting this done. Or by using map.
By mapping:
let array = ["one" , "two" , "three"]
array.map({$0}).joined(seperator : ",")
so in separator you can modify the string.
Output-> ("one,two,three")
The Swift equivalent to what you're describing is string interpolation. If you're thinking about things like JavaScript doing "x" + array, the equivalent in Swift is "x\(array)".
As a general note, there is an important difference between string interpolation vs the Printable protocol. Only certain classes conform to Printable. Every class can be string interpolated somehow. That's helpful when writing generic functions. You don't have to limit yourself to Printable classes.
You can print any object using the print function
or use \(name) to convert any object to a string.
Example:
let array = [1,2,3,4]
print(array) // prints "[1,2,3,4]"
let string = "\(array)" // string == "[1,2,3,4]"
print(string) // prints "[1,2,3,4]"
Create extension for an Array:
extension Array {
var string: String? {
do {
let data = try JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted])
return String(data: data, encoding: .utf8)
} catch {
return nil
}
}
}
if you have string array list , then convert to Int
let arrayList = list.map { Int($0)!}
arrayList.description
it will give you string value
for any Element type
extension Array {
func joined(glue:()->Element)->[Element]{
var result:[Element] = [];
result.reserveCapacity(count * 2);
let last = count - 1;
for (ix,item) in enumerated() {
result.append(item);
guard ix < last else{ continue }
result.append(glue());
}
return result;
}
}
Try This:
let categories = dictData?.value(forKeyPath: "listing_subcategories_id") as! NSMutableArray
let tempArray = NSMutableArray()
for dc in categories
{
let dictD = dc as? NSMutableDictionary
tempArray.add(dictD?.object(forKey: "subcategories_name") as! String)
}
let joinedString = tempArray.componentsJoined(by: ",")
use this when you want to convert list of struct type into string
struct MyStruct {
var name : String
var content : String
}
let myStructList = [MyStruct(name: "name1" , content: "content1") , MyStruct(name: "name2" , content: "content2")]
and covert your array like this way
let myString = myStructList.map({$0.name}).joined(separator: ",")
will produce ===> "name1,name2"
FOR SWIFT 3:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == phoneField
{
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
let components = newString.components(separatedBy: NSCharacterSet.decimalDigits.inverted)
let decimalString = NSString(string: components.joined(separator: ""))
let length = decimalString.length
let hasLeadingOne = length > 0 && decimalString.character(at: 0) == (1 as unichar)
if length == 0 || (length > 10 && !hasLeadingOne) || length > 11
{
let newLength = NSString(string: textField.text!).length + (string as NSString).length - range.length as Int
return (newLength > 10) ? false : true
}
var index = 0 as Int
let formattedString = NSMutableString()
if hasLeadingOne
{
formattedString.append("1 ")
index += 1
}
if (length - index) > 3
{
let areaCode = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("(%#)", areaCode)
index += 3
}
if length - index > 3
{
let prefix = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("%#-", prefix)
index += 3
}
let remainder = decimalString.substring(from: index)
formattedString.append(remainder)
textField.text = formattedString as String
return false
}
else
{
return true
}
}
If you question is something like this:
tobeFormattedString = ["a", "b", "c"]
Output = "abc"
String(tobeFormattedString)

Error while trying to convert the Array to Int

So i've taken my bundle version and save it in an array but it s of type String. I ve tried and .map it to be Int but i receive Cannot invoke initializer for type 'Int' with an argument list of type''((String.Element))
let versionName = String(describing: Bundle.main.infoDictionary!["CFBundleShortVersionString"]!) // 1.9.1
let versionNumeral = Array(versionName.westernArabicNumeralsOnly) // ["1","9","1"]
let convertedToInt = versionNumeral.map {Int($0)} // so i want it here to be [1, 9, 1] so I can add them between them.
for element in convertedToInt {
total += element
}
.westernArabicNumeralsOnly is a string extension that removes my dots from version and only let the numbers
extension String {
var westernArabicNumeralsOnly: String {
let pattern = UnicodeScalar("0")..."9"
return String(unicodeScalars
.flatMap { pattern ~= $0 ? Character($0) : nil })
}
}
There is no direct conversion between Characters and Ints. If you are wanting to extract the version numbers from the string the following might be a better way to do it. It would also allow for version numbers greater than 9
let versionName = String(describing: Bundle.main.infoDictionary!["CFBundleShortVersionString"]!) // 1.9.1
let versionComponents = versionName.components(separatedBy: ".")
let convertedToInt = versionComponents.map {Int($0)}
This version would output an array of Int?. If you want an array of Int then you could use
let convertedToInt = versionComponents.map {Int($0)!}
but you'd have to be sure you aren't sent a string with non-numerical characters in it.
I'd use this:
let versionString = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? ""
let versionInts = versionString.components(separatedBy: ".").map { Int($0) ?? 0 }
Fetch number easily without westernArabicNumeralsOnly function
let versionName = String(describing: Bundle.main.infoDictionary!["CFBundleShortVersionString"]!) // 1.9.1
let versionNumeral = Array(versionName.components(separatedBy: ".")) // ["1","9","1"]
let convertedToInt = versionNumeral.map{Int($0)} // [1,9,1]
var total = 0
for element in convertedToInt {
total = total + element!
}
print(total) // 11

How can I remove or replace all punctuation characters from a String?

I have a string composed of words, some of which contain punctuation, which I would like to remove, but I have been unable to figure out how to do this.
For example if I have something like
var words = "Hello, this : is .. a string?"
I would like to be able to create an array with
"[Hello, this, is, a, string]"
My original thought was to use something like words.stringByTrimmingCharactersInSet() to remove any characters I didn't want but that would only take characters off the ends.
I thought maybe I could iterate through the string with something in the vein of
for letter in words {
if NSCharacterSet.punctuationCharacterSet.characterIsMember(letter){
//remove that character from the string
}
}
but I'm unsure how to remove the character from the string. I'm sure there are some problems with the way that if statement is set up, as well, but it shows my thought process.
Xcode 11.4 • Swift 5.2 or later
extension StringProtocol {
var words: [SubSequence] {
split(whereSeparator: \.isLetter.negation)
}
}
extension Bool {
var negation: Bool { !self }
}
let sentence = "Hello, this : is .. a string?"
let words = sentence.words // ["Hello", "this", "is", "a", "string"]
String has a enumerateSubstringsInRange() method.
With the .ByWords option, it detects word boundaries and
punctuation automatically:
Swift 3/4:
let string = "Hello, this : is .. a \"string\"!"
var words : [String] = []
string.enumerateSubstrings(in: string.startIndex..<string.endIndex,
options: .byWords) {
(substring, _, _, _) -> () in
words.append(substring!)
}
print(words) // [Hello, this, is, a, string]
Swift 2:
let string = "Hello, this : is .. a \"string\"!"
var words : [String] = []
string.enumerateSubstringsInRange(string.characters.indices,
options: .ByWords) {
(substring, _, _, _) -> () in
words.append(substring!)
}
print(words) // [Hello, this, is, a, string]
This works with Xcode 8.1, Swift 3:
First define general-purpose extension for filtering by CharacterSet:
extension String {
func removingCharacters(inCharacterSet forbiddenCharacters:CharacterSet) -> String
{
var filteredString = self
while true {
if let forbiddenCharRange = filteredString.rangeOfCharacter(from: forbiddenCharacters) {
filteredString.removeSubrange(forbiddenCharRange)
}
else {
break
}
}
return filteredString
}
}
Then filter using punctuation:
let s:String = "Hello, world!"
s.removingCharacters(inCharacterSet: CharacterSet.punctuationCharacters) // => "Hello world"
let charactersToRemove = NSCharacterSet.punctuationCharacterSet().invertedSet
let aWord = "".join(words.componentsSeparatedByCharactersInSet(charactersToRemove))
An alternate way to filter characters from a set and obtain an array of words is by using the array's filter and reduce methods. It's not as compact as other answers, but it shows how the same result can be obtained in a different way.
First define an array of the characters to remove:
let charactersToRemove = Set(Array(".:?,"))
next convert the input string into an array of characters:
let arrayOfChars = Array(words)
Now we can use reduce to build a string, obtained by appending the elements from arrayOfChars, but skipping all the ones included in charactersToRemove:
let filteredString = arrayOfChars.reduce("") {
let str = String($1)
return $0 + (charactersToRemove.contains($1) ? "" : str)
}
This produces a string without the punctuation characters (as defined in charactersToRemove).
The last 2 steps:
split the string into an array of words, using the blank character as separator:
let arrayOfWords = filteredString.componentsSeparatedByString(" ")
last, remove all empty elements:
let finalArrayOfWords = arrayOfWords.filter { $0.isEmpty == false }
NSScaner way:
let words = "Hello, this : is .. a string?"
//
let scanner = NSScanner(string: words)
var wordArray:[String] = []
var word:NSString? = ""
while(!scanner.atEnd) {
var sr = scanner.scanCharactersFromSet(NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ"), intoString: &word)
if !sr {
scanner.scanLocation++
continue
}
wordArray.append(String(word!))
}
println(wordArray)

SwiftCsv Parser Support for DoubleQuotes

Im using Swiftcsv library to parse CSV file.
How to ignore Comma delimiter for strings within DoubleQuotes e.g "Abcd, went to Apple" ?
here the parser takes Abcd as one Value and went to Apple as another value.
Code :
func parseRows(fromLines lines: [String]) -> [Dictionary<String, String>] {
var rows: [Dictionary<String, String>] = []
for (lineNumber, line) in enumerate(lines) {
if lineNumber == 0 {
continue
}
var row = Dictionary<String, String>()
let values = line.componentsSeparatedByCharactersInSet(self.delimiter)
for (index, header) in enumerate(self.headers) {
let value = values[index]
row[header] = value
}
rows.append(row)
}
return rows
}
How can i change line.componentsSeparatedByCharactersInSet(self.delimiter) to ignore commas within Doublequotes?
Use this instead https://github.com/Daniel1of1/CSwiftV
Had the same issue. It took me an hour or so to figure out the problem is that SwiftCSV doesn't, erm, work.
Text in CSV files is inside quotes so commas and newlines in a CSV don't screw up the parser. I looked at the SwiftCSV source and there is no support for that - meaning that any commas or newlines screw up the parsing.
You could patch up SwiftCSV, or just go with CSwiftV I linked above.
I don't know if you are still looking for a solution, but I just came up with a quick way to do this as it is a problem that just came up for me.
The code isn't full proof because I am only using it for a side project so if you want to handle more cases you probably will need to make some changes.
func parseRows(fromLines lines: [String]) -> [Dictionary<String, String>] {
var rows: [Dictionary<String, String>] = []
for (lineNumber, line) in enumerate(lines) {
if lineNumber == 0 {
continue
}
var row = Dictionary<String, String>()
// escape commas in the string when it is surrounded by quotes
let convertedLine = NSString(string: line) // have to convert string to NSString because string does not have all NSString API
var escapedLine = line
var searchRange = NSMakeRange(1,convertedLine.length)
var foundRange:NSRange
if NSString(string: line).containsString("\"")
{
while (searchRange.location < convertedLine.length) {
searchRange.length = convertedLine.length-searchRange.location
foundRange = convertedLine.rangeOfString("\"", options: nil, range: searchRange)
if (foundRange.location != NSNotFound) {
// found a quotation mark
searchRange.location = foundRange.location+foundRange.length
let movieTitle = convertedLine.substringToIndex(foundRange.location)
escapedLine = convertedLine.stringByReplacingOccurrencesOfString(",", withString: "&c", options: nil, range: NSMakeRange(0,foundRange.location))
} else {
// no more substring to find
break
}
}
}
var values = escapedLine.componentsSeparatedByCharactersInSet(self.delimiter)
for (index, header) in enumerate(self.headers) {
var value = values[index]
//reinsert commas if they were escaped and get rid of quotation marks
value = value.stringByReplacingOccurrencesOfString("\"", withString: "")
value = value.stringByReplacingOccurrencesOfString("&c", withString: ",")
row[header] = value
}
rows.append(row)
}
return rows
}

How do I convert a Swift Array to a String?

I know how to programmatically do it, but I'm sure there's a built-in way...
Every language I've used has some sort of default textual representation for a collection of objects that it will spit out when you try to concatenate the Array with a string, or pass it to a print() function, etc. Does Apple's Swift language have a built-in way of easily turning an Array into a String, or do we always have to be explicit when stringifying an array?
If the array contains strings, you can use the String's join method:
var array = ["1", "2", "3"]
let stringRepresentation = "-".join(array) // "1-2-3"
In Swift 2:
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
This can be useful if you want to use a specific separator (hypen, blank, comma, etc).
Otherwise you can simply use the description property, which returns a string representation of the array:
let stringRepresentation = [1, 2, 3].description // "[1, 2, 3]"
Hint: any object implementing the Printable protocol has a description property. If you adopt that protocol in your own classes/structs, you make them print friendly as well
In Swift 3
join becomes joined, example [nil, "1", "2"].flatMap({$0}).joined()
joinWithSeparator becomes joined(separator:) (only available to Array of Strings)
In Swift 4
var array = ["1", "2", "3"]
array.joined(separator:"-")
With Swift 5, according to your needs, you may choose one of the following Playground sample codes in order to solve your problem.
Turning an array of Characters into a String with no separator:
let characterArray: [Character] = ["J", "o", "h", "n"]
let string = String(characterArray)
print(string)
// prints "John"
Turning an array of Strings into a String with no separator:
let stringArray = ["Bob", "Dan", "Bryan"]
let string = stringArray.joined(separator: "")
print(string) // prints: "BobDanBryan"
Turning an array of Strings into a String with a separator between words:
let stringArray = ["Bob", "Dan", "Bryan"]
let string = stringArray.joined(separator: " ")
print(string) // prints: "Bob Dan Bryan"
Turning an array of Strings into a String with a separator between characters:
let stringArray = ["car", "bike", "boat"]
let characterArray = stringArray.flatMap { $0 }
let stringArray2 = characterArray.map { String($0) }
let string = stringArray2.joined(separator: ", ")
print(string) // prints: "c, a, r, b, i, k, e, b, o, a, t"
Turning an array of Floats into a String with a separator between numbers:
let floatArray = [12, 14.6, 35]
let stringArray = floatArray.map { String($0) }
let string = stringArray.joined(separator: "-")
print(string)
// prints "12.0-14.6-35.0"
Swift 2.0 Xcode 7.0 beta 6 onwards uses joinWithSeparator() instead of join():
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
joinWithSeparator is defined as an extension on SequenceType
extension SequenceType where Generator.Element == String {
/// Interpose the `separator` between elements of `self`, then concatenate
/// the result. For example:
///
/// ["foo", "bar", "baz"].joinWithSeparator("-|-") // "foo-|-bar-|-baz"
#warn_unused_result
public func joinWithSeparator(separator: String) -> String
}
Swift 3
["I Love","Swift"].joined(separator:" ") // previously joinWithSeparator(" ")
Since no one has mentioned reduce, here it is:
[0, 1, 1, 0].map {"\($0)"}.reduce("") { $0 + $1 } // "0110"
In the spirit of functional programming 🤖
In Swift 4
let array:[String] = ["Apple", "Pear ","Orange"]
array.joined(separator: " ")
To change an array of Optional/Non-Optional Strings
//Array of optional Strings
let array : [String?] = ["1",nil,"2","3","4"]
//Separator String
let separator = ","
//flatMap skips the nil values and then joined combines the non nil elements with the separator
let joinedString = array.flatMap{ $0 }.joined(separator: separator)
//Use Compact map in case of **Swift 4**
let joinedString = array.compactMap{ $0 }.joined(separator: separator
print(joinedString)
Here flatMap, compactMap skips the nil values in the array and appends the other values to give a joined string.
Nowadays, in iOS 13+ and macOS 10.15+, we might use ListFormatter:
let formatter = ListFormatter()
let names = ["Moe", "Larry", "Curly"]
if let string = formatter.string(from: names) {
print(string)
}
That will produce a nice, natural language string representation of the list. A US user will see:
Moe, Larry, and Curly
It will support any languages for which (a) your app has been localized; and (b) the user’s device is configured. For example, a German user with an app supporting German localization, would see:
Moe, Larry und Curly
Mine works on NSMutableArray with componentsJoinedByString
var array = ["1", "2", "3"]
let stringRepresentation = array.componentsJoinedByString("-") // "1-2-3"
In Swift 2.2 you may have to cast your array to NSArray to use componentsJoinedByString(",")
let stringWithCommas = (yourArray as NSArray).componentsJoinedByString(",")
let arrayTemp :[String] = ["Mani","Singh","iOS Developer"]
let stringAfterCombining = arrayTemp.componentsJoinedByString(" ")
print("Result will be >>> \(stringAfterCombining)")
Result will be >>> Mani Singh iOS Developer
If you want to ditch empty strings in the array.
["Jet", "Fire"].filter { !$0.isEmpty }.joined(separator: "-")
If you want to filter nil values as well:
["Jet", nil, "", "Fire"].flatMap { $0 }.filter { !$0.isEmpty }.joined(separator: "-")
you can use joined() to get a single String when you have array of struct also.
struct Person{
let name:String
let contact:String
}
You can easily produce string using map() & joined()
PersonList.map({"\($0.name) - \($0.contact)"}).joined(separator: " | ")
output:
Jhon - 123 | Mark - 456 | Ben - 789
if you want convert custom object array to string or comma separated string (csv) you can use
var stringIds = (self.mylist.map{$0.id ?? 0}).map{String($0)}.joined(separator: ",")
credit to : urvish modi
post: Convert an array of Ints to a comma separated string
A separator can be a bad idea for some languages like Hebrew or Japanese.
Try this:
// Array of Strings
let array: [String] = ["red", "green", "blue"]
let arrayAsString: String = array.description
let stringAsData = arrayAsString.data(using: String.Encoding.utf16)
let arrayBack: [String] = try! JSONDecoder().decode([String].self, from: stringAsData!)
For other data types respectively:
// Set of Doubles
let set: Set<Double> = [1, 2.0, 3]
let setAsString: String = set.description
let setStringAsData = setAsString.data(using: String.Encoding.utf16)
let setBack: Set<Double> = try! JSONDecoder().decode(Set<Double>.self, from: setStringAsData!)
You can either use loops for getting this done. Or by using map.
By mapping:
let array = ["one" , "two" , "three"]
array.map({$0}).joined(seperator : ",")
so in separator you can modify the string.
Output-> ("one,two,three")
The Swift equivalent to what you're describing is string interpolation. If you're thinking about things like JavaScript doing "x" + array, the equivalent in Swift is "x\(array)".
As a general note, there is an important difference between string interpolation vs the Printable protocol. Only certain classes conform to Printable. Every class can be string interpolated somehow. That's helpful when writing generic functions. You don't have to limit yourself to Printable classes.
You can print any object using the print function
or use \(name) to convert any object to a string.
Example:
let array = [1,2,3,4]
print(array) // prints "[1,2,3,4]"
let string = "\(array)" // string == "[1,2,3,4]"
print(string) // prints "[1,2,3,4]"
Create extension for an Array:
extension Array {
var string: String? {
do {
let data = try JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted])
return String(data: data, encoding: .utf8)
} catch {
return nil
}
}
}
if you have string array list , then convert to Int
let arrayList = list.map { Int($0)!}
arrayList.description
it will give you string value
for any Element type
extension Array {
func joined(glue:()->Element)->[Element]{
var result:[Element] = [];
result.reserveCapacity(count * 2);
let last = count - 1;
for (ix,item) in enumerated() {
result.append(item);
guard ix < last else{ continue }
result.append(glue());
}
return result;
}
}
Try This:
let categories = dictData?.value(forKeyPath: "listing_subcategories_id") as! NSMutableArray
let tempArray = NSMutableArray()
for dc in categories
{
let dictD = dc as? NSMutableDictionary
tempArray.add(dictD?.object(forKey: "subcategories_name") as! String)
}
let joinedString = tempArray.componentsJoined(by: ",")
use this when you want to convert list of struct type into string
struct MyStruct {
var name : String
var content : String
}
let myStructList = [MyStruct(name: "name1" , content: "content1") , MyStruct(name: "name2" , content: "content2")]
and covert your array like this way
let myString = myStructList.map({$0.name}).joined(separator: ",")
will produce ===> "name1,name2"
FOR SWIFT 3:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == phoneField
{
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
let components = newString.components(separatedBy: NSCharacterSet.decimalDigits.inverted)
let decimalString = NSString(string: components.joined(separator: ""))
let length = decimalString.length
let hasLeadingOne = length > 0 && decimalString.character(at: 0) == (1 as unichar)
if length == 0 || (length > 10 && !hasLeadingOne) || length > 11
{
let newLength = NSString(string: textField.text!).length + (string as NSString).length - range.length as Int
return (newLength > 10) ? false : true
}
var index = 0 as Int
let formattedString = NSMutableString()
if hasLeadingOne
{
formattedString.append("1 ")
index += 1
}
if (length - index) > 3
{
let areaCode = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("(%#)", areaCode)
index += 3
}
if length - index > 3
{
let prefix = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("%#-", prefix)
index += 3
}
let remainder = decimalString.substring(from: index)
formattedString.append(remainder)
textField.text = formattedString as String
return false
}
else
{
return true
}
}
If you question is something like this:
tobeFormattedString = ["a", "b", "c"]
Output = "abc"
String(tobeFormattedString)

Resources