Comments (5)
If you expect the response content to already have been transformed into a String
by the earlier transformers in the pipeline, then do this:
service().configureTransformer("/foo") { $0.content as String } // example 0
This doesn’t transform the content, but it does cause Siesta to report it as an error if the content comes out as anything other than a string.
If you are getting something else from the transformers, an Int
for example, and you want to convert it into a string, do this:
service().configureTransformer("/foo") { String($0.content as Int) } // example 1
Or if you prefer a very explicit approach:
service().configureTransformer("/foo") { // example 2
(content: Int, entity: Entity) -> String in
String(content)
}
Example 2 is what the Swift compiler infers from example 1, all spelled out. Those two are equivalent.
The underlying principle to all of this is that configureTransformer(...)
uses Swift’s type inference to determine your transformer’s input and output type. That means that your closure needs to make it clear to the compiler exactly what types it expects, resulting in two common confusions:
- If you just use a bare
$0
in your transformer closure, Swift doesn’t know whether you mean thecontent
argument, or the entire(content,entity)
tuple. Usually Swift can infer which one you mean, but becauseconfigureTransformer(...)
’s first argument is a generic type, there’s not enough information in this case. Saying$0.content
removes that ambiguity. - You need to make sure that
$0.content
can only have one possible type in the context where you use it, or the compiler can’t infer its type. There are manyString(_:)
initializers, so you need to pick one using theas
clause. Similarly, you need to make sure your closure has an unambiguous return type.
from siesta.
Awesome! That helps immensely.
Works great now! Thanks!
from siesta.
Glad it helped!
Please keep the issues coming. It’s really helpful to know how people are using Siesta, and where the stumbling blocks are.
from siesta.
from siesta.
How to map the model for the response string i am using object mapper and am facing the same issue
MyAPI.configureTransformer("foo") { //
Mapper().map($0)
}
Something like this and i get the error as
Cannot invoke 'configureTransformer' with an argument list of type '(String, (_) -> _)'
from siesta.
Related Issues (20)
- Possible memory leak in ProgressTracker HOT 13
- tvOS Carthage compilation broken for Xcode 9 HOT 2
- Swift 4.2 + Xcode 10 HOT 5
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:66: 'UIApplicationDidReceiveMemoryWarning' has been renamed to 'UIApplication.didReceiveMemoryWarningNotification' HOT 1
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:42: Type 'NSNotification.Name' has no member 'UIApplication' HOT 1
- Readme 404 link HOT 2
- unexpected duplicate task error for new Xcode build system from Xcode 10 IOS 12.0.1 HOT 1
- How to refresh same web service ?
- Wildcard in PipelineStage.add(_, contentTypes:) doesn't work as expected
- RemoteImageView placeholderImage not showing when imageResource or ImageURL is nil HOT 2
- File-based caching for Siesta HOT 4
- Unexpected version number in 'available' attribute for non-specific platform '*' HOT 2
- Cached Resource timestamp refresh after loading
- Proposal: More elegant way to avoid memory leak in closures HOT 4
- Y'all got wiki-spammed HOT 2
- Chained request progress support
- 404 errors from a resource don't clear as expected from a resource("static url").load()
- Xcode SPM will not accept Quick@around-each dependency HOT 7
- The package product 'Alamofire' requires minimum platform version 10.0 for the tvOS platform, but this target supports 9.0 HOT 3
- Quick dependancy is not correctly versioned HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from siesta.