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?
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?
Hi, I have a problem with the following code:
struct AddView: View {
@State var text:String = ""
@State private var image1 : String?
@State private var showingPicker = false
@Binding var listItems: [Item]
@State private var inputImage : String?
var body: some View {
VStack{
TextField("Name", text: $text).padding().background(Color(.systemGray5)).frame(width: 400,alignment: .center).cornerRadius(20)
Image(systemName: "person.circle")
.frame(width: 170, height: 170, alignment: .center)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(Circle()
.stroke(Color.blue))
.position(x: 209, y: 100)
.onTapGesture {
self.showingPicker = true
}.sheet(isPresented: $showingPicker, onDismiss: loadImage){
ImagePicker(image: self.$inputImage)
}
Button(action: {
listItems.append(Item(name: text, image: "Gif"))
UserDefaults.standard.saveItems(listItems)
print(listItems)
}, label: {
Text("Add")
})
}
}
func loadImage(){
guard let inputImage1 = inputImage else {
print(inputImage)
return
}
}
}
extension UserDefaults {
func saveItems(_ items: [Item]) {
let encoder = JSONEncoder()
if let data = try? encoder.encode(items) {
set(data, forKey: "Items")
}
}
}
struct ImagePicker : UIViewControllerRepresentable{
class Coordinator : NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate{
let parent : ImagePicker
init(_ parent: ImagePicker){
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[UIImagePickerController.InfoKey : String]){
if let uiImage = info[.originalImage]{
parent.image = uiImage
}
parent.presentaitionMode.wrappedValue.dismiss()
}
}
@Environment(\.presentationMode) var presentaitionMode
@Binding var image : String?
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController{
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
}
When I print out the inputImage, it says it is nil. Why is it like this? Can you help me please?
Hi, I have a problem with the following code:
struct AddView: View {
@State var text:String = ""
@State private var image : UIImage?
@State private var showingPicker = false
@Binding var listItems: [Item]
@State private var inputImage : UIImage?
var body: some View {
VStack{
TextField("Name", text: $text).padding().background(Color(.systemGray5)).frame(width: 400,alignment: .center).cornerRadius(20)
Image(systemName: "person.circle")
.frame(width: 170, height: 170, alignment: .center)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(Circle()
.stroke(Color.blue))
.position(x: 209, y: 100)
.onTapGesture {
self.showingPicker = true
}.sheet(isPresented: $showingPicker, onDismiss: loadImage){
ImagePicker(image: self.$inputImage)
}
Button(action: {
listItems.append(Item(name: text, image: inputImage))
UserDefaults.standard.saveItems(listItems)
print(listItems)
}, label: {
Text("Add")
})
}
}
func loadImage(){
guard let inputImage1 = inputImage else {
image = inputImage
return
}
}
}
extension UserDefaults {
func saveItems(_ items: [Item]) {
let encoder = JSONEncoder()
if let data = try? encoder.encode(items) {
set(data, forKey: "Items")
}
}
}
struct ImagePicker : UIViewControllerRepresentable{
class Coordinator : NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate{
let parent : ImagePicker
init(_ parent: ImagePicker){
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[UIImagePickerController.InfoKey : Any]){
if let uiImage = info[.originalImage] as? UIImage{
parent.image = uiImage
}
parent.presentaitionMode.wrappedValue.dismiss()
}
}
@Environment(\.presentationMode) var presentaitionMode
@Binding var image : UIImage?
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController{
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
}
struct Item : Identifiable, Codable{
var id = UUID()
var name : String
var image : String
}
When I want to append an Image to the listItems, I get the following Error:
Cannot convert value of type 'UIImage?' to expected argument type 'String'
I don't know what I can do to change it, because when I change the Type of image in Item to UIImage, I get these errors:
Type 'Item' does not conform to protocol 'Decodable'
Type 'Item' does not conform to protocol 'Identifiable'
What can I do?
Hi have a question to the following code:
struct ContentView: View {
@State private var listItems = [Item]()
let savedItems = UserDefaults.standard.string(forKey: "Items") ?? "error"
var body: some View {
NavigationView{
List{
ForEach(listItems){ item in
Text(item.name)
}
NavigationLink(
destination: AddView(listItems: $listItems),
label: {
Text(" Add")
.foregroundColor(.blue)
})
}
.navigationBarTitle("MyApp")
}
}
}
struct AddView: View {
@State var text:String = ""
@Binding var listItems: [Item]
var body: some View {
VStack{
TextField("Name", text: $text).padding().background(Color(.systemGray5)).frame(width: 400,alignment: .center).cornerRadius(20)
Button(action: {
listItems.append(Item(name: text, image: "Gif"))
print(listItems)
UserDefaults.standard.set(listItems, forKey: "Items")
}, label: {
Text("Add")
})
}
}
}
If i append something in the AddView, i want to save it in the UserDefaults and append it automatically when the app starts. But I don't know how to do this. Can you help me ?
PS: I'm using AppDelegate and SceneDelegate.
Hi,
i've a problem with the following code:
import SwiftUI
struct ContentView: View {
static var listItems = [Item]()
var body: some View {
NavigationView{
List{
ForEach(ContentView.listItems){ item in
Text(item.name)
}
NavigationLink(
destination: AddView(),
label: {
Text(" Add")
.foregroundColor(.blue)
})
}
.navigationBarTitle("MyApp")
}
}
}
struct Item : Identifiable{
var id = UUID()
var name : String
var image : String
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
When I klick on a Button in my List, I go to another view:
import SwiftUI
struct AddView: View {
@State var text:String = ""
var body: some View {
VStack{
TextField("Name", text: $text).padding().background(Color(.systemGray5)).frame(width: 400,alignment: .center).cornerRadius(20)
Button(action: {
ContentView.listItems.append(Item(name: text, image: "Gif"))
print(ContentView.listItems)
}, label: {
Text("Add")
})
}
}
}
struct AddView_Previews: PreviewProvider {
static var previews: some View {
AddView()
}
}
But when I klick on add in the AddView() and I go back to the First View nothing i changing. Can you help me please?
Hi
I have a question. How can I create a new swift file in s swift file? LG Konstantin
Hey,
I have a question:
Is there a function in Swift that is called when you start the app for the very first time?
LG
konste4sfi
Hello,
I have just started programming with Swift and I have a little question. I reprogrammed a quiz app from a tutorial and now have the following problem:
I have a main screen and a result screen. The score is counted in the first view controller and now I need this score in the second view controller. How can I access the variable in the first from the second controller?
Thank you!