Hi,I am having the following issue with multiple targets in a workspace and SwiftUI previews and am wondering, if that's a bug or a feature?I have a workspace containing an iOS and a watchOS target. Both targets have a source file with the name "FooView.swift" in them, which implements the same UI (using SwiftUI of course), but specific for the particular platform. When trying to use SwiftUI previews after switching targets it only works after a full clean of the project. I assume this is because either the filenames or class names clash somewhere.The other issue I saw is that HostingController.swift filename (generted by Xcode) clashes with some generated HostingController.swift file used by the SwiftUI previews in the iOS target. To be clear the iOS target has no HostingViewController.swift. I assume it's generated to "host" the SwiftUI preview. After renaming only the file (not the class) to WatchHostingViewContoller.swift everything was working again.So I am wondering if that should be considered expected behaviour or bugs?Thank you, Michael
Post
Replies
Boosts
Views
Activity
Hi,I have the issue (on iOS/iPadOS) once a NavigationLink was tapped/used it won't trigger the navigation again after navigating back. The most minimal code example would be:struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: PushedView()) {
Text("Press Me").font(.largeTitle)
}
}
}
struct PushedView: View {
var body: some View {
Text("Hello")
}
}
}So once "Push Me" was tapped and the navigation triggered it won't work again after navigating back. (Only tested in simulator so far.) Am I missing something?Kind regards, Michael
Hello,I recently see sometimes crashing errors with statements like "precondition failure: attribute failed to set an initial value: 103" logged to the console. The numbers at the end vary. Has anybody found a good way (or any way besides blindly trying to change things ;-) to track down those errors?Cheers, Michael
Hi,I made a small test program trying to figure out how @FetchRequest, List, ForEach work together with large data sets. At first glance it seems (judging from the memory consumption) that all the entities are loaded and not just those which are displayed (see code below). I am now wondering, if there is a way to setup FetchRequest (or the List, or...) in a way so it does the lazy loading for only visible items? Or is this just not (yet) possible?Here is the SwiftUI coding I am using.import SwiftUI
struct ContentView: View {
@FetchRequest(entity: TrackCoordinate.entity(), sortDescriptors: [])
var tracks: FetchedResults<TrackCoordinate>
var body: some View {
VStack {
List {
ForEach(tracks, id: \.self) { track in
Text("\(track.x) \(track.y)")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}Thanks, Michael
Hi,I have the issue, when navigating from a Form to a List (see sample code below) the List "jumps" once it appeared. Has anybody else encountered this and maybe a solution/workaround? Or is something wrong with the code?If I remove Text("Other"), it does not jump anymore, but then again I need more than one element in the Form ;-).Cheers, Michaelimport SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Form {
Text("Other")
NavigationLink(destination: NextView()) {
Text("to Next View")
}
.navigationBarTitle("Here")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// MARK: - NextView -
struct NextView: View {
var body: some View {
List {
Text("A")
Text("B")
Text("C")
}
.navigationBarTitle("There")
}
}
Hi,say I have a List. Each entry in the List is a NaviagtionLink, which has the same View (NextView) as the destination. I want NextView to display a different random number each time I navigate to that view and I want give this number as a parameter to NextView. How would I do that?(Of course, this is a contrived example to illustrate the issue, what I really want to do is inject a newly generated CoreData child managed context each time a navigation happens.)(Please see the code sample at the end of the post.If I just use NavigationLink(destination: NextView(n: Int.random(in: 0 ... 9))) it will always show the same number, because NextView[...] will be evaluated only, if the "outer" view body is evaluated.NavigationLink(destination: NextView(n: Int.random(in: 10 ... 20)), isActive: $showEdit) somehow retriggers the "outer" view body evaluation, so I get a new random number each time. But this seems like a hack.Ideally there would be a way, that evaluate NextView at the point in time when the navigation happens, but I can't think of a way to do this.Thanks in advance for any ideas, pointers, hints on what's wrong and how it should be done right ;-)Cheers, Michaelimport SwiftUI
struct ContentView: View {
@State var showEdit = false
var body: some View {
NSLog("ContentView Body")
return
NavigationView {
List {
NavigationLink(destination: NextView(n: Int.random(in: 10 ... 20)), isActive: $showEdit) {
Text("With binding")
}
NavigationLink(destination: NextView(n: Int.random(in: 0 ... 9))) {
Text("Without binding")
}
}
}
}
}
struct NextView: View {
var n: Int
init(n: Int) {
self.n = n
NSLog("init: \(n)")
}
var body: some View {
NSLog("Next View body")
return
VStack {
Text("Next View - What else")
Text("** \(n) **")
}
}
}
func getNew(number: Int) -> TheObject {
let obj = TheObject(number: number)
NSLog("getNew \(obj.number)")
return obj
}
class TheObject: ObservableObject {
@Published var number: Int
init(number: Int) {
self.number = number
}
deinit {
NSLog("Deinit \(self.number)")
}
}
Hi,when using an ForEach within an ScrollView the UI does not get updated if the ObservedObject, which is used in ForEach, changes. If the ForEach is not embedded in a ScrollView the update works.IMHO the ForEach within the ScrollView should work, or am I wrong?To illustrate the issue here's some example coding to paste in an (iOS) Playground.import PlaygroundSupport
import SwiftUI
struct User: Hashable {
var name: String
}
typealias Users = [User]
let usersData = [
User(name: "Alpha"),
User(name: "Beta"),
User(name: "Gamma"),
]
class UserViewModel: ObservableObject {
@Published var users: Users = []
func get() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
print("Update")
self.users = usersData
}
}
}
// If the ScrollView is present nothing is shown after the model is updated.
// If a screen refresh is triggered (for example when running this on device and switch
// from portrait to landscape the UI shows up.
struct ContentView: View {
@ObservedObject var user = UserViewModel()
var body: some View {
ScrollView { // comment scrollview out and it works
ForEach(user.users, id: \.self) { user in
Text("\(user.name)")
}
}
.onAppear {
self.user.get() // to simulate model update
}
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())Thanks for your Feeback!Cheers, Michael
Hi,does anyone know the reason why a mounted volume might not ave a `URLResourceValues.volumeUUIDString`?I have two mostly identical SD Cards (same size, formatted in the same camera). One has a volumeUUIDString when mounted and the other has none.The documentation for volumeUUIDString only says:"The volume's persistent `UUID` as a string, or nil if a persistent `UUID` is not available for the volume.". So no real explanation given why there might be no volumeUUIDString. Or what might be another option for uniquely identifying a volume.Any hints, info highly appreciated.Cheers, Michael
Hi,
is it possible to have access to NSApplicationDelegate methods like application(_ sender: NSApplication, openFiles filenames: [String])
when using the Swift UI App Life Cycle?
With "access" I mean equivalent functionality. I know there is support for a document based app using the Swift UI App Life Cycle, which would seem to resolve the issue for that particular openFiles delegate method, but for various reasons I can't use the document based app life cycle.
Thank you!
Cheers, Michael
Hi,
I have a strange case involving sheets, which I think it's a bug, but then again I might be missing something.
Using the following code:
import SwiftUI
enum WhichSheet: String {
	case one, two, three, none
}
struct ContentView: View {
	@State private var _showSheet = false
	@State private var _whichSheet: WhichSheet = .none
		var body: some View {
			VStack(spacing: 8) {
					Button("One Sheet", action: { self._whichSheet = .one; self._showSheet = true})
					Button("Two Sheet", action: { self._whichSheet = .two; self._showSheet = true})
					Button("Three Sheet", action: { self._whichSheet = .three; self._showSheet = true})
			}
				.sheet(isPresented: $_showSheet, content: {
				Text("whichSheet = \(_whichSheet.rawValue)")
			})
		}
}
struct ContentView_Previews: PreviewProvider {
		static var previews: some View {
			ContentView()
		}
}
I would assume that, depending on which button is pressed, I would say a sheet with the text "whichSheet = one" or "whichSheet = two" etc.
But no matter which button is pressed first the text on the sheet is always "whichSheet = none". Only if you choose a different button the second (or third, or ...) time the correct text is being displayed.
Bug or am I missing something really obvious?
(Test using an iOS 14 / 14.2 project with Xcode 12 / 12.2 beta)
Cheers, Michael
Hi,
I am wondering if anybody who is facing issues with @State not working properly in iOS 14 has received any update on that from Apple?
Since having working @State properly is quite essential to have a working SwiftUI implementation I am a bit puzzled to not have gotten any update at all on the issue from Apples side.
That's the issue I am encountering:
https://developer.apple.com/forums/thread/660976
Here are a few other threads from the forum which at least seem to be related to this issue:
https://developer.apple.com/forums/thread/652080
https://developer.apple.com/forums/thread/652258
https://developer.apple.com/forums/thread/661818
https://developer.apple.com/forums/thread/660927
https://developer.apple.com/forums/thread/661777
https://developer.apple.com/forums/thread/659660
https://developer.apple.com/forums/thread/661754
Hi,
I have a very simple program (see below) with some Views in an HStack, which is within a ScrollView. This works fine on iOS, but on macOS nothing scrolls. Am I missing something? Shouldn't it just scroll?
import SwiftUI
struct ContentView: View {
	var body: some View {
		ScrollView(.horizontal, showsIndicators: true) {
			HStack {
				ItemView(n: 1)
				ItemView(n: 2)
				ItemView(n: 3)
				ItemView(n: 4)
				ItemView(n: 5)
			}
		}
		.frame(minWidth: 350, maxWidth: 800, minHeight: 250, maxHeight: 250, alignment: .center)
	}
}
struct ContentView_Previews: PreviewProvider {
	static var previews: some View {
		ContentView()
	}
}
struct ItemView: View {
	var n: Int
	
	var body: some View {
		Rectangle()
			.frame(width: 300, height: 200)
			.overlay(Text("\(n)").foregroundColor(.white))
	}
}
Hi,
I want to have more than one MKAnnotation to be selected, but so far it seems that it is not possible, is that true? If yes are there any workarounds?
MKMapView has the selectedAnnotations property, which is an Array and also set-able, but the documentation states:
"Assigning a new array to this property selects only the first annotation in the array.". So I am wondering why this even is an Array and if there is in fact a way to have multiple annotations selected, that I am not aware of...
Any help, hints highly appreciated :-).
Cheers, Michael
Hi,
the documentation for @State says:
You should only access a state property from inside the view’s body, or from methods called by it. Therefore I am wondering of this is an incorrect (as aValue is set/accessed outside of a "view's body") usage of @State which, while it works now, might break in the future:
@main
struct SwiftUIArrayUpdateTestApp: App {
	@State private var aValue = 5
	var body: some Scene {
		WindowGroup {
			ContentView(value: $aValue)
		}
	}
}
Hi,
I am trying to develop a "ui control", which should react on, for example, the cursor keys when in focus. I could not find anything in the docs regarding keyboard and focus handling.
Has anybody any hints on how to achieve this or where to get information on that topic?
Thank you!
Cheers, Michael