Swift Global Variables - json

I'm a beginner at Swift so let me know if this doesn't quite make sense, but i have a JSON file that i can access in swift and parse into an array, from there i can get a string from the array and store it in a var. I want to be able to access this variable globally but i'm not sure how to do it.
With the help of another user "rmaddy". I have this code:
struct Games: Decodable {
let videoLink: String
}
class BroadService {
static let sharedInstance = BroadService()
func fetchBroadcasts(completion: #escaping ([Games]?) -> ()) {
let jsonUrlString = "LINK IS HERE."
guard let url = URL(string: jsonUrlString) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {
completion(nil)
return
}
do {
let games = try JSONDecoder().decode([Games].self, from: data)
completion(games)
} catch let jsonErr {
print("Error deserializing json:", jsonErr)
completion(nil)
}
}.resume()
}
}
I can then access it in another class from here:
BroadService.sharedInstance.fetchBroadcasts { (games) in
if let games = games {
let game = games[indexPath]
let videoLink = game.videoLink
}
I want to be able to access the contents of "videoLink" globally, without having to use "BroadService.sharedInstance.fetchBroadcasts { (games) in" how would i go about doing this

You shouldn't use global variables, I don't think that's recommended in any language.
Now here you have what looks like a Singleton class (BroadService), that's good because it's a nice solution for what you're looking for.
Next all you need to do is add a property to that class. Let's say videoLink is a string, you can add a string property to BroadService, for example storedVideoLink as an optional String, and the next time you need to obtain that value after you have already fetched it, you can access it like so: BroadService.sharedInstance.storedVideoLink.
One more thing, to have BroadService work properly as a singleton, you should make its init private.
To sum up, here's what I'm suggesting:
class BroadService {
static let sharedInstance = BroadService()
var storedVideoLink: String?
private init() {} // to ensure only this class can init itself
func fetchBroadcasts(completion: #escaping ([Games]?) -> ()) {
// your code here
}
}
// somewhere else in your code:
BroadService.sharedInstance.fetchBroadcasts { (games) in
if let games = games {
let game = games[indexPath]
let videoLink = game.videoLink
BroadService.sharedInstance.storedVideoLink = videoLink
}
}
// now you can access it from anywhere as
// BroadService.sharedInstance.storedVideoLink
This way it all stays cohesive in the same class. You can even add a getter method for storedVideoLink so you don't have to access it directly, and in this method you could state that if the string is nil then you fetch the data, store the link to the string, and then return the string.

You could create a file with a struct called something like Global and create a static var and set that inside your completion block once you have fetched the games.
Here is an example.
struct Global {
static var games:[Any]? = nil
static func setGames(games:[Any]) {
Global.games = games
}
}
Then you fetch the data once upon load of the app or somewhere before you use the Global and set that property:
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {
completion(nil)
return
}
do {
let games = try JSONDecoder().decode([Games].self, from: data)
Global.setGames(games: games)
completion(games)
} catch let jsonErr {
print("Error deserializing json:", jsonErr)
completion(nil)
}
}.resume()
Please note that this will make the Global.games accessible from everywhere but it will also not be a constant so you should be careful not to override it.
This way Global.games will be accessible from anywhere.

Related

How do you call an Async Throws function that connects to a remote API with nested JSON properties?

