Swift 3 String has no member components [duplicate] - swift

So I'm trying to prepare myself for coding interviews by doing HackerRank's test case samples. If you're familiar with the process, you usually take a standard input that has various lines of strings and you extract the information based on what the question is asking. I have come across numerous questions where they will give you a line (as a String) with n number of integers separated by a space (i.e. 1 2 3 4 5). In order to solve the problem I need to extrapolate an array of Int ([Int]) from a String. I came up with this nifty method:
func extractIntegers(_ s: String) -> [Int] {
let splits = s.characters.split { [" "].contains(String($0)) }
return splits.map { Int(String($0).trimmingCharacters(in: .whitespaces))! }
}
So I code it in my Playground and it works fantastic, I even run multiple test cases I make up, and they all pass with flying colors...then I copy the code to HackerRank and try running it for submission. And I get this:
solution.swift:16:29: error: value of type 'String' has no member 'trimmingCharacters'
return splits.map { Int(String($0).trimmingCharacters(in: .whitespaces))! }
So... okay maybe HR hasn't updated everything for Swift 3 yet. No big deal! I have an idea for an even cleaner solution! Here it is:
func extractIntegers(_ s: String) -> [Int] {
return s.components(separatedBy: " ").map { Int($0)! }
}
....AAAAANDDD of course:
solution.swift:15:12: error: value of type 'String' has no member 'components'
return s.components(separatedBy: " ").map { Int($0)! }
So now I'm forced to use a really sloppy method where I loop through all the characters, check for spaces, append substrings from ranges between spaces into an array, and then map that array and return it.
Does anyone have any other clean ideas to work around HR's inadequacies with Swift? I would like any recommendations I can get!
Thanks in advance!

The String methods
func trimmingCharacters(in set: CharacterSet) -> String
func components(separatedBy separator: String) -> [String]
are actually methods of the NSString class, defined in the Foundation
framework, and "bridged" to Swift. Therefore, to make your code compile,
you have go add
import Foundation
But a slightly simplified version of your first method compiles
with pure Swift, without importing Foundation. I handles leading, trailing, and intermediate whitespace:
func extractIntegers(_ s: String) -> [Int] {
let splits = s.characters.split(separator: " ").map(String.init)
return splits.map { Int($0)! }
}
let a = extractIntegers(" 12 234 -567 4 ")
print(a) // [12, 234, -567, 4]
Update for Swift 4 (and simplified):
func extractIntegers(_ s: String) -> [Int] {
return s.split(separator: " ").compactMap { Int($0) }
}

Related

Adding numbers inside a string in Swift

Reading through this problem in a book
Given a string that contains both letters and numbers, write a
function that pulls out all the numbers then returns their sum. Sample
input and output
The string “a1b2c3” should return 6 (1 + 2 + 3). The string
“a10b20c30” should return 60 (10 + 20 + 30). The string “h8ers” should
return “8”.
My solution so far is
import Foundation
func sumOfNumbers(in string: String) -> Int {
var numbers = string.filter { $0.isNumber }
var numbersArray = [Int]()
for number in numbers {
numbersArray.append(Int(number)!)
}
return numbersArray.reduce(0, { $0 * $1 })
}
However, I get the error
Solution.swift:8:33: error: cannot convert value of type 'String.Element' (aka 'Character') to expected argument type 'String'
numbersArray.append(Int(number)!)
^
And I'm struggling to get this number of type String.Element into a Character. Any guidance would be appreciated.
The error occurs because Int.init is expecting a String, but the argument number you gave is of type Character.
It is easy to fix the compiler error just by converting the Character to String by doing:
numbersArray.append(Int("\(number)")!)
or just:
numbersArray.append(number.wholeNumberValue!)
However, this does not produce the expected output. First, you are multiplying the numbers together, not adding. Second, you are considering each character separately, and not considering groups of digits as one number.
You can instead implement the function like this:
func sumOfNumbers(in string: String) -> Int {
string.components(separatedBy: CharacterSet(charactersIn: "0"..."9").inverted)
.compactMap(Int.init)
.reduce(0, +)
}
The key thing is to split the string using "non-digits", so that "10" and "20" etc gets treated as individual numbers.

Append Values in NSSet To An Array of Type [String]

