Invalid redeclaration of typealias
I don't quite see what the typealias
is for. The function declaration alone is sufficient to tell the compiler what BindType
must be.
The problem I found with your code (apart from the missing Observable declaration, of course) is that the Reactive class itself doesn't conform to TwoWayBindDelegate. To get around that, I threw in an arbitrary implementation of twoWayBind
. When I did, and when I deleted the unnecessary typealias
declarations, your code compiled for me:
struct Observable<T> {}
protocol TwoWayBindDelegate: class {
associatedtype BindType
func twoWayBind(to observable: Observable<BindType>?, observableChanged: ((BindType) -> ())?)
}
class Reactive<Base: UIView>: TwoWayBindDelegate {
public var base: Base
init(base: Base) {
self.base = base
}
func twoWayBind(to observable: Observable<Int>?, observableChanged: ((Int) -> ())?) {
}
}
extension Reactive where Base == UISlider {
func twoWayBind(to observable: Observable<Float>?, observableChanged: ((Float) -> ())?) {
}
}
extension Reactive where Base == UITextField {
func twoWayBind(to observable: Observable<String>?, observableChanged: ((String) -> ())?) {
}
}
Swift typealias error
This is not a correct function type declaration:
typealias LoginHandler = (_ msg: String) -› Void
You are using the wrong "greater than" symbol (unicode hex 203A).
Your code should look like this:
typealias LoginHandler = (_ msg: String) -> Void
Notice that difference in the second character making up the "arrow operator" (unicode hex 003E).
Invalid redeclaration of rawValue in Release build
The raw value syntax for enums in Swift is “just” a shorthand for conformance to the RawRepresentable protocol. It’s easy to add this manually if you want to use otherwise unsupported types as raw values.
Source
I'm not sure why it works in debug because when you create a typed enum you are already 'conforming' to RawRepresentable
. So when you create an NS_ENUM
it is imported in to swift like so:
public enum ABCCategory : UInt {
case first
case second
}
Meaning that it already conforms to RawRepresentable
. The fix can be achieved two ways, one in Swift and in Objective-C
In Swift we just remove the RawRepresentable
and change rawValue
to stringValue
, and RawValue
to String
:
extension ABCCategory {
var stringValue: String {
switch self {
case .first: return "first"
case .second: return "second"
}
}
init(_ value: String) {
switch value {
case "first":
self = .first
case "second":
self = .second
default:
self = .first
}
}
}
Or you could just change the Objective-C to use NS_TYPED_ENUM
. Some info here. However this will change your enum to a struct
.h
typedef NSString *ABCCategory NS_TYPED_ENUM;
extern ABCCategory const ABCCategoryFirst;
extern ABCCategory const ABCCategorySecond;
.m
ABCCategory const ABCCategoryFirst = @"first";
ABCCategory const ABCCategorySecond = @"second";
This will be imported by swift like so:
public struct ABCCategory : Hashable, Equatable, RawRepresentable {
public init(rawValue: String)
}
public static let first: ABCCategory
public static let second: ABCCategory
How does class know associated type?
So, what does Coordinator = Void mean and why does defining a nested class specify the associate type?
Coordinator = Void
means that coordinator is optional and you can have representable without coordinator, ie. Void
is default type.
Does it infer it from the return type of makeCoordinator?
Yes, it does.
Why won't it let me additionally explicitly specify it using typealias Coordinator = Self.Coordinator?
Because typealias
introduces new type, but here you want Coordinator = Coordinator
, so compiler reports duplication. If you want you can introduce it as
typealias OneMoreCoordinator = Self.Coordinator
Swift protocol with generic method: invalid redeclaration of implementation
You have no problem the mistake is var value
, and redeclaring a function with name of func value<Bool>
, i just changed the variable name and it worked, the error clearly says
error: invalid redeclaration of 'value()'
class BoolImpl: MyProtocol {
var bool: Bool
init() {
self.bool = false
}
init(value: Bool) {
self.bool = value
}
func value<Bool>() -> Bool {
return self.bool as! Bool
}
}
Error with type alias
Template aliases are not supported in gcc 4.6 : https://gcc.gnu.org/gcc-4.6/cxx0x_status.html
upgrade your compiler to a more recent version.
Extending type aliases in swift
typealias
just change or rename the type. It does not create another user type for you. You are actually extending Float
for Speed
, Altitude
again.
You can pass 180
to your custom struct by conforming Literals
types.
let mySpeed: Speed = 180
FloatLiteralConvertible
and IntegerLiteralConvertible
will give you same functionality you want and you can directly assign values to your custom struct types
as you assign to Float
struct Speed: FloatLiteralConvertible,IntegerLiteralConvertible {
var distance:Float
init(floatLiteral value: Float) {
distance = value
}
init(integerLiteral value: Int){
distance = Float(value)
}
var formatted: String {
return "\(distance * 3.6) km/h"
}
}
let mySpeed: Speed = 180.0
println(mySpeed.formatted) // 5.0 km/h
Property redefinition extension Swift
Well, there's two ways to look at this. One is that it's a bug, the other is that it isn't. :) The canonical report is probably the one at https://bugs.swift.org/browse/SR-3228 [WARNING: that URL may go bad when Apple moves over to GitHub Issues for its Swift bug reporting]. But read also the discussion of the underlying mechanism from Jordan Rose in the comments to https://bugs.swift.org/browse/SR-8010, where it seems that the real issue is how to maintain Objective-C compatibility.
So yes, Apple knows about this, and they know it would be nice to issue a warning about doing something iffy, but you are in fact allowed to create what amounts to a same-named but different method/property in a different module, so on your head be it if you do so.
Using a typealias when declaring an IBOutlet
Apparently a type alias of an implicit unwrapped optional is treated as a non-optional so you have to explicitly add the exclamation mark:
@IBOutlet var newShipCaptainName: TextField!
Related Topics
Crashlytics Doesn't Work with Xcode 10 Beta
Swift Textview Align with English and Arabic Languages
Assigning Values to Tuple in for Loop
Xcode9/Scenekit - .Dae File Not Loading into Scnscene - Returns Nil
Stripe API Response: The Data Couldn't Be Read Because It Isn't in The Correct Format
Parsing JSON from Url Ends Up with an Error - Swift 5
Invalid Update: Invalid Number of Rows in Section 1
Do Protocols Have an Effect on The Retain Count
Switch Statement Where Value Is Int But Case Can Contain Array
Swiftui Change on Multilevel Children Published Object Change
User Already Authenticated in App and Firebase Realtime Database Returns Failed: Permission_Denied
Avspeechsynthesizer Isspeaking Not Working in Swift
Using Auto Layout to Orientate Stack Views Vertically in Portrait and Horizontally in Landscape
iOS-Charts Error: Thread1: Exc_Bad_Access (Code=2, Address=0X2A0C220)