I am trying to implement a Result-enum as frequently seen in Swift-based code. I am having the code listed below but when I try to compile this in a shared project I am getting the error Duplicate identifier "error" [/Users/Development/Projects/version-3/config-app/SharedProject/Result.swift (40)] but I am failing to see where duplicate use of error is?
/// An enm representing a typical error for a Result<T>
public enum ResultError: Error {
case invalid(String)
case parseError
case networkError
case notFound
case cancelled
}
/// An enum representing either a failure with an explanatory error, or a success with a result value.
public enum Result<Value> {
case success(Value)
case failure(ResultError)
// MARK: Constructor
/// Constructs a success wrapping a `value`.
public init(value: Value) {
self = .success(value)
}
/// Constructs a failure wrapping an `error`.
public init(error: ResultError) {
self = .failure(error)
}
// MARK: Helper methods
public func dematerialize() throws -> Value {
switch self {
case let .success(value):
return value
case let .failure(error): // <--- ERROR HERE
throw error
}
}
}
If I am trying to the code listed below snippet I am getting the error:
Member "<special>.success" of type "<unknown type>" is a variable but is used as a method [/blah/SharedProject/SensorService.swift (55)]
As far as I know this code should just work as I basically copied it from an existing Swift project but now it’s in a Shared Project and uses Sugar’s Http classes.
/// An enm representing a typical error for a Result<T>
public enum ResultError: Error {
case invalid(String)
case parseError
case networkError
case notFound
case cancelled
}
/// An enum representing either a failure with an explanatory error, or a success with a result value.
public enum Result<Value> {
case success(Value)
case failure(ResultError)
// MARK: Constructor
/// Constructs a success wrapping a `value`.
public init(value: Value) {
self = .success(value)
}
/// Constructs a failure wrapping an `error`.
public init(error: ResultError) {
self = .failure(error)
}
}
typealias LocationList = [Location]
final class SensorService {
public static var sharedInstance: SensorService = {
let sensorService = SensorService()
return sensorService
}()
private init() {
}
func getAvailableSensors(completion completion: (Result<LocationList>) -> ()) {
let request = HttpRequest(Url.UrlWithString("https://www.mocky.io/v2/5c699176370000a90a07fd6f"))
Http.ExecuteRequest(request) { response in
let locations = LocationList()
completion(.success(locations)) // <-- line 55
}
}
}
Yes, they added a Result-type to Swift 5 but as far as I remember SwiftBaseLibrary is a RO library and it doesn’t include this Result-type.
Regarding the use of Elements RTL. I think I am already using it as I think it’s out-of-the-box included in a Swift-project in Fire when creating a Shared Project? My shared project I am having doesn’t seem to allow to add references like this. My iOS project does have libElements as reference
public enum Result<Value, Error: Swift.Error> {
case value(Value), error(Error)
}
It’s an option win the New Project dialog, yes.
Shared Projects can not have references, they only hold files. the references are on the “real” projects. Check out Shared Projects for more on this.
session.dataTask(with: urlRequest) { data, response, error in
guard let data = data, let _ = response as? HTTPURLResponse else { completion(.failure(ResultError.networkError)); return }
do {
guard
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? JSONDictionary,
let jsonData = json["d"] as? JSONDictionary
else {
completion(.failure(ResultError.parseError))
return
}
// TODO: Assumes that documents fetched by ID are always email templates. This will need to change based on a param in the response.
let documentItem = XXXCommTemplate()
documentItem.type = .emailTemplate
documentItem.applyData(jsonData)
completion(.success(documentItem))
} catch {
completion(.failure(ResultError.parseError))
}
}.resume()