I am attempting to get the values from an NSSet in core data and append those values to an array of type String.
func addStepsToArray() {
if let steps = entity.steps {
for i in steps {
recipeStep.append(String(i))
}
}
}
entity.steps is the list of steps tied to a core data entity. This is an NSSet. I am trying to copy those values to an array of type String.
#State var recipeStep: [String]
When trying to do this in my for in loop, I receive the following error: No exact matches in call to initializer
If I remove the conversion of "I" to String, I receive the following error:
Cannot convert value of type NSSet.Element (aka Any) to expected argument type String
Any idea on how to get this to work?
NSSet is defined in Objective C, which didn't have generics. It's an untyped collection, so you don't statically know anything about its elements.
As you've noticed, your i variable isn't a String, it's an Any.
You're confusing type coercion ("casting") with type conversion. If i were a Double, you could call String(i) to invoke an initializer which takes a double, and processes into a String.
You tried something similar by calling String(i), where you're making the Swift compiler find an initializer on String with the signitiure init(_: Any).
There is no such initializer. And besides, that's not what you want. You don't want to create a new String from a different kind of value. You already have a string, it's just "hidden" behind an Any reference.
What you're looking for is to do a down-cast, from Any to String:
func addStepsToArray() {
if let steps = entity.steps {
for i in steps {
guard let step = i as? String else {
fatalError("Decide what to do if the value isn't a String.")
}
recipeStep.append(i as String)
}
}
}
I'll warn you though, there are several issues/blemishes with this code:
You're using a for loop to do what is ultimately just a mapping operation
Your computation doesn't return its ouput, and instead indirectly achieves its goal through a side-effect on recipeStep
Your computation doesn't take a its input as a parameter, and instead indirectly achieves its goal through a side-effect on entity
i is conventionally expected to be an integer index of a for loop iterating over a sequence of numbers. Here it's an Any (a String at runtime)
Here's what I would suggest instead:
func getRecipeSteps(from entity: MyEntityType) -> [String] {
guard let steps = entity.steps else { return [] }
return steps.map { step in
guard let stringStep = step as? String else {
fatalError("Decide what to do if the value isn't a String.")
}
return step
}
}
Then in the rest of your code (and your tests), you can write self.recipeSteps = getRecipeSteps(from: myEntity). Elegant!
If you're certain that these entity.steps values can only ever be strings, then you can boil this down to a single map with a force-cast:
func getRecipeSteps(from entity: MyEntityType) -> [String] {
entity.steps?.map { $0 as! String } ?? []
}
Just convert directly:
let set = Set(["1", "2", "3"])
let array = Array(set)
DDLog(set)//Set<String>)
DDLog(array)//[String]

Cannot invoke 'reduce' with an argument list of type '(String, (String) -> String)'

I am trying to convert Swift 3 to Swift 4 for a repo on github. Here is a function that blocks me.
func times(_ n: Int) -> String {
return (0..<n).reduce("") { $0 + self }
}
The error Xcode gives is:
"Cannot invoke 'reduce' with an argument list of type '(String, (String) -> String)'"
I looked at Apple's official page and found reduce(_:_:) and reduce(into:_:), and someone's question. Have tried the code below but I still can't get it to work. Please point out what I am missing.
return (0..<n).character.reduce("") { string, character in
(0..<n) + self }
return (0..<n).character.reduce("") { $0 + self }
Here $0 refers to the closure's first argument (I think). Then we can self property to refer to the current instance within its own instance methods.
Your closure will be receiving two parameters and you're only using one ($0). You could use $0.0 in the closure or simply use the string constructor that does the same thing without reduce:
func times(_ n: Int) -> String
{ return String(repeating:self, count:n) }
OR, if you want to use Python like multiplication to repeat a string, you could add an operator:
extension String
{
static func *(lhs:String,rhs:Int) -> String
{ return String(repeating:lhs, count:rhs) }
}
// then, the following will work nicely:
"blah " * 10 // ==> "blah blah blah blah blah blah blah blah blah blah "
The answer depends on what are you going to do. It was hard to get it out what reduce exactly do, but you must understand that the main point of this function needed to reduce the array into one variable.
Take a look on example:
let items = ["one", "two", "three"]
let final = items.reduce("initial") { text, item in "\(text), \(item)" }
print(final) // initial, one, two, three
In closure the text is a cumulating string. The initial value is setted as a parameter. "initial" in our example. At the first iteration the text would be initial, one. At second: initial, one, two. And so on. That's because we setted a rule how to reduce the array: "\(text), \(item)"
In your example:
func times(_ n: Int) -> String {
return (0..<n).reduce("") { $0 + self }
}
First we create an array with n items by this (0..<n): [0, 1, 2, 3, 4..]
Then we set an initial value as an empty string.
Next can't know what you need.
Maybe you need a result string as 0123456789.., then there a code:
let reduced = (0..<n).reduce("") { text, value in text + "\(value)" }
Hope that would help you =)

NSLocalizedString with format specifiers in Swift yields garbage

