I think I used to get error complaining about having a local variable in the view builder. I also looked up online and found this:
Closure containing a declaration cannot be used with function builder 'ViewBuilder'
However, today when I tried it out, I was able to create local variables in a view builder without any problem:
#ViewBuilder
private var foo: some View {
let something = 3
Text("foo")
Text("bar")
}
May I know why?
When SwiftUI was first released (in 2019), the Swift language didn't allow local declarations (like let something = 3) inside what were then called “function builders”. However, function builders evolved into what are now called “result builders” and do support local declarations as of Swift 5.4.
This behavior is documented in SE-0289 Result Builders:
Declaration statements
Local declarations are left alone by the transformation. This allows developers to factor out subexpressions freely to clarify their code, without affecting the result builder transformation.
Related
I've applied the guidance on programmatic usage of M2Doc (also with this help) to successfully generate a document via the API, which was previously prepared by using the M2Doc GUI (configured .docx plus a .genconf file). It seems to also work with a configured .docx, but without a .genconf file.
Now I would like to go a step further and ease the user interface in our application. The user should come with a .docx, include the {m:...} fields there, especially for variable definition, and then in our Eclipse application just assign model elements to the list of variables. Finally press "generate". The rest I would like to handle via the M2Doc API:
Get list of variables from the .docx
Tell M2Doc the variable objects (and their types and other required information, if that is separately necessary)
Provide M2Doc with sufficient information to handle AQL expressions like projectmodel::PJDiagram.allInstances() in the Word fields
I tried to analyse the M2Doc source code for this, but have some questions to achieve the goal:
The parse/generate API does not create any config information into the .docx or .genconf files, right? What would be the API to at least generate the .docx config information?
The source code mentions "if you are using a Generation" - what is meant with that? The use of a .genconf file (which seems to be optional for the generate API)?
Where can I get the list of variables from, which M2Doc found in a .docx (during parse?), so that I can present it to the user for Object (Model Element) assignment?
Do I have to tell M2Doc the types of the variables, and in which resource file they are located, besides handing over the variable objects? My guess is no, as using a blank .docx file without any M2Doc information stored also worked for the variables themselves (not for any additional AQL expressions using other types, or .oclAsType() type castings).
How can I provide M2Doc with the types information for the AQL expressions mentioned above, which I normally tell it via the nsURI configuration? I handed over the complete resourceSet of my application, but that doesn't seem to be enough.
Any help would be very much appreciated!
To give you an impression of my code so far, see below - note that it's actually Javascript instead of Java, as our application has a built-in JS-Java interface.
//=================== PARSING OF THE DOCUMENT ==============================
var templateURIString = "file:///.../templateReqs.docx";
var templateURI = URI.createURI(templateURIString);
// canNOT be empty, as we get nullpointer exceptions otherwise
var options = {"TemplateURI":templateURIString};
var exceptions = new java.util.ArrayList();
var resourceSetForModels = ...; //here our application's resource set for the whole model is used, instead of M2Doc "createResourceSetForModels" - works for the moment, but not sure if some services linking is not working
var queryEnvironment = m2doc.M2DocUtils.getQueryEnvironment(resourceSetForModels, templateURI, options);
var classProvider = m2doc.M2DocPlugin.getClassProvider();
// empty Monitor for the moment
var monitor = new BasicMonitor();
var template = m2doc.M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI, queryEnvironment, classProvider, monitor);
// =================== GENERATION OF THE DOCUMENT ==============================
var outputURIString = "file:///.../templateReqs.autogenerated.docx";
var outputURI = URI.createURI(outputURIString);
variables["myVar1"] = ...; // assigment of objects...
m2doc.M2DocUtils.generate(template, queryEnvironment, variables, resourceSetForModels, outputURI, monitor);
Thanks!
No the API used to parse an generate don't modifies the template file nor the .genconf file. To modify the configuration of the template you will need to use the
TemplateCustomProperties class. That will allow you to register your metamodels and service classes. This instormation is then used to configure the IQueryEnvironment, so you might also want to directly configure the IQueryEnvironment in your code.
The generation in this context referes to the .genconf file. Note The genconf file is also an EMF model, so you can also craft one in memory to launch you generation if it's easier for you. But yes the use of a .genconf file is optional like in your code example.
To the list of variables in the template you can use the class TemplateCustomProperties:
TemplateCustomProperties.getVariables() will list the variables that are declared with their type
TemplateCustomProperties.getMissingVariables() to list varaibles that are used in the template but not declared
You can also find le list of used metamodels (EPackage nsURIs) and imported services classes.
The type of variables is not needed at generation time, it's only needed if you want to validate your template. At generation time you need to pass a map from the variable name to its value as you did in your example. The value of a variable can be a any object from your model (an EObject), a String, an Integer, ... If you want to use something like oclIsKindOf(pkg::MyEClass) you will need to register the nsURI of pkg first see the next point.
The code you provided should let you use something like projectmodel::PJDiagram.allInstances(). This service needs a ResourceSetRootEObjectProvider() that is initialized in M2DocUtils.getQueryEnvironment(). But you need to declare the nsURI of your metamodel in your template (see TemplateCustomProperties). This will register it in the IQueryEnvironment. You can also register it yourself using IQueryEnvironment.registerEPackage().
This should help you finding the missing parts in the configuration of the AQL environment. Your code seems good and should work when you add the configuration part.
Following part 3 of Apple's tutorial on SwiftUI, Handling User Input, I get this error:
Unable to infer complex closure return type; add explicit type to disambiguate
I'm using the same code as the tutorial (even coping from the 'Complete' sample code doesn't resolve the error).
Does anyone have a cue what is going wrong here?
struct LandmarkRow: View {
var landmark: Landmark
var body: some View {
HStack {
landmark.image(forSize: 50)
Text(verbatim: landmark.name)
Spacer()
if landmark.isFavorite {
Image(systemName: "star.fill")
.imageScale(.medium)
}
}
}
}
Regardless of the cause of the issue, how could you indeed add an explicit type to disambiguate here? What would the return type in such a case be?
--
Update
Apparently you should not continue with your result of the 2nd tutorial with the 3rd. Some things changed in between of the tutorials that is not documented in the tutorial. I've added the project files to Github so you can check out the diff.
It's better to start the 3rd tutorial fresh with a fresh download of the Project files of the 3rd tutorial.
The issue is not with the closure, but with the isFavorite property on landmark.
It is not declared on the Landmark type, and the compiler instead of showing the undeclared property error, unable to resolve the stacks build closure return type, so it shows and error there.
Great job Apple tutorial writers and even better one Xcode11 compiler.
To fix:
Declare isFavorite variable on the Landmark type.
Make sure you update the landmarkData.json for every landmark record with the isFavorite = false entry, otherwise the app will crash at runtime.
Some background to this problem
Like #dirtydanee already answered there is a difference between those two tutorials. But the problem behind the problem is that while it looks like you're doing a configuration it's actually just functions nested in functions using generics and protocols to "magically" parse everything into a compiling function.
However conformance to these generics and protocols need to be pretty precise because if not the whole tree of functions cannot compile anymore. But it's hard to determine for the compiler what conformance actually failed. This is why you see an error at the top rather than at the point where it actually happens.
It's strongly advised to make sure your views are decomposed into natural and simple blocks so you're not pouring over hundreds of lines of View code to find that one bug.
Dave DeLong had a really great talk about how to compose Views from ViewControllers that still holds true until today: basically you never use View as a subview inside another View but you need to decompose your View of many, simple Views. Otherwise these errors'll drive you nuts.
For people getting this error on SwiftUI and looking for a way to debug their view stack
This error happens mainly when there is a compilation issue on one child View.
1 - Make sure your parent view can support multiple subviews (VStack, ZStack) and you have less than 10 subviews. Sometime you may want to add a Group wrapper.
2 - If this is not the problem, there is probably an issue with one subview. Try to isolate the one you suspect to have the issue. Usually I copy the subview into a property and a different error appears
var debug: some View {
MyViewWithError(property: self.property)
}
Most of the time you'll encounter this because you pass the property (self.property) instead of a binding (self.$property)
Hope this can help some people
I have run into this error when made a typo. Also this error appears when a code has syntax issues. Just check if your changes are correct
I encountered the problem in a subview where a #Binding prop was of a different type of the #Binding source.
I just tried to install the latest version of Realm (2.1.0) for Swift 3 and I'm getting an error on this line: let realm = RLMRealm(path: databasePath) - DataManager.swift:258:21: Argument labels '(path:)' do not match any available overloads
The declaration of the argument is here:
var databasePath : String
I know that swift 3 is requiring label for the first argument, but I've looked everywhere for the replacement and can't find an answer.
+ [RLMRealm realmWithPath:] was an API that was deprecated and entirely removed from Realm many months ago. It was replaced with + [RLMRealm realmWithConfiguration:], where you supply an RLMRealmConfiguration object with the file path to the target file set in its fileURL property.
let configuration = RLMRealmConfiguration.defaultConfiguration()
configuration.fileURL = URL(path: databasePath)
let realm = RLMRealm(configuration: configuration)
On a side note, unless you've got a specific reason for using the Objective-C version of Realm in Swift, I highly recommend you use the native Swift version of Realm instead. That one should be a lot easier, and feel a lot more natural in Swift 3. :)
I am using microsoft dynamics navision 2009. And I am using code unit. But if I press f5 on a function, for example this:
lRecStatus.SETRANGE("Change Status",lRecStatus."Change Status"::Released);
So I press f5 on: lRecStatus and then I hit f5 and then I see this:
See image. But how to know witch table it is then?
Thank you
You'll find all of the variable decelerations in one of two places, global or local, this includes the reference to the table.
From the C/AL Editor you can access these under the view menu (Beeld for you I think).
C/AL Globals will be available for the whole object
This will open directly to the variables tab, and you can see the type and subtype there.
C/AL Locals will be available for just the function, so you will want to be in the same procedure for the variable you are looking for.
There are three places a local variable being used can be found.
Parameters: These are passed to and from the function
Return Value: this is a basic data type used to return from the function
Variables: this are variables local in scope to that specific
function
Is it possible to save all my custom expressions inside an external .jsx file, so I can call them from inside my projects?
For example, let's say I have an expression I use a lot, that ties a puppet tool point to a null object's position. I know I can I transform that expression into a function that could take, say, two arguments, but can I save it into a custom library, so that I can easily re-use it inside the project?
Right now, I'm simply copying and pasting the same code from property to property, and I'm beginning to feel there is an alternative way to do this more efficiently.
Thanks in advance!
I tested this with success, put this on top of your expression:
$.evalFile("/Users/myname/ae/functions.txt");
Then you can access whatever functions you have in your functions.txt file.
See here for more details: https://forums.creativecow.net/readpost/227/29337
The problem with this method is that you still need to include the extra line for each expression.
Anyway here is my complete setup, just in case: my expression is:
$.evalFile("/Users/myname/ae/functions.txt");
var p1 = thisComp.layer("Null 1").transform.position;
var p2 = thisComp.layer("Null 4").transform.position;
var p = p2 - p1;
printPosition(p)
And my functions.txt file contains the following:
function printPosition (p){
return " " + parseInt(p[0]) + " : " + parseInt(p[1])
}
A bit old question but I found a similar ling's solution without external files.
Add a null layer for your function
Add a marker and write your function on comment
Call eval on your comp->layer->marker->comment
When you want call your function you only need one line:
eval(thisComp.layer("FunctionLayer").marker.key(1).comment);
test(1);
I don't think that is possible.
But you can accomplish a lot by saving the expression as a preset (by dragging the actual layer property to the Effects & Presets window.
Note than you can the apply a preset to several layers by selecting them and using the Animation->Apply Animation Preset function
Hope that helps a little.