Does Swift support implicit conversion?
There is no implicitly cast in Swift.
Easy way of conversion in swift is using constructor of particular type.
Like if you want to get Float from double then you can use Float(doubleValue)
and Same way if you want to convert float to integer then you can use Int(floatValue)
.
In your case:
let intValue = UInt8(doubleValue)
Beware that you will lose any value after the decimal point. So, choose a better way. Above conversion is just to help you in understanding.
Note that Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.
In Swift is it possible to create implicit conversions for your class types?
Swift doesn't support implicit conversion - the closest equivalent is done through extra constructors. So to convert a Double to an Int:
let i: Int = Int(myDouble)
In your case, you could define an extension to Int to create a new init that takes fauxInit as a parameter:
extension Int {
init(_ myFaux: FauxInt) {
self = myFaux.value
}
}
let fauxInt = FauxInt(14)
let realInt = Int(fauxInt)
Is it Possible To Set Up Implicit Conversion from NSError to Custom Error in Swift?
No. Swift generally avoids implicit conversions by design. They even took out the tools we used to use to implement it (__conversion
) because it creates a lot of headaches, both for the compiler and by injecting subtle bugs. It is very common that implicit conversion introduces small differences that matter. It is often important that the programmer consider these things and not fall back on magic. This has been a long source of bugs in C and C++.
If you add an _
before the error
(so the label isn't required) then explicit conversion is trivial:
init(_ error: NSError) { ... }
...
CustomError(error)
See also Does Swift support implicit conversion?
Implicit conversion from UnsafeBufferPointer to UnsafeRawBufferPointer
You cannot cast a UnsafeBufferPointer
to a UnsafeRawBufferPointer
because that is more than reinterpreting the pointer: It requires
to calculate the raw byte count.
But you can create a UnsafeRawBufferPointer
from a UnsafeBufferPointer
:
withUnsafePointer(to: &x) { (ptr) in
let buff = UnsafeBufferPointer(start: ptr, count: 1)
let rawBuff = UnsafeRawBufferPointer.init(buff)
printBuffer(address: rawBuff, as: Int.self)
}
Here is the implementation of that initializer in UnsafeRawBufferPointer.swift.gyb:
/// Creates a raw buffer over the contiguous bytes in the given typed buffer.
///
/// - Parameter buffer: The typed buffer to convert to a raw buffer. The
/// buffer's type `T` must be a trivial type.
@_inlineable
public init<T>(_ buffer: UnsafeMutableBufferPointer<T>) {
self.init(start: buffer.baseAddress!,
count: buffer.count * MemoryLayout<T>.stride)
}
Make a struct or class implicitly convertible to String
class C: CustomStringConvertible {
let property1: Int;
init(i: Int) {
property1 = i
}
var property2: ()->() = { print("some computed property") }
var description: String {
return "instance of this class C has two properties: property1: \(property1) and property2: \(property2) whith type \(property2.dynamicType)"
}
}
let c = C(i: 100)
let s: String = c.description
print(s) // instance of this class C has two properties: property1: 100 and property2: (Function) whith type () -> ()
see that
print(c)
gives you the same result!
and
var text: String = ""
print(c, toStream: &text)
print(text)
sets the text to the same value. By the way
print("\(c.property2), \(c.property2())")
// prints two lines
/*
some computed property
(Function), ()
*/
UPDATE
what about extension String:IKString { ... } ??
struct Localisation {}
protocol IKString {
mutating func foo()
init(s: String, l: Localisation)
}
extension String: IKString {
init(s: String, l: Localisation) {
// some code
self = s
foo()
}
mutating func foo() {
self = self.uppercaseString
}
}
let s2 = String(s: "blabla",l: Localisation())
print(s2) // BLABLA
Related Topics
How to Change App Icon Programmatically
How to Mutate Structs in Swift Using Map
Xcode 6: No Frameworks Folder in Project Navigator
Send Programmatically Sms on Jailbreak Device
Xcode 6.3 Adding Margins to Tableviewcell
Dismiss View Controller with Custom Animation
How to Make Post Nsurlrequest with 2 Parameters
Prevent Uialertcontroller to Dismiss
How to Change Uitableviewrowaction Title Color
How to Use Keychain for Saving Password Like Generickeychain Sample Code
Does Swift Support Implicit Conversion
Why Is Uitextfield.Text an Optional
Cannot Use Instance Member Within Property Initializer
How to Add Image in Uitableviewrowaction
Segue in Skscene to Uiviewcontroller
How to Change Uidatepicker to a Specific Time (In Code)
Nscache Doesn't Work with All Images When Loading for the First Time