Home > other >  Trying to learn how to pull data from urls, followed guide completely, still wrong
Trying to learn how to pull data from urls, followed guide completely, still wrong

Time:01-21

This is the guide I'm trying to follow: https://www.youtube.com/watch?v=MBCX1atOvdA&t=878s

This is my code (Exact same as his):

import SwiftUI

struct Response: Codable {
    var results: [Result]
}

struct Result: Codable {
    var trackId: Int
    var trackName: String
    var collectionName: String
}

struct ContentView: View {
    @State private var results = [Result]()

    var body: some View {
        List(results, id: \.trackId) { item in
            VStack(alignment: .leading) {
                Text(item.trackName)
                    .font(.headline)
                Text(item.collectionName)
            }
            }.task {
                await loadData()
        }
    }
    
    func loadData() async {

        guard let url = URL(string: "https://itunes.apple.com/search?term=taylor swift&entity") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                results = decodedResponse.results
            }
        } catch {
            print("Invalid data")
        }
        
    }
    
}

The issue is I get this error:

2022-01-19 21:34:45.807870-0500 pulldata[46561:4433324] [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_* flags 2022-01-19 21:34:45.813838-0500 pulldata[46561:4433324] [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_* flags 2022-01-19 21:34:45.841534-0500 pulldata[46561:4433324] [] nw_resolver_can_use_dns_xpc_block_invoke Sandbox does not allow access to com.apple.dnssd.service 2022-01-19 21:34:45.842830-0500 pulldata[46561:4433324] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not permitted 2022-01-19 21:34:45.842911-0500 pulldata[46561:4433324] [connection] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) 2022-01-19 21:34:45.843380-0500 pulldata[46561:4433324] Connection 1: received failure notification 2022-01-19 21:34:45.844386-0500 pulldata[46561:4433324] Connection 1: failed to connect 10:-72000, reason -1 2022-01-19 21:34:45.844448-0500 pulldata[46561:4433324] Connection 1: encountered error(10:-72000) 2022-01-19 21:34:45.845461-0500 pulldata[46561:4433327] Task <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1> HTTP load failed, 0/0 bytes (error code: -1003 [10:-72000]) 2022-01-19 21:34:45.846729-0500 pulldata[46561:4433327] Task <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={_kCFStreamErrorCodeKey=-72000, NSUnderlyingError=0x6000024aff90 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0, ipv4, dns, _kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1>" ), NSLocalizedDescription=A server with the specified hostname could not be found., NSErrorFailingURLStringKey=https://itunes.apple.com/search?term=taylor swift&entity, NSErrorFailingURLKey=https://itunes.apple.com/search?term=taylor swift&entity, _kCFStreamErrorDomainKey=10} Invalid data 2022-01-19 21:34:45.883310-0500 pulldata[46561:4433298] [com.what.pulldata] copy_read_only: vm_copy failed: status 1.

Yes I know that the site doesn't exist anymore, but the code doesn't work with any site and I feel it has something to do with this part of the error

Sandbox does not allow access to com.apple.dnssd.service

Any other videos showing me how to get data from websites using swift, or any help with this guide would be greatly appreciated!

CodePudding user response:

try something like this with optionals, works for me:

import SwiftUI

struct Response: Codable {
    let results: [Result]
}

struct Result: Codable {
    var trackId: Int?        // <--- here optional
    var trackName: String?    // <--- here optional
    var collectionName: String? // <--- here optional
}

struct ContentView: View {
    @State private var results = [Result]()
    
    var body: some View {
        List(results, id: \.trackId) { item in
            VStack(alignment: .leading) {
                Text(item.trackName ?? "").font(.headline) // <--- here optional
                Text(item.collectionName ?? "") // <--- here optional
            }
        }.task {
            await loadData()
        }
    }
    
    func loadData() async {
        guard let url = URL(string: "https://itunes.apple.com/search?term=taylor swift&entity") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                results = decodedResponse.results
            }
        } catch {
            print("Invalid data")
        }
    }
  
}
  •  Tags:  
  • Related