Disclaimer: Another very basic question below. I am trying to learn the basics of IOS development.
I'm currently trying to parse data from an API to a SwiftUI project and am not able to successfully do so.
The code goes as follows:
import SwiftUI
struct Poem: Codable, Hashable {
let title, author: String
let lines: [String]
let linecount: String
}
struct ContentView: View {
var poems = [Poem]()
var body: some View {
VStack {
if let poem = poems.first {
Button("Refresh") {getPoem()}
Text("\(poem.author): \(poem.title)").bold()
Divider()
ScrollView {
VStack {
ForEach(poem.lines, id: \.self) {
Text($0)
}
}
}
}
}
}
}
func getPoem() {
let url = URL(string: "https://poetrydb.org/random/1")!
// 2.
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let poemData = data {
// 3.
let decodedData = try JSONDecoder().decode([Poem].self, from: poemData)
DispatchQueue.main.async {
self.poems = decodedData
}
} else {
print("No data")
}
} catch {
print("Error")
}
}.resume()
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The code does not build. The error thrown happens in the Func getPoem where "Cannot find 'self' in scope".
Any ideas? All help is appreciated.
I meant something like
class FetchPoem: ObservableObject {
// 1.
#Published var poems = [Poem]()
init() {
getPoem()
}
func getPoem() {
let url = URL(string: "https://poetrydb.org/random/1")!
// 2.
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let poemData = data {
// 3.
let decodedData = try JSONDecoder().decode([Poem].self, from: poemData)
DispatchQueue.main.async {
self.poems = decodedData
}
} else {
print("No data")
}
} catch {
print("Error")
}
}.resume()
}
}
struct PoemContentView: View {
#ObservedObject var fetch = FetchPoem()
var body: some View {
VStack {
Button("Get Next Poem") { fetch.getPoem() }
if let poem = fetch.poems.first {
Text("\(poem.author): \(poem.title)").bold()
Divider()
ScrollView {
VStack {
ForEach(poem.lines, id: \.self) {
Text($0)
}
}
}
} else {
Spacer()
}
}
}
}
Related
I need to create new array using objects from Core Data.
When I am trying to do that, I am getting an error (purple warning) on line:
for item in items {
Warning message:
Accessing StateObject's object without being installed on a View. This
will create a new instance each time.
My app runs, but it results in empty array.
Expected result: New array with all the objects from Core Data.
How to make it work?
Data model in this example is very simple: 1 entity: Item with 1 attribute: text: String.
EXAMPLE CODE
ContentView.swift
import SwiftUI
import CoreData
struct Item2: Identifiable {
let id: UUID
var text: String
}
struct ContentView: View {
#Environment(\.managedObjectContext) private var viewContext
#FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.text, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var items2 = [Item2]()
init() {
for item in items {
items2.append(Item2(
id: UUID(),
text: item.text!
))
}
}
var body: some View {
NavigationView {
List {
ForEach(items2) { item2 in
Text(item2.text)
}
}
.toolbar {
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
ToolbarItem(placement: .destructiveAction){
Button(action: deleteAll) {
Image(systemName: "trash")
.foregroundColor(.red)
// .accessibilityLabel("Delete")
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.text = "List item"
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteAll() {
items.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
Here's one solution :) Credits to my good friend and awesome iOS developer Yasuhito Nagatomo (AtarayoSD on Twitter)
The solution is very simple. I just needed to use computed property instead of init().
Here's the code:
import SwiftUI
import CoreData
struct Item2: Identifiable, Equatable {
let id: Int
var text: String
}
struct ContentView: View {
#Environment(\.managedObjectContext) private var viewContext
#FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.text, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var items2: [Item2] {
items.enumerated().map {
Item2(id: $0.0, text: $0.1.text!)
}.reversed()
}
var body: some View {
NavigationView {
List {
ForEach(items2, id: \.self.id) { item2 in
Text(item2.text)
}
}
.toolbar {
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
ToolbarItem(placement: .destructiveAction){
Button(action: deleteAll) {
Image(systemName: "trash")
.foregroundColor(.red)
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.text = "List item"
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteAll() {
items.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
this is my Model
public struct Welcome: Decodable{
public let userslist: [Userslist]
}
public struct Userslist: Decodable, Hashable{
public let full_name: String
public let partner_media: [PartnerMedia]
public init( partner_media: [PartnerMedia]) {
self.partner_media = partner_media
}
}
public struct PartnerMedia: Decodable , Hashable{
public var id = UUID()
public let url: String
public init( url: String) {
self.url = url
}
}
This is View Model I follow the MVVM pattern for accessing the data from API.
class PublisherModelVM: ObservableObject {
#Published var datas = [PartnerMedia]()
let url = "APIUrl"
init() {
getData(url: url)
}
func getData(url: String) {
guard let url = URL(string: url) else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
if let data = data {
do {
let results = try JSONDecoder().decode(Welcome.self, from: data)
DispatchQueue.main.async {
self.datas = results.userslist `//Cannot assign value of type '[Userslist]' to type '[PartnerMedia]' what should I write for getting proper response`
}
}
catch {
print(error)
}
}
}.resume()
}
}
I want to fetch the url and full_name And set to the View
struct PublisherListView: View{
#StateObject var list = PublisherModelVM()
var body: some View{
ScrollView(.horizontal,showsIndicators: false){
ForEach(list.datas, id: \.id){ item in
Text(item.full_name)
AsyncImage(url: URL(string: item.url)){image in
image
.resizable()
.frame(width: 235, height: 125).cornerRadius(8)
}placeholder: {
Image(systemName: "eye") .resizable()
.frame(width: 235, height: 125).cornerRadius(8)
}
}
}
}
}
this Error show in my Xcode Cannot assign value of type '[Userslist]'
to type '[PartnerMedia]'
Please help me.
can anyone help me for recommending for API related full detailed
courses and thank you in advance
As I said before (in the questions you have deleted) pay attention to the details of your models to match the json data. Try this approach, works very well for me:
struct ContentView: View {
var body: some View {
PublisherListView()
}
}
struct ServerResponse: Decodable {
let userslist: [User]
}
struct User: Decodable, Identifiable {
let id: Int
let totalBooks: Int
let totalfollowers: Int
let fullAddress: String?
let partnerMedia: [PartnerMedia]
enum CodingKeys: String, CodingKey {
case id, totalBooks,totalfollowers
case partnerMedia = "partner_media"
case fullAddress = "full_address"
}
}
struct PartnerMedia: Decodable, Identifiable {
let id: Int
let url: String
}
struct PublisherListView: View{
#StateObject var list = PublisherModelVM()
var body: some View{
ScrollView(.horizontal,showsIndicators: false){
HStack(spacing:15) {
ForEach(list.datas, id: \.id){ item in
AsyncImage(url: URL(string: item.url)){ image in
image
.resizable()
.frame(width: 235, height: 125).cornerRadius(8)
} placeholder: {
Image(systemName: "eye") .resizable()
.frame(width: 235, height: 125).cornerRadius(8)
}
}
}
}
}
}
class PublisherModelVM: ObservableObject {
#Published var datas = [PartnerMedia]()
let url = "https://alibrary.in/api/publisherList"
init() {
getData(url: url)
}
func getData(url: String) {
guard let url = URL(string: url) else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
if let data = data {
do {
let results = try JSONDecoder().decode(ServerResponse.self, from: data)
DispatchQueue.main.async {
for user in results.userslist {
self.datas.append(contentsOf: user.partnerMedia)
}
}
}
catch {
print(error)
}
}
}.resume()
}
}
I have a class that has a calculated property, which is an array consisting of instances of the structure.
struct Team: Identifiable, Codable, Hashable {
var id = UUID()
var name : String
}
class TeamRow : ObservableObject {
#Published var teamsArray : [Team] = [] {
didSet {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(teamsArray) {
UserDefaults.standard.setValue(encoded, forKey: "Teams")
}
}
}
init() {
if let teams = UserDefaults.standard.data(forKey: "Teams") {
let decoder = JSONDecoder()
if let decoded = try? decoder.decode([Team].self, from: teams) {
self.teamsArray = decoded
return
}
}
}
}
Also, I have a view, with the ability to add elements(teams) to this array using a sheet.
struct PlayersRow: View {
#ObservedObject var teams = TeamRow()
#State private var team = ""
#State private var showTeamAddSheet = false
var body: some View {
Form {
ForEach(teams.teamsArray) { team in
Text(team.name)
.font(.system(size: 20))
.padding(.horizontal, 110)
.padding(.vertical, 10)
}
}
.navigationBarTitle("Teams")
.navigationBarItems(trailing: Button(action: {
self.showTeamAddSheet = true
}) {
Image(systemName: "plus")
.foregroundColor(.black)
.font(.system(size: 30))
})
.sheet(isPresented: $showTeamAddSheet) {
AddPlayerView(teams: self.teams)
}
}
}
This is a sheet view.
struct AddPlayerView: View {
#Environment(\.presentationMode) var presentationMode
#ObservedObject var teams : TeamRow
#State private var team = ""
var body: some View {
NavigationView {
Form {
TextField("Add new team", text: $team)
}
.navigationBarItems(trailing: Button(action: {
let newTeam = Team(name: self.team)
self.teams.teamsArray.append(newTeam)
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("Сохранить")
.font(.custom("coolJazz", size: 20))
.foregroundColor(.black)
}))
.navigationBarTitle("Add Team")
}
}
}
And I have a view where I need to output the array elements one by one, using a button, clicked on the button, the view screen displayed 0 element, clicked on the button, displayed first element, etc.
struct GameView: View {
#ObservedObject var teams = TeamRow()
//#State var index = 0
var body: some View {
VStack(spacing: 40) {
//Text(teams.teamsArray[index]) this isn't worked, return an error: Initializer 'init(_:)' requires that 'Team' conform to 'StringProtocol'
Button(action: {
}) {
Text("press it")
}
}
}
}
struct GameView_Previews: PreviewProvider {
static var previews: some View {
GameView().environmentObject(TeamRow())
}
}
if the array is set initially in the class, I have no problem displaying the elements on the screen by increasing the index, but how to solve this problem I do not know...
Can some one explain newbie?
Change your GameView Code to following:
struct GameView: View {
#ObservedObject var teams = TeamRow()
#State var index = 0
var body: some View {
VStack(spacing: 40) {
if teams.teamsArray.count > index {
Text(teams.teamsArray[index].name)
}
Button(action: {
index += 1
}) {
Text("press it")
}
}
}
}
Im learning swiftui at this moment. But now i have come across a problem.
Im trying to append data to an array that is an struct.
struct outfit:Identifiable {
var id = UUID()
var user: String
var amount: Double
var rating: Double
}
and the other file
import SwiftUI
import Firebase
import FirebaseStorage
import FirebaseFirestore
struct rate: View {
private var db = Firestore.firestore()
#State var user = Auth.auth().currentUser
#State private var outfitcards = [outfit]()
#State private var cards = [1, 2, 3]
#State private var offset = [CGSize.zero, CGSize.zero]
init () {
loadcards()
}
var body: some View {
GeometryReader { frame in
ZStack{
VStack {
Text("outfit")
.font(.largeTitle)
.padding()
ZStack {
Text("No cards to show")
.frame(width: frame.size.width * 0.6, height: frame.size.width * 1.6)
HStack {
Image(systemName: "record.circle.fill")
.foregroundColor(.red)
Spacer()
Image(systemName: "record.circle.fill")
.foregroundColor(.green)
}
ForEach(outfitcards) { index in
Text(index.user)
}
}
}
}
}
}
func loadcards () {
db.collection("rating").whereField("user", isNotEqualTo: user?.uid ?? "Error")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err.localizedDescription)")
} else {
for document in querySnapshot!.documents {
let cuser = document.get("user") as! String
let camount = document.get("amount") as! Double
let crating = document.get("rating") as! Double
print("user=\(cuser) amount=\(camount) crating=\(crating)")
outfitcards.append(outfit(user: cuser, amount: camount, rating: crating))
}
print(outfitcards)
}
}
}
}
It does print the username, the amount and the rating but then when i print the array itself it is giving me a []. So it doens't append. Also the for each loop is empty so that also means that the array is empty
and nothing is appended
Does anyone know what I do wrong?
Loading data in a View like that is a dangerous practice and it'll get reset/reloaded any time that view is re-rendered (or even re-inited in this case).
Instead, move your code that loads the data to an ObservableObject:
struct Outfit:Identifiable {
var id = UUID()
var user: String
var amount: Double
var rating: Double
}
class Loader : ObservableObject {
private var db = Firestore.firestore()
private var user = Auth.auth().currentUser
#Published var outfitcards = [Outfit]()
func loadcards () {
db.collection("rating").whereField("user", isNotEqualTo: user?.uid ?? "Error")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err.localizedDescription)")
} else {
for document in querySnapshot!.documents {
let cuser = document.get("user") as! String
let camount = document.get("amount") as! Double
let crating = document.get("rating") as! Double
print("user=\(cuser) amount=\(camount) crating=\(crating)")
self.outfitcards.append(Outfit(user: cuser, amount: camount, rating: crating))
}
print(self.outfitcards)
}
}
}
}
struct Rate: View {
#StateObject var loader = Loader()
#State private var cards = [1, 2, 3]
#State private var offset = [CGSize.zero, CGSize.zero]
var body: some View {
GeometryReader { frame in
ZStack{
VStack {
Text("outfit")
.font(.largeTitle)
.padding()
ZStack {
Text("No cards to show")
.frame(width: frame.size.width * 0.6, height: frame.size.width * 1.6)
HStack {
Image(systemName: "record.circle.fill")
.foregroundColor(.red)
Spacer()
Image(systemName: "record.circle.fill")
.foregroundColor(.green)
}
ForEach(loader.outfitcards) { index in
Text(index.user)
}
}
}
}
}.onAppear {
loader.loadcards()
}
}
}
Now, the loader is responsible for making the database call. It updates a #Published property, which the View observes.
Note that I changed the capitalization of a few things (Rate, Outfit). In Swift, the common practice is to capitalize types (classes/structs/enums) and start variable names with lowercase letters.
Also probably worth noting that the way that you're casting the data from firebase (with as!) is dangerous and can crash your program if the data isn't in the format you expect. Better to use optional binding (let user = document.get("user") as? String)
Your issue is there you are updating #State value in initializing View, which you are doing in Wrong way, do like this onAppear:
struct rate: View {
private var db = Firestore.firestore()
#State var user = Auth.auth().currentUser
#State private var outfitcards = [outfit]()
#State private var cards = [1, 2, 3]
#State private var offset = [CGSize.zero, CGSize.zero]
var body: some View {
GeometryReader { frame in
ZStack{
VStack {
Text("outfit")
.font(.largeTitle)
.padding()
ZStack {
Text("No cards to show")
.frame(width: frame.size.width * 0.6, height: frame.size.width * 1.6)
HStack {
Image(systemName: "record.circle.fill")
.foregroundColor(.red)
Spacer()
Image(systemName: "record.circle.fill")
.foregroundColor(.green)
}
ForEach(outfitcards) { index in
Text(index.user)
}
}
}
}
}
.onAppear() { loadcards() } // <<: Here
}
func loadcards () {
db.collection("rating").whereField("user", isNotEqualTo: user?.uid ?? "Error")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err.localizedDescription)")
} else {
for document in querySnapshot!.documents {
let cuser = document.get("user") as! String
let camount = document.get("amount") as! Double
let crating = document.get("rating") as! Double
print("user=\(cuser) amount=\(camount) crating=\(crating)")
outfitcards.append(outfit(user: cuser, amount: camount, rating: crating))
}
print(outfitcards)
}
}
}
}
I still have the problem when the count reaches 3, the reset function only stops it, but the count is not set to 0. I use the reset function with a button, it works perfectly. i would like to understand it and hope someone knows the reason for it?
import SwiftUI
import Combine
import Foundation
class WaitingTimerClass: ObservableObject {
#Published var waitingTimerCount: Int = 0
var waitingTimer = Timer()
func start() {
self.waitingTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
self.waitingTimerCount += 1 }}
func stop() { waitingTimer.invalidate() }
func reset() { waitingTimerCount = 0; waitingTimer.invalidate() }
}
struct ContentView: View {
#ObservedObject var observed = WaitingTimerClass()
var body: some View {
VStack {
Text("\(self.observed.waitingTimerCount)")
.onAppear { self.observed.start() }
.onReceive(observed.$waitingTimerCount) { count in
guard count == 3 else {return}
self.observed.reset() // does not work
}
Button(action: {self.observed.start()}) {
Text("Start") }
Button(action: {self.observed.reset()}) { // works
Text("Reset") }
Button(action: {self.observed.stop()}) {
Text("Stop") }
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
It is because reset changes property affecting UI during body construction, so ignored. It should be changed as below
func reset() {
waitingTimer.invalidate()
DispatchQueue.main.async {
self.waitingTimerCount = 0
}
}