Hi,
I want to find the index of an item in an array. I need something like this:
let item = Item(title: "Test")
let index : Int = myArray.indexFor(item)
print(index)
Can you help me?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi,
I' programming an app, where I want to see "Transactions".
They are saved in Firestore and have a Date, an User and money. I want a list, where the users are sorted in sections by the date. Like:
21/3/2020
User1 11:08 AM
User3 11:07 AM
User2 10:08 AM
Is it possible to do that?
Hi, In my SwiftUI app, I have a settings and a content view. I use UserDefaults to save the settings and I user onDisappear() to save it in the UserDefaults. In the content View, I use onAppear() to look for the settings, and if I print both out, the onAppear() function get called in front of the onDisappear() function. Why? Can you help me?
Hi, In my App, I have a SettingsView, where I want to make a toggle. I want to store the information if the toggle is set true or false in the UserDefaults. How can I do that?
Hi, In my code, I have a value which counts calls from Navigation Links. This works fine but I want that if this count is over 1, the ContentView updates and add a cell. At the moment, I have to restart the app and then I have the new cell, but I want, that it updates directly. Is this possible? This is my code:
struct Website : Identifiable, Hashable, Comparable{
static func < (lhs: Website, rhs: Website) -> Bool {
lhs.name < rhs.name
}
var id = UUID()
var url : String
var name : String
var image : String
var calls : Int
}
struct websiteCallsCell : View{
var website : Website
@State var press = false
@State var tap = false
@State var isPresented = false
var body: some View{
NavigationLink(destination: WebView(website: website, url: URL(string: website.url)), label: {
Text(website.name)
.font(.system(size: 20, weight: .semibold, design: .rounded))
.frame(width: UIScreen.main.bounds.width*0.85, height: 60)
.background(
ZStack {
Color(press ? colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) : colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1))
RoundedRectangle(cornerRadius: 16, style: .continuous)
.foregroundColor(Color(press ? colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1) : colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)))
.blur(radius: 4)
.offset(x: -8, y: -8)
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(
LinearGradient(gradient: Gradient(colors: [Color( colorLiteral(red: 0.9019607843, green: 0.9294117647, blue: 0.9882352941, alpha: 1)), Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing)
)
.padding(2)
.blur(radius: 2)
}
)
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.overlay(
HStack {
AnimatedImage(url: URL(string: website.image))
.font(.system(size: 24, weight: .light))
.foregroundColor(Color.white.opacity(press ? 0 : 1))
.frame(width: press ? 220 : 54, height: press ? 4 : 50)
.background(Color( colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)))
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.shadow(color: Color( colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)).opacity(0.3), radius: 10, x: 10, y: 10)
.offset(x: press ? 70 : -10, y: press ? 16 : 0)
Spacer()
}
)
.shadow(color: Color(press ? colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) : colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1)), radius: 20, x: 20, y: 20)
.shadow(color: Color(press ? colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1) : colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)), radius: 20, x: -20, y: -20)
.scaleEffect(tap ? 1.2 : 1)
})
}
}
List{
ForEach(websites){ website in
if UserDefaults.standard.integer(forKey: "calls\(website.name)") > 0 && (UserDefaults.standard.integer(forKey: "lastCallsCount") < 8) {
VStack{
websiteCallsCell(website: website)
lineSpacing(20)
Spacer(minLength: 5)
}
}
}
}
Hi,
In the following code, I filter an array, but it only works with the first letter of the name. For Example, when I write "A" in the TextField, the filter shows me "Apple". But if I write "Ap", the filter shows me nothing. Can you help me?
List{
ForEach(groupedArray.keys.sorted().filter{
text.isEmpty || $0.contains(text)
}, id: \.self){key in
Section(header: Text(key)) {
ForEach(groupedArray[key]!, id: \.self){website in
NavigationLink(
destination: WebView(website: website, url: URL(string: website.url)),
label: {
Text(website.name)
Spacer()
if website.image != ""{
RemoteImage(url: website.image).frame(width: 25, height: 25, alignment: .center)
}else{
Loader()
}
})
}
}
}
}
Hi, I have an array of Strings and want to sort it with Sections from a to z in a list. How can I do it?