Concatenate String in Swift - ios

i have an array which contains strings i.e Array
i tried to concatenate string, but i got an error as "String is not identical to UInt8"
var titleString:String! = ""
for title in array {
titleString += "\(title)"
}

To concatenate all elements of a string array, you can use the reduce method:
var string = ["this", "is", "a", "string"]
let res = string.reduce("") { $0 + $1 }
The first parameter is the initial string, which is empty, and the second is a closure, which is executed for each element in the array. The closure receives 2 parameters: the value returned at the previous step (or the initial value, if it's the 1st element), and the current element value.
More info here
Addendum I forgot to explicitly answer to your question: the concatenation doesn't work because you declared the titleString as optional - just turn into a non optional variable and it will work. If you still want to use the optional, then use forced unwrapping when doing the assignment:
titleString! += "\(title)"
Addendum 2 As suggested by #MartinR, there's another simpler way to concatenate:
join("", string)

In Swift 3, this is how you join elements of a String array:
["this", "is", "a", "string"].joined(separator: "")
Although, joined(separator:) is really geared for actually putting a separator between the strings. Reduce is still more concise:
["this", "is", "a", "string"].reduce("", +)

Related

Difference between Swift String Interpolation Prints

I am new to iOS development, while I was going through string interpolation. I want to know the clarification between these print statement's output:
var value = "5"
print("Values is: \(value)")
print("Values is:", value)
print("Values is: " + value)
Output is : Values is: 5
Values is: 5
Values is: 5
Practically all three forms do the same thing.
The differences are
String interpolation syntax. You can put everything within the inner parentheses which responds to the CustomStringConvertible protocol.
Variadic parameter syntax. print is declared func print(_ items: Any...,. Any... means you can pass multiple items comma separated which are treated as array.
String concatenation syntax : The strings are concatenated with the + operator
If 5 was Int rather than String forms 1 and 2 are valid but not form 3
For such a question, we should take a look at print(_:separator:terminator:) parameters:
1) items: is a variadic parameter of type Any, which means that you can pass zero or more items to it. Example:
print() // passing nothing
print("Hello") // passing single item (String)
print(101, 40.45, false, ["Hi", "Greetings"]) // passing multiple items
2) separator: the string to print between each item (as mentioned in its documentation). Example:
print(101, 40.45, false, ["Hi", "Greetings"], separator: " <=> ")
// 101<=>40.45<=>false<=>["Hi", "Greetings"]
3) terminator: the string to print after all items have been printed (as mentioned in its documentation). Example:
print(101, 40.45, false, ["Hi", "Greetings"], terminator: " ==>")
// 101 40.45 false ["Hi", "Greetings"] ==>
Back to your cases:
First, keep in mind that for all of your three cases you are passing only items parameter; It is valid -for sure- because separator and terminator have default values as " " and \n.
Now, for the first and third print statements
print("Values is: \(value)")
print("Values is: " + value)
what happens is: actually you are dealing with Strings, it is not about the print itself. You can do interpolation in strings as well as using the + for concatenating strings without the print:
// interpolation:
let name = "Jack"
let greetingMessage = "Greetings, \(name)"
print(greetingMessage) // => Greetings, Jack
// concatenating:
let concatenated = "Greetings" + ", " + "Sara"
print(concatenated) // => "Greetings" + ", " + "Sara"
Which means that you are passing a single String item, regardless of doing interpolation or concatenation for it.
You could also check The + function implementation in Swift. Basically, it is an append!
The second print statement:
print("Values is:", value)
What happens here is: you are passing two items; According to the default value for separator, the output is:
Values is: 5
As:
Values is: 5
^ ^^
| ||__ item #2
item #1 |
|
default separator (" ")
In this print statement output is the same but there is different like in first statement use \(value) variable within the string data.
The second statement append data in your string value with keep one space
The third statement just concat two value (it does not keep space between two value), In this statement "+" sign used as operator overloading to concat two value
let value = "5"
print("Values is: \(value)") //use variable value within string
print("Values is:", value) //append value, with keep one space
print("Values is: " + value) //just concat two value
var value = "5"
print("Values is: (value)")
// Print the value as a part of the the string. If you use print("Values is:(value)"), it will print the output without space.
print("Values is:", value)
// you do not need to add a separate space to add the value to sting. It will automatically add the value to the string with a space.
print("Values is: " + value)
// It will show error if you use integer value "Binary operator '+' cannot be applied to operands of type 'String' and 'Int'"
otherwise it will work. And if you want to concatenate int with sting you need to do something like below:-
print("Values is: " + String(value))
// it is normal concatenate number with string
All the above will print the exact