To facilitate easier localizing in a very small app of mine, I have this String extension method:
extension String {
func localized(with values: Any...) -> String {
// debug values
for v in values {
print("\(type(of: v)): \(v)")
}
return String.localizedStringWithFormat(NSLocalizedString(self, comment: ""), values)
}
}
My German localization of Localizable.strings contains this key/value pair:
"WeeksFuture" = "In %d Wochen";
Doing this:
for _ in 0..<5 {
let localized = "WeeksFuture".localized(with: 3)
print(localized)
}
while having Xcode set to debug the app in German (although this happens in every other language too) prints this to the output window:
Int: 3
In 151.456 Wochen
Int: 3
In 186.912 Wochen
Int: 3
In 186.880 Wochen
Int: 3
In 187.264 Wochen
Int: 3
In 187.488 Wochen
Obviously, this is all wrong. Why do I first get the correct output of "Int: 3", and then a string with a seemingly random garbage number?
String.localizedStringWithFormat takes a String and CVarArg... as arguments. You passed in an array of Any - values as the second argument. It is forced to convert an array to a decimal number, resulting in the weird result.
To solve this problem, you just need to find an overload that takes an [CVarArg] instead. Luckily, there is an init overload like that:
return String.init(format:
NSLocalizedString(self, comment: ""), arguments: values)
However, values is an [Any], which is not compatible with the expected [CVarArg]. You should probably change the parameter type.
So your whole extension looks like this:
func localized(with values: CVarArg...) -> String {
return String.init(format: NSLocalizedString(self, comment: ""), arguments: values)
}

How to convert a Swift array to a tuple in a single line? [duplicate]

I just want to convert an array into a tuple in Swift; something like the following:
>>> myArray = [1,2,3,4,5]
>>> mytuple = tuple(myArray)
>>> mytuple
(1, 2, 3, 4, 5)
What's the easiest way of doing that?
(I only started learning Swift today, so I am very, very new).
It's actually quite possible, if you are willing to do some low level magic. I don't think it works if the tuple has a collection type, though. This is mainly for interacting with C libraries.
typealias TupleType = (UInt8, UInt8, UInt8)
var array = [2, 3, 4] as [UInt8]
var tuple = UnsafeMutablePointer<StepsType>(malloc(UInt(sizeof(TupleType))))
memcpy(tuple, array, UInt(array.count))
More of this stuff can be found here:
https://codereview.stackexchange.com/questions/84476/array-to-tuple-in-swift/84528#84528
Because 70% of us are highly visual beings:
You can't do this because the size of a tuple is part of its type, and this isn't true for an array. If you know the array's length, you can just do let myTuple = (myArray[0], myArray[1], ...). Or you can build your architecture around tuples from the beginning. What are you trying to do?
This is a common problem which occurs in lots of other languages. See How do I convert a list to a tuple in Haskell?
if what you want to do is extract multiple value from array at the same time, maybe
the following code could help you
extension Array {
func splat() -> (Element,Element) {
return (self[0],self[1])
}
func splat() -> (Element,Element,Element) {
return (self[0],self[1],self[2])
}
func splat() -> (Element,Element,Element,Element) {
return (self[0],self[1],self[2],self[3])
}
func splat() -> (Element,Element,Element,Element,Element) {
return (self[0],self[1],self[2],self[3],self[4])
}
}
then you can use it like this
let (first,_,third) = ( 0..<20 ).map { $0 }.splat()
you can even write a codegen to generate the extension code
This my universal solution for copy array data to tuple. The only problem that I could not solve is to check the type of the element in the tuple and the element from the array.
/// Unsafe copy array to tuple
func unsafeCopyToTuple<ElementType, Tuple>(array: inout Array<ElementType>, to tuple: inout Tuple) {
withUnsafeMutablePointer(to: &tuple) { pointer in
let bound = pointer.withMemoryRebound(to: ElementType.self, capacity: array.count) { $0 }
array.enumerated().forEach { (bound + $0.offset).pointee = $0.element }
}
}
Scared of unsafe pointers? Or need support for tuples with different types inside?
It can also be done like this:
public extension Array {
public var tuple: Any? {
switch count {
case 0:
return ()
case 1:
return (self[0])
case 2:
return (self[0], self[1])
case 3:
return (self[0], self[1], self[2])
case 4:
return (self[0], self[1], self[2], self[3])
case 5:
return (self[0], self[1], self[2], self[3], self[4])
default:
return nil
}
}
Because typing this out can be a little error prone, it's a good idea to write tests for this that make sure the right tuple is returned.
I've used this approach in ArrayPlusTuple.
So instead of writing this functionality yourself you could just add pod 'ArrayPlusTuple' to your pod file and get this tested functionality for free.