To preface, I am self taught and I know there are tutorials and similar questions on how to do something like this but with the small contextual differences something just isn't clicking. So any help on a solution or a tutorial more closely related to what I am trying to do would be very helpful.
I am trying to access a remote API but am struggling with the call function to execute the Async Throws function. I am trying to get the information from the JSON to be stored in a way that I can access it later for a calculation but have no idea what to put in the Task.
Minimal Reproducible Example-
Code:
// Struct Declarations
struct Response: Decodable {
let data: [StockValues]
}
struct StockValues: Decodable {
let high: Decimal
let low: Decimal
let close: Decimal
let volume: Decimal
}
// Async Throws Function
class dataFetcher {
static func Fetch() async throws -> [StockValues] {
guard let url = URL(string: "http://api.marketstack.com/v1/eod/latest")
else {
throw APIError.invalidServerResponse
}
var request = URLRequest(url: url)
let (data, _) = try await URLSession.shared.data(from: url)
let stockValues = try JSONDecoder().decode(Response.self, from: data)
return stockValues.data
}
}
// Call Async Throws Function
func CallFunction() {
Task {
let fetchedInfo = try await dataFetcher.Fetch()
Response.data = fetchedInfo // Error: Instance member 'data' cannot be used on type 'Response'; did you mean to use a value of this type instead?
}
}
This code assumes that you have a key for the API and that your Response is correct
//Your error refers to
//`Response` is a type it can't hold a value so `Response.data` is unacceptable
struct Response: Decodable {
let data: [StockValues]
}
actor MarketStackService {
//What you are attempting to do is something like this setup
//Something global that anything can access. Using `static` to hold a response is not approriate.
static func fetchLatest() async throws -> [StockValues] {
//Add API key
guard let url = URL(string: "https://api.marketstack.com/v1/eod/latest")
else {
throw APIError.invalidURL
}
let (data, _) = try await URLSession.shared.data(from: url)
let stockValues = try JSONDecoder().decode(Response.self, from: data)
return stockValues.data
}
}
enum APIError: LocalizedError{
case invalidURL
case error(Error)
var errorDescription: String?{
switch self {
case .error(let error):
return error.localizedDescription
default:
let string = String(describing: self)
return NSLocalizedString(string, comment: "AppError")
}
}
}
There are many ways of setting this is up but this is a basic class that works well with SwiftUI. It can hold the value from the API call and trigger changes to the body
#MainActor
class MarketStackViewModel: ObservableObject{
//Variables are lowercased
//A variable can hold a value. That is why you need a `class` or `struct`
#Published var data: [StockValues] = []
#Published var alert: (isPresented: Bool, error: APIError?) = (false, nil)
//`func` are lowercased
func getLatest() async {
do{
let fetchedInfo = try await MarketStackService.fetchLatest()
data = fetchedInfo
}catch{
alert = (true, .error(error))
}
}
}
The View is where the value would be displayed and where the call can be triggered.
//`class` and `struct` are uppercased
struct MarketStackView: View {
#StateObject var vm: MarketStackViewModel = .init()
var body: some View {
VStack{
if vm.data.isEmpty{
Text("Hello, World!")
.task {
//Runs the request as soon as the View displays.
await vm.getLatest()
}
}else{
Text(vm.data.description)
//This is another way of making the call
Button("Get latest") {
Task{
await vm.getLatest()
}
}
}
}.alert(isPresented: $vm.alert.isPresented, error: vm.alert.error) {
Button("Ok") {
vm.alert = (false, nil)
}
}
}
}
struct MarketStackView_Previews: PreviewProvider {
static var previews: some View {
MarketStackView()
}
}
If you paste this code into a .swift file it should work and you can see the flow. I suggest you try the Apple SwiftUI Tutorials. They might help clear some concepts.

The transition from "fetching" to "displaying" JSON API data Swift Node.js

I understand how to "fetch" data from a JSON API (my local server, in fact), but how should I think about the pipeline from merely having the data to displaying it in views? What I intuitively want to do is "return" the data from the fetching function, though I know that's not the paradigm that the Swift URL functions operate with. My thought is that if I can "return" the data (as a struct) it will be easy to pass into a view for visualization.
Sample Code:
This is the structure of the fetched JSON and the kind of variable I want to pass into views.
struct User: Codable {
let userID: String
let userName: String
let firstName: String
let lastName: String
let fullName: String
}
My hope is that the printUser function can return instead of print a successful fetch.
func printUser() {
fetchUser { (res) in
switch res {
case .success(let user):
print(user.userName) // return here?
// I know it won't work, but that's what seems natural
case .failure(let err):
print("Failed to fetch user: ", err)
}
}
}
func fetchUser(completion: #escaping (Result<User, Error>) -> ()) {
let urlString = "http://localhost:4001/user"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, resp, err) in
if let err = err {
completion(.failure(err))
return
}
do {
let user = try JSONDecoder().decode(User.self, from: data!)
completion(.success(user))
} catch let jsonError {
completion(.failure(jsonError))
}
}.resume()
}
Sample view that would take a user struct
struct DisplayUser: View {
var user: User
var body: some View {
Text(user.userID)
Text(user.userName)
Text(user.lastName)
Text(user.firstName)
Text(user.fullName)
}
}
The reason that you can't just "return" is that your fetchUser is asynchronous. That means that it might return relatively instantaneously or it may take a long time (or not finish at all). So, your program needs to be prepared to deal with that eventuality. Sure, it would be "be easy to pass into a view for visualization" as you put it, but unfortunately, it just doesn't fit the reality of the situation.
What you can do (in your example) is set the User as an Optional -- that way, if it hasn't been set, you can display some sort of loading view and if it has been set (ie your async function has returned a value), you can display it. That would look something like this:
class ViewModel : ObservableObject {
#Published var user : User? //could optionally store the entire Result here
func runFetch() {
fetchUser { (res) in
switch res {
case .success(let user):
DispatchQueue.main.async {
self.user = user
}
case .failure(let err):
print("Failed to fetch user: ", err)
//set an error message here? Another #Published variable?
}
}
}
func fetchUser(completion: #escaping (Result<User, Error>) -> ()) {
//...
}
}
struct DisplayUser: View {
#StateObject private var viewModel = ViewModel()
var body: some View {
VStack {
if let user = viewModel.user {
Text(user.userID)
Text(user.userName)
Text(user.lastName)
Text(user.firstName)
Text(user.fullName)
} else {
Text("Loading...")
}
}.onAppear {
viewModel.fetchUser()
}
}
}
Note: I'd probably refactor the async stuff to use Combine if this were my program, but it's a personal preference issue

