Skip to content

Instantly share code, notes, and snippets.

@alien3d
Created January 4, 2018 11:48
Show Gist options
  • Save alien3d/b19d75faa35f07407bd8c8bd1a87048b to your computer and use it in GitHub Desktop.
Save alien3d/b19d75faa35f07407bd8c8bd1a87048b to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// todoSwift4
//
// Created by hafizan on 04/01/2018.
// Copyright © 2018 hafizan. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelText: UILabel!
@IBOutlet weak var labelSecondText: UILabel!
@IBOutlet weak var labelThreeText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
labelText.text="sifoo";
// testing array
let Marvel : [String] = ["Captain Marvel","Captain Blue","Captain America"];
labelText.text = Marvel[1];
labelText.center = self.view.center;
labelText.text = getMarvelAsgardian()[1];
labelSecondText.text = getMarvelSpidy()[2];
labelThreeText.text = getMarvelNewAvengers();
}
func getMarvelAsgardian() -> [String] {
let Asgardian : [String] = ["Loki","Thor","Sit"];
return Asgardian;
}
/**
* this sample using dictionary
*/
func getMarvelSpidy()->[Int:String]{
var SpidyList = [Int:String]();
SpidyList[0]="Spiderman";
SpidyList[1]="Morales";
SpidyList[2]="spidy-gwen";
return SpidyList;
}
/**
* this is to test either json availble in the parse
*/
func getMarvelNewAvengers () -> String {
var test : String? = nil;
let newAvenger:String = """
{
"success":true,
"message":"idiot",
"yess":"She Hulk"
}
""";
let data = newAvenger.data(using:.utf8)!
// there is limitation for object
// we just need to knew success false or true only.
// so we need to create two struct .. messy yess
struct Info:Decodable {
let success:Bool;
let message:String;
}
struct InfoReal:Decodable{
let success:Bool;
let message:String;
let yess:String;
}
do {
let decoder = JSONDecoder();
let infox = try decoder.decode(Info.self,from:data);
if(infox.success == true){
// now we parse new struct data
do {
let decoderSecond = JSONDecoder();
let infoxReal = try decoderSecond.decode(InfoReal.self, from:data);
test = infoxReal.yess;
}catch let error{
print(error);
}
}else{
test="Korean Hulk Cho";
}
}catch let error{
print(error);
}
return test!;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/**
* this on purpose to render void string
*/
func voidtestArrayOne() -> Void {
_ = "will be as it";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment