SFSafeSymbols / SFSafeSymbols

Safely access Apple's SF Symbols using static typing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many cases can't be initialized from raw value

skjiisa opened this issue · comments

In many cases, for example 15.square.fill, the rawValue returned from its enum case (SFSymbol._15SquareFill) will return the proper name (15.square.fill), but it cannot be initialized from its rawValue (SFSymbol(rawValue: 15.square.fill) returns nil).

@isvvc Thanks for raising this issue!

I think this is not only the case for some symbols, but for all of them. The reason is that due to the new versioning that was required for SFSymbols v2, I couldn't just write

case someSymbol = "some.symbol"

any longer, but had to change it to

case someSymbol

.
.
.

var rawValue: String {
    switch self {
    case .someSymbol: return "some.symbol"
    ...
    }
}

which of cause breaks the init(rawValue:) functionality.

The approach suggested in #65 is probably the best way to fix this. If we would stick with the enum implementation, a generated initializer would be needed, switching over the rawValue string. But that's only an idea if it turns out there are issues with the approach presented in #65.

As @ddddxxx discovered in #71, rawValue initialization works in fact, but not with the actual rawValue but with the enum case name:

SFSymbol(rawValue: "15.square.fill") // nil
SFSymbol(rawValue: SFSymbol._15SquareFill.rawValue) // nil (same as above)

let symbol SFSymbol(rawValue: "_15SquareFill") // works
symbol.rawValue // 15.square.fill

As you see, this is very inconsistent and I highly recommend not using initialization via the raw value until #65 is addressed which would also solve this issue.

Fixed in #72