How to iterate through JSON

I have a method which performs a GET request to an API:
public func getApiData(completion: #escaping () -> (), fullUrl: String)
{
let session = URLSession.shared
let url = URL(string: fullUrl)!
let task = session.dataTask(with: url) { (data, _, _) -> Void in
if let data = data {
self.serializeToJSON(jsonData: data)
completion()
}
}
task.resume()
}
Using SwiftyJSON I then convert the data into JSON:
private func serializeToJSON(jsonData: Data) {
self.json = JSON(data: jsonData)
print(self.json)
for (index,item) in self.json {
print("hi")
}
}
Printing the full JSON gives:
[{"TenantID":1,"Tenant1":"RAC"},{"TenantID":2,"Tenant1":"VictorMillwell"},{"TenantID":3,"Tenant1":"Comfort"},{"TenantID":4,"Tenant1":"Greenlight"}]
However the JSON can't be iterated through as the print("hi") isn't executed, I'm not sure why, I've looked everywhere on the internet to understand why it doesn't iterate and I cant seem to understand why.
Does anyone know why?
There is a good tutorial here, but in the manual it says you can loop like this:
// If json is .Dictionary
for (key,subJson):(String, JSON) in self.json {
// Do something you want
}
// If json is .Array
// The `index` is 0..<json.count's string value
for (index,subJson):(String, JSON) in self.json {
// Do something you want
}
if you don't know if it's a dictionary or array, maybe you can do it like this:
switch self.json.type {
case .array:
for (index,subJson):(String, JSON) in self.json {
// Do something you want
}
case .dictionary:
for (key,subJson):(String, JSON) in self.json {
// Do something you want
}
default:
// Do some error handling
}
It isn't clear why you want to enumerate the JSON. It's trivial to decode this JSON in Swift 4:
struct Tenant:Decodable { let TenantID:Int; let Tenant1:String }
let arr = try! JSONDecoder().decode([Tenant].self, from: data)
Now arr is a Swift array of Tenant, where each Tenant has a TenantID property and a Tenant1 property. And now you can do whatever you like with that array, including cycling through it if you wish.

Swift how to reuse my JSON HTTP Request header

I am making an application which makes a lot of requests from an API. So I don't want to copy and past the code over and over. I was wondering how I can reuse my code in a some more efficient way? Maybe with extensions?
This is my code know:
func apiRequest() {
let config = URLSessionConfiguration.default
let username = "****"
let password = "****"
let loginString = String(format: "%#:%#", username, password)
let userPasswordData = loginString.data(using: String.Encoding.utf8)
let base64EncodedCredential = userPasswordData?.base64EncodedString()
let authString = "Basic " + (base64EncodedCredential)!
print(authString)
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
var running = false
let urlProjects = NSURL(string: "https://start.jamespro.nl/v4/api/json/projects/?limit=10")
let task = session.dataTask(with: urlProjects! as URL) {
( data, response, error) in
if let taskHeader = response as? HTTPURLResponse {
print(taskHeader.statusCode)
}
if error != nil {
print("There is an error!!!")
print(error)
} else {
if let content = data {
do {
let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
print(dictionary)
if let items = dictionary["items"] as? [[String:Any]] {
for item in items {
if let description = item["Description"] as? String {
self.projectNaam.append(description)
}
if let id = item["Id"] as? String {
self.projectId.append(id)
}
if let companyId = item["CompanyId"] as? String {
self.companyId.append(companyId)
}
}
}
self.apiRequestCompani()
}
catch {
print("Error: Could not get any data")
}
}
}
running = false
}
running = true
task.resume()
while running {
print("waiting...")
sleep(1)
}
}
Yes, you can use Extensions to create a BaseViewController and extend that where you want to use your code over and over again. Then you should abstract all dynamic data over input parameters to that method.
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func getApiRequest (Parameters) {
//API Request
}
And then in your view controller you just extend BaseViewController
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Call method in baseviewcontroller
getApiRequest(parameters)
//Call method in self
self.getApiRequest(parameters)
}
override func getApiRequest(Parameters) {
//IF you need to override default configuration
}
So I don't want to copy and past the code over and over.
Absolutely right, no one aiming to get duplicated code; That's the issue of massive view controller. This issue appears since the view controller layer in your application handles most of the responsibilities, such as: getting data from the network, how data should be represented, deliver the formatted data to the view layer, etc...
There are many approaches for solving such an issue (using an appropriate architectural pattern for your application), for simplicity, I would recommend to apply the MVC-N (or MVCNetworking) approach into your app, it is almost the same usual MVC, with a separated files (managers), represent a new layer for handling -for instance- the integration with the external APIs.
Applying the MVN-N should not be that complex, nevertheless it needs to be described well (which might be too abroad to be descried in the answer), I would suggest to check the above mentioned apple example, also watching this video should be useful.

