[ad_1]
I am attempting to show my quotes in a json file on the apple watch however after I run my code is ready to construct efficiently however nothing is showing on the watch simulator. Is there one thing that I am doing mistaken that’s stopping me from seeing the main points from my json file on the simulator?
Listed here are the information I am utilizing. This codecs my codable quote elments
struct WelcomeElement: Codable {
var id: String
var textual content: String
var writer: String?
}
typealias Welcome = [WelcomeElement]
Right here is my class the place I decode my json file.
import WatchKit
//public class GetData
class NetworkService
{
//@Printed var userData = [Welcome]()
public static var sharedobj = NetworkService()
let fileLocation = Bundle.fundamental.url(forResource: "quotesw", withExtension: "json")!
non-public let session = URLSession(configuration: .default)
init()
{
//load(onSuccess: (Welcome) -> Void)
}
func load(onSuccess: @escaping(Welcome) -> Void)
{
// let fileLocation = Bundle.fundamental.url(forResource: "quotesw", withExtension: "json")!
let process = session.dataTask(with: fileLocation) { (information, response, error) in
DispatchQueue.fundamental.async {
do
{
let information = strive Knowledge(contentsOf: self.fileLocation)
let jsonDecode = strive JSONSerialization.jsonObject(with: information, choices: .mutableContainers)
print("(jsonDecode)")
}
catch
{
print(error.localizedDescription)
}
}
}
func kind()
{
//self.userData = self.userData.sorted(by: { $0.writer < $1.writer})
}
process.resume()
}
}
Right here is my interface the place I name within the information to the controller
import Basis
class InterfaceController: WKInterfaceController {
var quotes = Welcome()
//var quotes = NetworkService()
@IBOutlet weak var labelQuote: WKInterfaceLabel!
@IBOutlet weak var quoteauthorlbl: WKInterfaceLabel!
override func awake(withContext context: Any?) {
}
override func willActivate() {
//let information = GetData().$userData
NetworkService.sharedobj
.load() {(w) in
self.quotes = w
if let randomquote = self.quotes.randomElement()
{
self.labelQuote.setText("(randomquote.textual content)")
self.quoteauthorlbl.setText("By (randomquote.writer!)")
}
}
}
override func didDeactivate() {
// This technique known as when watch view controller is not seen
}
}
[ad_2]
