Questions tagged [rawrepresentable]
27 questions
1
vote
2 answers
Without using an associated type, can you restrict a variable in a protocol to only RawRepresentables where the raw type is String?
I have a case where I am trying to define a function that takes in an array of objects with the requirement that each object must define a string-based enum called 'Commands'.
Here's an example of how you would do it if you were using an associated…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
1
vote
0 answers
Swift: Associate a tuple to a enum
Is there a way to associate a tuple to a enum?
For example:
typealias MyTuple = (Int, String)
enum A: MyTuple {
case .Foo = (1, "Bazz")
}
I get the error 'A does not conform RawRepresentable'

Godfather
- 4,040
- 6
- 43
- 70
1
vote
1 answer
I want to return a RawRepresentable's rawValue as an Any, but I only receive it as an Any
so I have a function which receives an Any and it checks if the Any is an enum by using reflection:
func extractRawValue(subject: Any) throws -> Any {
let mirror = Mirror(reflecting: subject)
guard let displayStyle = mirror.displayStyle,
…

Evert
- 2,022
- 1
- 20
- 29
1
vote
1 answer
Generic of type RawRepresentable is misinterpreted as self it seems
To use NSCoding with Swift's Enum type I made an extension on NSCoder:
extension NSCoder {
func encodeEnum(value: Enum, forKey key: String) {
self.encodeObject(value.rawValue, forKey:…

Jens Kohl
- 5,899
- 11
- 48
- 77
1
vote
2 answers
Swift 2.1 ErrorType does not conform protocol RawRepresentable
I have declared error type
enum UserServicesError: ErrorType {
case UserNotLogged
}
but I get an error
Argument type 'UserServicesError' does not conform to expected type 'ErrorType'
Type 'UserServicesError' does not conform to protocol…

Aleš Oskar Kocur
- 1,381
- 1
- 13
- 29
0
votes
0 answers
Comparing Object properties
I'm trying to compare two objects to check which properties were updated. I'm using Mirror to list the properties and try the comparison. Code is:
let mirrorLote = Mirror(reflecting: l)
let mirrorLoteUpd = Mirror(reflecting:…

Ilian Felinto
- 63
- 10
0
votes
2 answers
Swift - Why String doesn't conform to RawRepresentable?
I'm learning Swift and cannot realize why this code is correct:
enum Test1: String {
case value
}
let test1 = Test1.value.rawValue
but this one is incorrect and shows me errors
struct MyStruct {
}
extension MyStruct: Equatable {
static…

user14205680
- 113
- 6
0
votes
1 answer
Protocol 'RawRepresentable' requires 'init(rawValue:)' to be available in iOS 13.0.0 and newer
I am currently building a framework, and would like to conform SwiftUI Color to the RawRepresentable protocol only for iOS 14. I have the following code:
@available(iOS 14.0, *)
extension Color: RawRepresentable {
public init?(rawValue: Data) {
…

YulkyTulky
- 886
- 1
- 6
- 20
0
votes
2 answers
Swift protocol for things that convert to and from String
I'd like to have a GenericThing with a template parameter that is any type that can sensibly be converted to and from a string.
// ConvertsToAndFromString is a made up protocol here – what should I use instead?
struct GenericThing

Benjohn
- 13,228
- 9
- 65
- 127
0
votes
1 answer
Invalid redeclaration of rawValue in Release build
I have a mixed project and came across an interesting issue.
There's an enum, defined in obj-c
typedef NS_ENUM (NSUInteger, ABCCategory) {
ABCCategoryFirst,
ABCCategorySecond
};
Next, there's a swift file where an extension is defined
extension…

bitemybyte
- 971
- 1
- 10
- 24
0
votes
1 answer
Struct does not conform to RawRepresentable protocol?
I have a struct here, which generates errors when Xcode tries to compile it
public struct GATToIPPermissions : OptionSet {
public init(rawValue: UInt)
public static var read: GATToIPPermissions { get {}}
public static var write:…

bingbomboom
- 209
- 1
- 4
0
votes
1 answer
Storing Enum type in Swift
I am working on Enum serialization for my library (https://github.com/JiriTrecak/Warp) and I got little stuck on storing the enum type in the property (I need to know that type so I can serialize / deserialize it on demand).
I have a struct that…

Jiri Trecak
- 5,092
- 26
- 37