return value from json session to use outside of session

I've searched for how to do this but something just isn't making sense for me and I can't do it. All I need to do is get data out of my json session (if thats what you call it). I started programming about 3 weeks ago so I need lay mans terms please. I realize this is probably going to get marked as a duplicate but most of the answers on this / related topics are for other languages and I barely understand swift so they don't help me much.
I've spent hours trying to find the answer and since I'm new I don't know if what I'm searching for is even the right thing to be searching for. I've also tried reading the iOS developer library but either I don't understand what it's telling me or I haven't found the right section because I still can't figure this out. Please try to explain this instead of sending me to read other resources.
here is my function
func parseData() {
let urlString = "http://heroesjson.com/heroes.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: urlString)!
session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
guard let responseData = data else { return }
var json: [[String: AnyObject]]!
do {
json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! [[String: AnyObject]]
}
catch {
//handle error
}
var arrayToReturn = [Hero]()
for element in json {
let hero = Hero(fromDictionary: element as! [String: AnyObject])
arrayToReturn.append(hero)
}
}.resume()//Closes Session.dataTaskWithURL
} //Closes parseData()
the goal is to get the json variable in my do statement so I can parse it outside of the function or get my "arrayToReturn" so I can save it to a global variable that I use.
If I understand correctly I can't just assign the value (arrayToReturn) to my global variable (heroes) because this is an asynchronous request so it just returns nil because the command is called before the request is finished. I think I have to use a completion handler or callback function. I don't really understand the difference between them and don't understand how or where to implement them.
Also, I don't understand this code very much either, I just know it's necessary to get what I want.
session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
"(data: NSData?, response:NSURLResponse?, error: NSError?)" looks like parameters but they don't seem to be attached to a function so that doesn't make sense to me
"-> Void" Doesn't make sense to me because -> means return whatever follows, but void indicates to me that its returning nothing, so why not just leave it out all together?
"-> Void in" What what is the significance of in here? what does it mean / signal?
Go and read about Swift Closures. To use a value outside of it, you'd need to pass it to another closure.
func parseData(callback: (heroes: [Hero]) -> Void) {
let urlString = "http://heroesjson.com/heroes.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: urlString)!
session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
guard let responseData = data else { return }
var json: [[String: AnyObject]]!
do {
json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! [[String: AnyObject]]
}
catch {
//handle error
}
var arrayToReturn = [Hero]()
for element in json {
let hero = Hero(fromDictionary: element as! [String: AnyObject])
arrayToReturn.append(hero)
}
callback(heroes: arrayToReturn)
}.resume()//Closes Session.dataTaskWithURL
} //Closes parseData()
And you'd call it:
parseData { heroes in
// do something with the array
}