The following code was written to display a single string (6000 chars) of environment values:
struct ContentView: View {
#Environment(\.description) var description
var body: some View {
VStack {
Text("#Environment Values").font(.title)
ScrollView {
Text("\(description)").font(.caption)
}.padding()
}
}
}
The result is inconsistent on both simulator and iPhone. When run, sometimes the information is displayed as desired on the left but often no values are displayed as shown on the right:
Other things tried didn't resolve the problem. Placing the environment variable in a viewmodel gives the message "Accessing Environment's value outside of being installed on a View. This will always read the default value and will not update."
Splitting the string into two strings gives the same inconsistent results.
Is this a thread issue?
Outside of listing every environment key and value, can anything be done to display the #Environment(.description) consistently?
Related
When executing the .onDelete function, I am getting an index out of range. I have successfully used onDelete elsewhere in my app, deleting data directly from core data. This version is to only delete from a list not stored in core data.
I created some temporary lists for testing, and using this .onDelete was successful. The successful test cases were with arrays that already had data in them and did not need to be initialized. The array used in the code below is one that is initialized when the user lands on this page - could this be why it is failing?
There is lots of code inside this struct, but the relevant parts are below:
#State var recipeStep: [String]
More code, eventually reaching the ForEach causing the problems
List {
ForEach(0..<recipeStep.count, id: \.self) { index in
HStack {
Text(String(stepNumber[index]) + ".").bold()
TextField("", text: $recipeStep[index]).multilineTextAlignment(.leading).padding(.leading)
}
}.onDelete { (indexSet) in
recipeStep.remove(atOffsets: indexSet)}
Any thoughts as to why I am getting an index out of range error?
You loop with recipeStep in
ForEach(0..<recipeStep.count, id: \.self) { index in
While you use both $recipeStep[index] and stepNumber[index]
So recipeStep and stepNumber should be of same size to avoid crashes or better union them in 1 model
I was able to get this to work by modifying my textfield to work the following way:
EditorView(container: self.$recipeStep, index: index, text: recipeStep[index])
The solution was found here, posted by Asperi: https://stackoverflow.com/a/58911168/12299030
I had to modify his solution a bit to fit my forEach, but overall it works perfectly!
Whenever a user returns to a form sub-view after saving data through a binding variable, the form displays old data from before it was edited, and I cannot understand why.
I have an array of Face objects (technically Core Data entities) declared as a State variable in a parent NavigationView. I then loop through them to create NavigationLinks like this:
#State var formDieFaces: [Face] = [] // this gets initialized akin to the below (truncated)
...
let face1 = Face(entity: Face.entity(), insertInto: nil)
self._formDieFaces = State(initialValue: [face1])
...
ForEach (formDieFaces.indices) { curIndex in
NavigationLink(
destination: FaceForm(self.$formDieFaces[curIndex]))
) {
FaceRowView(faceToList: self.formDieFaces[curIndex])
}
}
And in FaceForm, I receive the variable and "save" it as such:
#Binding var faceToEdit: Face
#State var formFaceText: String = String()
...
// in the form
TextField("Placeholder text", text: $formFaceText)
...
// on save, do the below
self.faceToEdit.text = self.formFaceText
What's weird is that on "saving" in FaceForm, the main NavigationView does update, and in that NavigationLink (FaceRowView) everything shows the correct data. But if I click back into FaceForm thereafter, it displays old data from before the save.
I have a feeling I'm doing something phenomenally obtuse with the State and Binding variables, but I just can't for the life of me figure out what's going on. I've tried various combinations of #ObservedObject, #ObservableObject, #StateObject, etc.
Could anyone please point me in the right direction?
Edit: to be clear, FaceRowView works and displays the correct data. FaceForm is where the problem is occurring, almost like it's remembering an old copy of the data.
If it's helpful, here is a link to a video of the behavior: https://youtu.be/8eC-TdtFP5s
For those curious, I'm not 100% sure this is a pure, proper SwiftUI solution, but I found a hack/workaround on this page that I've implemented and seems to have "solved" the problem: https://stackoverflow.com/a/57733708.
I'm working on a weather app where I have subviews for each statistic ex: CurrentWeather, HourlyForecast, and DailyForecast are all separate views
and I am currently making a separate api call for each view but I know that this is very inefficient and I was wondering how I could call the func only once for everything. I have tried putting the func in a .onAppear on the "home screen" of my app which features all of the subviews but it doesn't seen to work as the data I need is being accessed in the subviews.
Any help would be very much appreciated as I am fairly new to SwiftUI
Here is a version of what I am basically doing right now:
#EnvironmentObject var data: WeatherAPI
VStack {
// Weather Condition and Temp
CurrentWeather().environmentObject(WeatherAPI())
//Hourly Forecast
HourlyModuleView()
//Weekly Forecast
ForecastModuleView().environmentObject(WeatherAPI())
}.onAppear(perform: data.loadData)
You should remove over-defines for environment objects and use one from, as I understood, HomeScreen view, because environment object injected into root view becomes automatically available for all subviews, like
#EnvironmentObject var data: WeatherAPI
...
VStack {
// Weather Condition and Temp
CurrentWeather() // << data injected automatically
//Hourly Forecast
HourlyModuleView() // << data injected automatically
//Weekly Forecast
ForecastModuleView() // << data injected automatically
}.onAppear(perform: data.loadData)
I understand how I could, for example look at one element in a table, but what's the correct way to simply iterate through all the elements, however many are found, to for example tap on them?
let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)
Okay I succeeded in figuring out the simple syntax. I have just started working with Swift and so it took me sleeping on it to think of the answer.
This code works:
var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}
print (elementLabels)
My guess is the answer is there is no way to do so.
The reason is because of my experience with one of the critical iOS components while making a number of UI tests: UIDatePicker.
If you record a test where you get the page up and then you spin the picker, you will notice that the commands are all non-specific and are screen related. In the case of the picker, however, community requests resulted in the addition of a method for doing tests: How to select a picker view item in an iOS UI test in Xcode?.
Maybe you can add a helper method to whatever controller contains this table. Also, keep in mind that you can easily add methods without polluting the class interface by defining them as extensions that are in test scope only.
For Xcode 11.2.1, SwiftUI and swift 5 based app, the following code works for testing a list, each element in this case appears as a button in the test code. The table is set up like this (for each row) :
NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
}
Then I catch the members of the table by getting the buttons into an array:
let myTable = app.tables.matching(identifier: "newTopicTable")
var elementLabels = [String]()
for i in 0..<myTable.buttons.count {
elementLabels.append (tablesQuery.buttons.element(boundBy: i).label)
}
print (elementLabels)
Finally, I deleted each member of the table by selecting the detail view where I have a delete button, again with
.accessibility(identifier: "deleteTopic"
I wanted to delete all members of the table:
for topicLabel in elementLabels {
let myButton = app.buttons[topicLabel]
myButton.firstMatch.tap()
app.buttons["deleteTopic"].tap()
}
I could create and replay the following script very well in the mac I Use.
var target = UIATarget.localTarget();
UIATarget.localTarget().delay(15);
target.frontMostApp().mainWindow().tableViews()[0].textFields()[0].tap();
When I run the above script in another mac, It shows error in the 3rd line.
after changing the above script's third line as following, it is replaying fine.
target.frontMostApp().mainWindow().tableViews()[1].textFields()[0].tap();
Just I have changed tableview's index from 0 to 1. How can I achieve this in multiple
mac systems? Both mac are having same xcode version(xcode 5) and simulator version(6.1) and mac version.Why Instruments are taking scripts API differently in different macs?
For more consistent results one of the ways can be to access AX element by name (or other identifier/expected content/number of contained cells etc.). For example here Trouble Getting Elements by Name from UIAElementArray in UIAutomation SO question and corresponding discussion about how name for element can be set in different ways.
For example:
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields()[0].tap();
or
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields.withName("TextFieldName")[0].tap();
If using names are not practicable can be analysed content of the table and according to that right table can be selected. For example if table has some cell with name "Cell name":
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
if (tableViews[0].cells().firstWithName("Cell name")) {
tableViews[0].textFields()[0].tap();
} else if (tableViews[1].cells().firstWithName("Cell name")) {
tableViews[1].textFields()[0].tap();
}
More details about identifying cells in tableView for example in SO question UIACollectionView cells vs visibleCells This looks not very nice but it can be reasonable workaround and should work quite reliable. If number of cells are know and different in these tables than number of cells can be compared to find required table.