I'm upgrading code from Swift 2 to Swift 3 and ran across this error:
wordcount.swift:7:5: error: value of type 'String' has no member 'enumerateSubstringsInRange'
line.enumerateSubstringsInRange(range, options: .ByWords) {w,,,_ in
In Swift 2, this method comes from a String extension of which the compiler is aware.
I have not been able to locate this method in the Swift 3 library. It appears in the documentation for Foundation here:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/occ/instm/NSString/enumerateSubstringsInRange:options:usingBlock:
My entire script is:
import Foundation
var counts = [String: Int]()
while let line = readLine()?.lowercased() {
let range = line.characters.indices
line.enumerateSubstringsInRange(range, options: .ByWords) {w,_,_,_ in
guard let word = w else {return}
counts[word] = (counts[word] ?? 0) + 1
}
}
for (word, count) in (counts.sorted {$0.0 < $1.0}) {
print("\(word) \(count)")
}
It works with Swift 2.2 (modulo the changes I have already made for Swift 3, such as lowercase -> lowercased and sort -> sorted) but fails to compile with Swift 3.
And very strangely, neither the Swift 3 command line compiler nor the Swift Migration assistant in XCode 8 Beta suggests a replacement, as it does for many other renamed methods. Perhaps enumerateSubstringsInRange is deprecated or its parameter names changed?
If you type str.enumerateSubstrings in a Playground, you'll see the following as a completion option:
enumerateSubstrings(in: Range<Index>, options: EnumerationOptions, body: (substring: String?, substringRange: Range<Index>, enclosingRange: Range<Index>, inout Bool) -> ())
In addition to addressing the new enumerateSubstrings(in:options:body:) syntax, you need to also change how you get the range for the string:
import Foundation
var counts = [String: Int]()
while let line = readLine()?.lowercased() {
let range = line.startIndex ..< line.endIndex
line.enumerateSubstrings(in: range, options: .byWords) {w,_,_,_ in
guard let word = w else {return}
counts[word] = (counts[word] ?? 0) + 1
}
}
for (word, count) in (counts.sorted {$0.0 < $1.0}) {
print("\(word) \(count)")
}
Related
I want to have a random emoji inside a Label and every tableViewCell should show a different, random picked emoji...
I tried following function, but sadly it's giving me following error: Value of type '[String]' has no member 'shuffled'
let emojis: [String] = {
let url = Bundle.main.url(forResource: "emojis", withExtension: "txt")!
let list = try! String(contentsOf: url).map { String($0) }
return list.shuffled()
}()
po String(UnicodeScalar(Array(0x1F300...0x1F3F0).randomElement()!)!)
As Carpsen says, the shuffled() function is only in Swift ≤ 4.2. In earlier versions of the language you have to write your own.
Here is an example implementation. I called my scrambled(), so it will work in any version of Swift including 4.2:
extension Array {
func scrambled () -> [Element] {
var source = self
var dest = Array<Element>()
for _ in 1...self.count {
let index = Int(arc4random_uniform(UInt32(source.count)))
dest.append(source.remove(at: index))
}
return dest
}
}
You could scramble the entire set of emojis, but it seems to me you'd be better off creating an array of random emoji when you first populate the data source of your table view. To do that you could use a function randomEmoji():
func randomEmoji() -> String {
let range = 0x1F300...0x1F3F0
let index = Int(arc4random_uniform(UInt32(range.count)))
let ord = range.lowerBound + index
guard let scalar = UnicodeScalar(ord) else { return "❓" }
return String(scalar)
}
So you'd create a stuct to hold all the data for a table view cell, and then populate each struct with a call to randomEmoji().
Note that the randomEmoji() function only chooses emoji in the unicode range from 0x1F300 to 0x1F3F0. Also, the approach of using randomEmoji() may cause duplicate emoji.
You can easily use this extension for emoji string :)
extension NSObject {
public var emojiString: String {
let pointer = Unmanaged.passUnretained(self).toOpaque()
// You can adjust your range
//let range = 0x1F600...0x1F64F
let range = 0x1F300...0x1F3F0
let index = (pointer.hashValue % range.count)
let ord = range.lowerBound + index
guard let scalar = UnicodeScalar(ord) else { return "❓" }
return String(scalar)
}
}
Just call cell.textLabel?.text = cell.emojiString and you're good to go.
Unless you are using Swift 4.2, the shuffled()is not available and the error message is spot on.
You could create your own shuffled() function.
With the new ways of handling string in Swift 4, I'm trying to wrap my head around how to write the equivalent of the Mid function from other languages (visual basic, etc), so that
let testString = "0123456"
print Mid(testString, 2,4) // "1234" (or "2345" would work too)
This question is the same idea, but everything there predates Swift 4. If the answer there by jlert is still the best way to do things in Swift 4, that works, although it seems like so much has changed that the best practice to do this may have changed as well.
One way to do it is with a combination of dropFirst and prefix and then use String to convert the result back to String:
let testString = "0123456"
func mid(_ str: String, _ low: Int, _ count: Int) -> String {
return String(str.dropFirst(low).prefix(count))
}
print(mid(testString, 2,4)) // 2345
dropFirst and prefix are forgiving and won't crash if enough letters are not there. They will just give a truncated result. Depending on how you define the function, this is a perfectly acceptable implementation.
Another approach would be to use array subscripting. If you do it that way, you need to check inputs to avoid Array index out of range fatal errors:
func mid(_ str: String, _ low: Int, _ count: Int) -> String? {
guard low >= 0,
count >= 0,
low < str.count,
low + count <= str.count
else { return nil }
return String(Array(str)[low ..< low + count])
}
let testString = "abcdefghi"
if let result = mid(testString, 2, 4) {
print(result) // cdef
}
You can do:
extension String {
func mid(_ startOffset: Int, _ length: Int) -> Substring {
let start = index(startIndex, offsetBy: startOffset, limitedBy: endIndex) ?? endIndex
let end = index(start, offsetBy: length, limitedBy: endIndex) ?? endIndex
return self[start ..< end]
}
}
let string = "0123456"
let result = string.mid(2, 4) // 2345
Note, this returns a Substring, which enjoys memory efficiency. But if you want a String, you can do :
let result = String(string.mid(2, 4))
(Or you can incorporate this in your mid function.)
I would take a different approach using String.Index and return a Substring instead of a new String object. You can also add precondition to restrict improper use of that method:
func mid(_ string: String, _ positon: Int, length: Int) -> Substring {
precondition(positon < string.count, "invalid position")
precondition(positon + length <= string.count, "invalid substring length")
let lower = string.index(string.startIndex, offsetBy: positon)
let upper = string.index(lower, offsetBy: length)
return string[lower..<upper]
}
let testString = "0123456"
mid(testString, 2, length: 4) // "2345"
Another option would be creating that method as a string extension:
extension String {
func mid(_ positon: Int, length: Int) -> Substring {
precondition(positon < count, "invalid position")
precondition(positon + length <= count, "invalid substring length")
let lower = index(startIndex, offsetBy: positon)
let upper = index(lower, offsetBy: length)
return self[lower..<upper]
}
}
let testString = "0123456"
testString.mid(2, length: 4) // "2345"
iOS 14, Swift 5... same thing as vacawama excellent answer, but as an extension, so it's even less code :)
extension String {
func mid(_ low: Int, _ count: Int) -> String {
return String(self.dropFirst(low).prefix(count))
}
}
Use it like this.
let pString = "01234567"
for i in 0 ..< 8 {
print("\(i) \(pString.mid(i,1)")
}
Prints out your string to the console, one character at a time
I'd like to convert an Int in Swift to a String with leading zeros. For example consider this code:
for myInt in 1 ... 3 {
print("\(myInt)")
}
Currently the result of it is:
1
2
3
But I want it to be:
01
02
03
Is there a clean way of doing this within the Swift standard libraries?
Assuming you want a field length of 2 with leading zeros you'd do this:
import Foundation
for myInt in 1 ... 3 {
print(String(format: "%02d", myInt))
}
output:
01
02
03
This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.
The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:
let hours = 3
let minutes = 15
let seconds = 7
print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
output:
03:15:07
With Swift 5, you may choose one of the three examples shown below in order to solve your problem.
#1. Using String's init(format:_:) initializer
Foundation provides Swift String a init(format:_:) initializer. init(format:_:) has the following declaration:
init(format: String, _ arguments: CVarArg...)
Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted.
The following Playground code shows how to create a String formatted from Int with at least two integer digits by using init(format:_:):
import Foundation
let string0 = String(format: "%02d", 0) // returns "00"
let string1 = String(format: "%02d", 1) // returns "01"
let string2 = String(format: "%02d", 10) // returns "10"
let string3 = String(format: "%02d", 100) // returns "100"
#2. Using String's init(format:arguments:) initializer
Foundation provides Swift String a init(format:arguments:) initializer. init(format:arguments:) has the following declaration:
init(format: String, arguments: [CVarArg])
Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted according to the user’s default locale.
The following Playground code shows how to create a String formatted from Int with at least two integer digits by using init(format:arguments:):
import Foundation
let string0 = String(format: "%02d", arguments: [0]) // returns "00"
let string1 = String(format: "%02d", arguments: [1]) // returns "01"
let string2 = String(format: "%02d", arguments: [10]) // returns "10"
let string3 = String(format: "%02d", arguments: [100]) // returns "100"
#3. Using NumberFormatter
Foundation provides NumberFormatter. Apple states about it:
Instances of NSNumberFormatter format the textual representation of cells that contain NSNumber objects and convert textual representations of numeric values into NSNumber objects. The representation encompasses integers, floats, and doubles; floats and doubles can be formatted to a specified decimal position.
The following Playground code shows how to create a NumberFormatter that returns String? from a Int with at least two integer digits:
import Foundation
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 2
let optionalString0 = formatter.string(from: 0) // returns Optional("00")
let optionalString1 = formatter.string(from: 1) // returns Optional("01")
let optionalString2 = formatter.string(from: 10) // returns Optional("10")
let optionalString3 = formatter.string(from: 100) // returns Optional("100")
For left padding add a string extension like this:
Swift 5.0 +
extension String {
func padLeft(totalWidth: Int, with byString: String) -> String {
let toPad = totalWidth - self.count
if toPad < 1 {
return self
}
return "".padding(toLength: toPad, withPad: byString, startingAt: 0) + self
}
}
Using this method:
for myInt in 1...3 {
print("\(myInt)".padLeft(totalWidth: 2, with: "0"))
}
Swift 3.0+
Left padding String extension similar to padding(toLength:withPad:startingAt:) in Foundation
extension String {
func leftPadding(toLength: Int, withPad: String = " ") -> String {
guard toLength > self.characters.count else { return self }
let padding = String(repeating: withPad, count: toLength - self.characters.count)
return padding + self
}
}
Usage:
let s = String(123)
s.leftPadding(toLength: 8, withPad: "0") // "00000123"
Swift 5
#imanuo answers is already great, but if you are working with an application full of number, you can consider an extension like this:
extension String {
init(withInt int: Int, leadingZeros: Int = 2) {
self.init(format: "%0\(leadingZeros)d", int)
}
func leadingZeros(_ zeros: Int) -> String {
if let int = Int(self) {
return String(withInt: int, leadingZeros: zeros)
}
print("Warning: \(self) is not an Int")
return ""
}
}
In this way you can call wherever:
String(withInt: 3)
// prints 03
String(withInt: 23, leadingZeros: 4)
// prints 0023
"42".leadingZeros(2)
// prints 42
"54".leadingZeros(3)
// prints 054
Using Swift 5’s fancy new extendible interpolation:
extension DefaultStringInterpolation {
mutating func appendInterpolation(pad value: Int, toWidth width: Int, using paddingCharacter: Character = "0") {
appendInterpolation(String(format: "%\(paddingCharacter)\(width)d", value))
}
}
let pieCount = 3
print("I ate \(pad: pieCount, toWidth: 3, using: "0") pies") // => `I ate 003 pies`
print("I ate \(pad: 1205, toWidth: 3, using: "0") pies") // => `I ate 1205 pies`
in Xcode 8.3.2, iOS 10.3
Thats is good to now
Sample1:
let dayMoveRaw = 5
let dayMove = String(format: "%02d", arguments: [dayMoveRaw])
print(dayMove) // 05
Sample2:
let dayMoveRaw = 55
let dayMove = String(format: "%02d", arguments: [dayMoveRaw])
print(dayMove) // 55
The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.
let str = "a str"
let padAmount = max(10, str.count)
String(repeatElement("-", count: padAmount - str.count)) + str
Output "-----a str"
The below code generates a 3 digits string with 0 padding in front:
import Foundation
var randomInt = Int.random(in: 0..<1000)
var str = String(randomInt)
var paddingZero = String(repeating: "0", count: 3 - str.count)
print(str, str.count, paddingZero + str)
Output:
5 1 005
88 2 088
647 3 647
Swift 4* and above you can try this also:
func leftPadding(valueString: String, toLength: Int, withPad: String = " ") -> String {
guard toLength > valueString.count else { return valueString }
let padding = String(repeating: withPad, count: toLength - valueString.count)
return padding + valueString
}
call the function:
leftPadding(valueString: "12", toLength: 5, withPad: "0")
Output:
"00012"
Details
Xcode 9.0.1, swift 4.0
Solutions
Data
import Foundation
let array = [0,1,2,3,4,5,6,7,8]
Solution 1
extension Int {
func getString(prefix: Int) -> String {
return "\(prefix)\(self)"
}
func getString(prefix: String) -> String {
return "\(prefix)\(self)"
}
}
for item in array {
print(item.getString(prefix: 0))
}
for item in array {
print(item.getString(prefix: "0x"))
}
Solution 2
for item in array {
print(String(repeatElement("0", count: 2)) + "\(item)")
}
Solution 3
extension String {
func repeate(count: Int, string: String? = nil) -> String {
if count > 1 {
let repeatedString = string ?? self
return repeatedString + repeate(count: count-1, string: repeatedString)
}
return self
}
}
for item in array {
print("0".repeate(count: 3) + "\(item)")
}
Unlike the other answers that use a formatter, you can also just add an "0" text in front of each number inside of the loop, like this:
for myInt in 1...3 {
println("0" + "\(myInt)")
}
But formatter is often better when you have to add suppose a designated amount of 0s for each seperate number. If you only need to add one 0, though, then it's really just your pick.
I know that to program in STDIN and STDOUT, we need to make an command line project in Xcode.
But how do I take a standard input in playground.
Whenever I try to run such code in playground
var input = readLine()!
I always get this error
Execution was interrupted, reason: EXC_BAD_INSTRUCTION
(Code=EXC_l386_INVOP, subcode=0x0)
Is it possible to take STDIN in playground or not?
UPDATE
I know this error is because of nil input variable but want to know how to overcome this nil value.
Fixed Solution for SWIFT 3
To make it work, Create a new command line tool project.
Go to "File" -> "New" -> "Project" -> "macOS" -> "Command Line Tool".
import Foundation
print("Hello, World!")
func solveMefirst(firstNo: Int , secondNo: Int) -> Int {
return firstNo + secondNo
}
func input() -> String {
let keyboard = FileHandle.standardInput
let inputData = keyboard.availableData
return NSString(data: inputData, encoding:String.Encoding.utf8.rawValue) as! String
}
let num1 = readLine()
let num2 = readLine()
var IntNum1 = Int(num1!)
var IntNum2 = Int(num2!)
print("Addition of numbers is : \(solveMefirst(firstNo: IntNum1!, secondNo: IntNum2!))")
And run the project using CMD + R
Playground can not read an input from the commend line.
You can use a custom "readLine()" function and a global input variable, each element in the input array is presenting a line:
import Foundation
var currentLine = 0
let input = ["5", "5 6 3"]
func readLine() -> String? {
if currentLine < input.endIndex {
let line = input[currentLine]
currentLine += 1
return line
} else {
return nil
}
}
let firstLine = readLine() // 5
let secondLine = readLine() // 5 6 3
let thirdLine = readLine() // nil
Try using Optional Chaining:
if let input = readLine() {
print("Input: \(input)")
} else {
print("No input.")
}
Go to
New > Project > MacOs > Command Line Tool
then you can apply :
let value1: String?
value1 = readLine()
print(value1 ?? "")
"" for the default value
For getting input from command line, like Console.ReadLine... Chalkers has a solution as follows.
func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding) as! String
}
please ask for further if it doesn't work Vinod.
I am taking my first foray into writing generic functions in Swift. What I am trying to do is write a function that takes an array input of any type as long as that type is convertible to a floating point number. I am wondering if I can leverage some of the Swift standard library protocols to do this. Here is a trivial example (I am searching for what to use as ConvertibleToFloatingPointTypeProtocol):
func toDoubleArray<T: ConvertibleToFloatingPointTypeProtocol>(array: [T]) -> [Double] {
var doubleArray = [Double]()
for arrayItem in array {
doubleArray.append(Double(arrayItem))
}
return doubleArray
}
The compiler error I get from this when I try FloatingPointType, etc. is: "Cannot find an initializer for type 'Double' that accepts an argument list of type '(T)'"
Now I know another option is to create my own protocol and then extend the types that I am interested in to adopt it, but this just feels like something that exists right under my nose.
Try FloatLiteralConvertible:
import Darwin
// Swift 2.0
func toDoubleArray<T : FloatLiteralConvertible>(arr : [T]) -> [Double] {
return arr.flatMap { $0 as? Double }
}
// Swift 1.2
func toDoubleArray<T : FloatLiteralConvertible>(arr : [T]) -> [Double] {
var result = [Double]()
for a in arr {
if let d = a as? Double {
result.append(d)
}
}
return result
}
let a = toDoubleArray([1, 2, 3])
let b = toDoubleArray([M_PI, 2 as Int, 3.3])
let c = toDoubleArray(["a", "b", "c"]) // Error, cannot convert [String] to [Double]
let d = toDoubleArray([1, 2, 3, "a"]) // Error, cannot convert [NSObject] to [Double]