NavigationLinkError (from YT Video) - swift

I Was following this tutorial (https://www.youtube.com/watch?v=GiCTgsH0dtk) and got this error anyone know a work around others in the comments having trouble with the same thing so i thought id help
Do you know the correct way to handle this new error for this piece of code?
NavigationLink(destination: SignUp(show: self.$show), isActive: self.$show) {
Text("")
}
.hidden()
Login(show: self.$show)
}
Error:
'init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a NavigationStack or NavigationSplitView

First, change your NavigationLink to NavigationStack.
And lastly, where your - if you have one - .navigationTitle("") goes (this is where NavigationView closes), Xcode is asking you to add a .navigationDestination() as following:
.navigationDestination(isPresented: $show) {
//Whatever View you want to display...
}
This alert, not error, happens because of iOS 16 and Apple's constant changes on the Swift and Swift's frameworks changes.

Related

SwiftUI on Mac: Help Text Always Visible Even within View with Zero Opacity

I have run into some unexpected behavior while using SwiftUI in a macOS app. I filed a Feedback with Apple in case it's a bug, but it might actually be designed to work this way, so I'm looking for a workaround.
I rely heavily on the use of .opacity() to show and hide different sections of my app with tabs. I don't use if clauses because each time the user changes the tab, you have to wait for the entire view to rebuild and that is pretty slow.
Here's a basic example that demonstrates the problem:
struct ContentView: View {
#State var viewAVisible = false
var body: some View {
VStack{
ZStack{
Text("View A Visible")
.frame(width: 500, height: 500)
.background(Color.blue)
.help("This is View A's help text. It should be invisible when View A is invisible.")
.opacity(viewAVisible ? 1 : 0)
Text("View B Visible")
.frame(width: 500, height: 500)
.background(Color.gray)
.opacity(viewAVisible ? 0 : 1)
}
Button("Toggle"){
viewAVisible.toggle()
}
}.padding()
}
}
The default app state is to hide the "View A" Text() and only show the "View B" Text(). But if you hover over View B, you still see View A's .help text:
In my opinion, if a view has .opacity(0) then its help text shouldn't show up. But regardless, I need to find a way around this.
I thought about doing something like this:
.help(viewAVisible ? "This is View A's help text..." : "")
...but that doesn't scale across dozens of views in my app--particularly among child views that don't know if their parent view is shown or hidden. As I mouse across my app, I see the help text of tons of views all over the place even though they are invisible. 😅
Has anyone run into this or have any suggestions on how to handle it?
Looks like a bug (they do not remove tracking rects), here is a demo of workaround - move help tag into background and remove it manually (tested with macOS 12.0.1)
Text("View A Visible")
.frame(width: 500, height: 500)
.background(Group {
if viewAVisible {
Color.blue.help("This is View A's help text. It should be invisible when View A is invisible.")
} else {
Color.clear
}
})
.opacity(viewAVisible ? 1 : 0)

NavigationLink syntax

