Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 498 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert Int to String in Swift

#21
**Swift 4**:

let x:Int = 45
let str:String = String(describing: x)

[Developer.Apple.com > String > init(describing:)][2]

> The String(describing:) initializer is the preferred way to convert an instance of any type to a string.

[Custom String Convertible][3]

[![enter image description here][1]][1]



[1]:

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#22
If you like swift extension, you can add following code

extension Int
{
var string:String {
get {
return String(self)
}
}
}
then, you can get string by the method you just added

var x = 1234
var s = x.string
Reply

#23
let intAsString = 45.description // "45"
let stringAsInt = Int("45") // 45
Reply

#24
**Swift String performance**

A little bit about performance
`UI Testing Bundle` on iPhone 7(real device) with iOS 14
```
let i = 0
lt result1 = String(i) //0.56s 5890kB
lt result2 = "\(i)" //0.624s 5900kB
lt result3 = i.description //0.758s 5890kB
```

``` Swift
import XCTest

class ConvertIntToStringTests: XCTestCase {
let count = 1_000_000

func measureFunction(_ block: () -> Void) {
let metrics: [XCTMetric] = [
XCTClockMetric(),
XCTMemoryMetric()
]
let measureOptions = XCTMeasureOptions.default
measureOptions.iterationCount = 5

measure(metrics: metrics, options: measureOptions) {
block()
}
}

func testIntToStringConstructor() {
var result = ""
measureFunction {
for i in 0...count {
result += String(i)
}
}
}

func testIntToStringInterpolation() {
var result = ""
measureFunction {
for i in 0...count {
result += "\(i)"
}
}
}

func testIntToStringDescription() {
var result = ""
measureFunction {
for i in 0...count {
result += i.description
}
}
}
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through