What's the best way to transform an Array of type Character to a String in Swift?

This question is specifically about converting an Array of type Character to a String. Converting an Array of Strings or numbers to a string is not the topic of discussion here.
In the following 2 lines, I would expect myStringFromArray to be set to "C,a,t!,🐱"
var myChars: [Character] = ["C", "a", "t", "!", "🐱"]
let myStringFromArray = myChars.joinWithSeparator(",");
However, I can't execute that code because the compiler complains about an "ambiguous reference to member joinWithSeparator".
So, two questions:
1) Apple says,
"Every instance of Swift’s Character type represents a single extended
grapheme cluster. An extended grapheme cluster is a sequence of one or
more Unicode scalars that (when combined) produce a single
human-readable character."
Which to me sounds at least homogeneous enough to think it would be reasonable to implement the joinWithSeparator method to support the Character type. So, does anyone have a good answer as to why they don't do that???
2) What's the best way to transform an Array of type Character to a String in Swift?
Note: if you don't want a separator between the characters, the solution would be:
let myStringFromArray = String(myChars)
and that would give you "Cat!🐱"
Which to me sounds at least homogeneous enough to think it would be reasonable to implement the joinWithSeparator method to support the Character type. So, does anyone have a good answer as to why they don't do that???
This may be an oversight in the design. This error occurs because there are two possible candidates for joinWithSeparator(_:). I suspect this ambiguity exists because of the way Swift can implicit interpret double quotes as either String or Character. In this context, it's ambiguous as to which to choose.
The first candidate is joinWithSeparator(_: String) -> String. It does what you're looking for.
If the separator is treated as a String, this candidate is picked, and the result would be: "C,a,t,!,🐱"
The second is joinWithSeparator<Separator : SequenceType where Separator.Generator.Element == Generator.Element.Generator.Element>(_: Separator) -> JoinSequence<Self>. It's called on a Sequence of Sequences, and given a Sequence as a seperator. The method signature is a bit of a mouthful, so lets break it down. The argument to this function is of Separator type. This Separator is constrained to be a SequenceType where the elements of the sequence (Seperator.Generator.Element) must have the same type as the elements of this sequence of sequences (Generator.Element.Generator.Element).
The point of that complex constraint is to ensure that the Sequence remains homogeneous. You can't join sequences of Int with sequences of Double, for example.
If the separator is treated as a Character, this candidate is picked, the result would be: ["C", ",", "a", ",", "t", ",", "!", ",", "🐱"]
The compiler throws an error to ensure you're aware that there's an ambiguity. Otherwise, the program might behave differently than you'd expect.
You can disambiguate this situation by this by explicitly making each Character into a String. Because String is NOT a SequenceType, the #2 candidate is no longer possible.
var myChars: [Character] = ["C", "a", "t", "!", "🐱"]
var anotherVar = myChars.map(String.init).joinWithSeparator(",")
print(anotherVar) //C,a,t,!,🐱
This answer assumes Swift 2.2.
var myChars: [Character] = ["C", "a", "t", "!", "🐱"]
var myStrings = myChars.map({String($0)})
var result = myStrings.joinWithSeparator(",")
joinWithSeparator is only available on String arrays:
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
}
You could create a new extension to support Characters:
extension SequenceType where Generator.Element == Character {
#warn_unused_result
public func joinWithSeparator(separator: String) -> String {
var str = ""
self.enumerate().forEach({
str.append($1)
if let arr = self as? [Character], endIndex: Int = arr.endIndex {
if $0 < endIndex - 1 {
str.append(Character(separator))
}
}
})
return str
}
}
var myChars: [Character] = ["C", "a", "t", "!", "🐱"]
let charStr = myChars.joinWithSeparator(",") // "C,a,t,!,🐱"
Related discussion on Code Review.SE.
Context: Swift3(beta)
TL;DR Goofy Solution
var myChars:[Character] = ["C", "a", "t", "!", "🐱"]
let separators = repeatElement(Character("-"), count: myChars.count)
let zipped = zip(myChars, separators).lazy.flatMap { [$0, $1] }
let joined = String(zipped.dropLast())
Exposition
OK. This drove me nuts. In part because I got caught up in the join semantics. A join method is very useful, but when you back away from it's very specific (yet common) case of string concatenation, it's doing two things at once. It's splicing other elements in with the original sequence, and then it's flattening the 2 deep array of characters (array of strings) into one single array (string).
The OPs use of single characters in an Array sent my brain elsewhere. The answers given above are the simplest way to get what was desired. Convert the single characters to single character strings and then use the join method.
If you want to consider the two pieces separately though... We start with the original input:
var input:[Character] = ["C", "a", "t", "!", "🐱"]
Before we can splice our characters with separators, we need a collection of separators. In this case, we want a pseudo collection that is the same thing repeated again and again, without having to actually make any array with that many elements:
let separators = repeatElement(Character(","), count: myChars.count)
This returns a Repeated object (which oddly enough you cannot instantiate with a regular init method).
Now we want to splice/weave the original input with the separators:
let zipped = zip(myChars, separators).lazy.flatMap { [$0, $1] }
The zip function returns a Zip2Sequence(also curiously must be instantiated via free function rather than direct object reference). By itself, when enumerated the Zip2Sequence just enumerates paired tuples of (eachSequence1, eachSequence2). The flatMap expression turns that into a single series of alternating elements from the two sequences.
For large inputs, this would create a largish intermediary sequence, just to be soon thrown away. So we insert the lazy accessor in there which lets the transform only be computed on demand as we're accessing elements from it (think iterator).
Finally, we know we can make a String from just about any sort of Character sequence. So we just pass this directly to the String creation. We add a dropLast() to avoid the last comma being added.
let joined = String(zipped.dropLast())
The valuable thing about decomposing it this way (it's definitely more lines of code, so there had better be a redeeming value), is that we gain insight into a number of tools we could use to solve problems similar, but not identical, to join. For example, say we want the trailing comma? Joined isn't the answer. Suppose we want a non constant separator? Just rework the 2nd line. Etc...

Append an array of strings to a string in Swift

I have an array of strings for one variable, and a string as another variable. I'd like to append all of the strings in the collection to the single string.
So for example I have:
var s = String()
//have the CSV writer create all the columns needed as an array of strings
let arrayOfStrings: [String] = csvReport.map{GenerateRow($0)}
// now that we have all the strings, append each one
arrayOfStrings.map(s.stringByAppendingString({$0}))
the line above fails. I've tried every combination I can think of, but at the end of the day, I can't get it unless I just create a for loop to iterate through the entire collection, arrayOfStrings, and add it one by one. I feel like I can achieve this the same way using map or some other function.
Any help?
Thanks!
You can use joined(separator:):
let stringArray = ["Hello", "World"]
let sentence = stringArray.joined(separator: " ") // "Hello World"
You could convert your array to string using joinWithSeparator(String)
here is an example
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
source: [ How do I convert a Swift Array to a String? ]
There are at least two options here. The most semantic choice is likely joinWithSeparator on the [String] object. This concatenates every string in the array, placing the separator provided as a parameter between each string.
let result = ["a", "b", "c", "d"].joinWithSeparator("")
An alternative is to use a functional reduce and the + function operator which concatenates strings. This may be preferred if you want to do additional logic as part of the combine. Both example code produce the same result.
let result = ["a", "b", "c", "d"].reduce("", combine: +)
It's also worth noting the second options is transferrable to any type that can be added, whereas the first only works with a sequence of strings, as it is defined on a protocol extension of SequenceType where Generator.Element == String.

How to use split and map method?

I saw a question: Swift: Split a String into an array
And there's some code I don't understand:
let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map{String($0)}
fullNameArr[0] // First
fullNameArr[1] // Last
How does split() and map{} work?
You're using a syntax that won't work in Xcode7. The correct syntax should be
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
Getting that out of the way let's break down that line into two pieces:
split takes
A collection of Characters representing the String's extended
grapheme clusters
-- From Xcode docs
and a closure taking a character and returning Bool - true if the character can be considered as a separator.
if this syntax is confusing try reading that:
fullNameArr = fullName.characters.split({
character in
return character == " "
})
Now, split returns an array of SubSequence objects. You want to convert them back to string to be able to print them nicely. So one way of doing it would be creating a for loop iterating over all the results of split and converting them to string, then appending to a result array, or using map method that does the same.
If you look closely at the first line, you execute map on the array and pass a closure that does something with every element of the array and writes it back.
A simple example how that works
let exampleArray = [1, 2, 3]
print(exampleArray.map {$0 * 3})
// prints [3, 6, 9]
Hope that helps!

Remove an element in an array without hard-coding the index? in Swift

This is my first post, and I am very happy to join in this community. I am learning Swift with Ray Wenderlich's video tutorial. The challenge I got for lesson 3 is remove a element in an array without hard-coding the index. I understand the correct answer that Ray provided, but just don't understand why my answer can not work. Please see following for Ray's answer as well as my answer. If anyone can explain it for me, that would be great!! thanks:]
Correct Answer:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
let optIndex = find(programmingLanguages, "Objective-C")
if let defIndex = optIndex {
programmingLanguages.removeAtIndex(defIndex)
}
programmingLanguages
My answer1:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
programmingLanguages.removeAtIndex(find(programmingLanguages,"Objective-C")
programmingLanguages
My answer2:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
let optIndex = find(programmingLanguages, "Objective-C")
programmingLanguages.removeAtIndex(optIndex)
programmingLanguages
When you use find() it is not guaranteed to return a result (what happens here?: find(programmingLanguages, "rick astley"))
To account for this possibility find() returns an "optional" - which basically just means that when it does not find a match it will return nil.
In order to protect you from accidentally using nil in some place that does not know how to handle it, the language considers optionals to be a distinct type. In short, you must check for nil when you get an optional value.
The main way to do that is with "if let" syntax – as seen in Ray's example – but a more terse (and more common) way to do it is like so:
if let defIndex = find(programmingLanguages, "Objective-C") {
programmingLanguages.removeAtIndex(defIndex)
}
if you know for sure that the optional is not nil (like if you are checking for something you just inserted) you can use "forced unwrapping" which grabs the value out of the optional (and will cause an error and crash if it's nil).
You do forced unwrapping using the forced unwrapping operator, the exclamation point !:
// defIndex is an optional
let defIndex = find(programmingLanguages, "Objective-C")
// defIndex! <-- unwraps the optional but raises an error if it's nil
programmingLanguages.removeAtIndex(defIndex!)
Your answer doesn't work because find returns an optional value (the type is Int? and not Int), primarily so that if the the value is not found, it returns nil, if the value is found it returns the index wrapped in an optional value. Your answer then attempts to pass that optional value to removeAtIndex, which takes a Int parameter, not an Int?.
Ray's answer conditionally unwraps the result and checks it's validity before attempting to pass it on to removeAtIndex.
While I think Jiaaro nails it brevity-wise, here's a function I wrote today that does something similar. I didn't know about the find function and while longer, it's easy to follow and might help you get the concept.
func arrayByRemovingObjectIdenticalTo<T: Equatable>(objectToRemove: T, fromArray:[T]) -> [T] {
var returnArray = fromArray
for (index, object) in enumerate(fromArray) {
if object == objectToRemove {
returnArray.removeAtIndex(index)
}
}
return returnArray
}

Resources