I'm trying to learn SwiftUI and Swift in general so I'm a total newbie here. I started by reading docs on Swift Language, making a console app and playing around with a bunch of concepts of the language there. Then I tried SwiftUI and immediately I see a bunch of syntax that wasn't documented in the language itself.
So I dug around some more and I see there is a bunch of compiler and language magic going on behind the scenes which makes the syntax I see in SwiftUI work. A lot of the magic maybe isn't so well documented. So I dug around and found answers to most of my questions about the SwiftUI syntax I'm seeing. Except this:
NavigationView{
VStack {
Text("Select your device")
List{
NavigationLink{
SensorView()
} label:{
Text("Raspberry Pi Zero")
}
}
}
What is with the label: statement outside of the curly braces for the NavigationLink closure all about? I can see in my app that it creates a label in the NavigationLink but I don't understand why the label: is outside of the curly braces where it looks to me to be more or less detached from the NavigationLink it is seemingly associated with? I'm trying to understand this so I will know when/where to apply this pattern. I copied the code above from someone else's sample.
Any insighgts or teachings would be appreciated.
This new syntax is part of Multiple Trailing Closures, which was released recently. Let's look at your NavigationLink initializer:
init(destination: () -> Destination, label: () -> Label)
Here, there's 2 parameters that take in closures: destination and label. So, that means you can write it like this (the plain, normal way):
NavigationLink(destination: {
Text("The destination")
}, label: {
Text("Click me!")
})
Or this, via trailing closure syntax:
NavigationLink(destination: {
Text("The destination")
}) {
Text("Click me!")
}
Or what you did in your question. Note: when you have multiple trailing closures, you need to add the argument label (for example label:) for all closures after the first.
NavigationLink {
Text("The destination")
} label: {
Text("Click me!")
}

SwiftUI Group{} Nested in VStack{} Throwing Errors

I am receiving two errors whenever I attempt to add a Group struct to the VStack struct. The first error is on line 5 VStack { and reads:
Static method 'buildBlock' requires that 'Group' conform to 'View'
The second error is on line 6 Group { and reads:
Missing arguments for parameters 'id', 'label' in call
Here is the complete code for the view file:
import SwiftUI
struct FailTest: View {
var body: some View {
VStack {
Group {
Text("1")
Text("2")
Text("3")
Text("4")
Text("5")
Text("6")
Text("7")
Text("8")
Text("9")
Text("10")
}
Text("11")
}
}
Any idea why this is throwing these two errors? The code is based on multiple examples I have found online, as well as two books.
I cannot reproduce this problem using just the code you've provided (plus a trailing brace that this seems to be missing, suggesting this might not be precisely all the code in the fille). I suspect you other code in the project, possibly another definition of Group that is causing the problem. I recommend moving just this code into a Playground to explore.
I'm testing with the latest Xcode 11.5 GA build, in case that is a difference.

SwiftUI view sometimes creates Thread 1: EXC_BAD_ACCESS (code=1, address=0x6002005b8368), when I put array as a parameter

SwiftUI View file
struct NoteListView: View {
#EnvironmentObject var presenter: NoteListPresenter
var body: some View {
NavigationView{
AddNoteButton()
Text(presenter.noteViewModels[0].title)
}
}
}
struct NoteListView_Previews: PreviewProvider {
static var previews: some View {
NoteListView()
}
}
I first got this error when I finished writing the logic and started to writing Views. After add my custom button AddNoteButton() to main view, it gave the following error, although it worked before I added this button. I tried to rewrite my views, but although it works at the beginning at some random point of time it throws this error again(by other words sometimes it works, but when i add some irrelevant code it stops working and discarding changes doesn't solve the problem). It always highlights line, where I use noteViewModels array inside of the NoteListView.
Also I added method which prints array to presenter and it always prints everything.
func printAll(){
for element in self.noteViewModels{
print(element.title)
}
}
I can not add other files to question, because there is too many of them. But i hope that I gave enough information, If you think that there is not enough information you are free to ask me or you can check out github:
https://github.com/ShynggysSaparbek/ViperNotesWithRealm/
Xcode version 11.3
Mac OS version Catalina 10.15.2

SwiftUI - NavigationView Error message - Argument passed to call that takes no arguments

I am trying to implement a really basic NavigationView in SwiftUI. When I try the sample code that I have seen on other websites it generates an error message in Xcode. I am not sure why or how to fix this.
I have tried to clean the project, quit Xcode-Beta and restart it but that did not work.
struct ContentView : View {
var body: some View {
NavigationView {
Text("This is a great app")
}
}
}
I thought the code above should work but the error I get says:
"Argument passed to call that takes no arguments."
Any ideas or suggestions?
VStack can only take 10 argument.
If more, there will be error, so you should make it nested.
from
VStack{
}
to
VStack{
VStack{
}
VStack{
}
}
I had this same error message too and figured out what I did wrong and then kind of felt like an idiot. Ha ha.
Take a look:
It took me a while to figure out that my struct was the same name as a previously defined struct VStack. Whoops!
So I'm wondering if you had a file in your project that did this too.
check-in your app is there any Swifui class with the name NavigationView.
also when you jump to the definition from NavigationView it should refer to:
#available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 7.0, *)
public struct NavigationView<Content> : View where Content : View {
public init(#ViewBuilder content: () -> Content)
/// The type of view representing the body of this view.
///
/// When you create a custom view, Swift infers this type from your
/// implementation of the required `body` property.
public typealias Body = Never
}
Testing Xcode 11.2.1 and it's still buggy. I noticed when I keep adding primitive views to my ContentView, I start getting errors like
"Argument passed to call that takes no arguments",
"Type of expression is ambiguous without more context" etc. on primitive views which worked before.
When I replaced, for example,
ScrollView {
VStack {
Text
Button
Image
Text
Button
Image
Text
Button
Image
...
}
}
with
ScrollView {
VStack {
VStack {
Text
Button
Image
}
VStack {
Text
Button
Image
}
VStack {
Text
Button
Image
}
...
}
my code started to compile and run again.
I found this problem when I accidentally try to redefine Text struct. Check if you naming your custom class the same as those in SwiftUI.
#Matteo Pacini helped me find the answer. When I started a new Xcode Project just to test the code above everything worked. I had a lot of files and was testing a lot of different code while experimenting with SwiftUI in my other project and for some reason XCode was always generating this error.
When I tried everything in a new project it worked. Something to be aware of while testing. Hope this helps others avoid similar problems.
Embed the 'Text("This is a great app")' in a VStack
struct ContentView : View {
var body: some View {
NavigationView {
Stack {
Text("This is a great app")
}
}
}
}