Skip to content

Instantly share code, notes, and snippets.

@jaisontj
jaisontj / JSONStringify.swift
Last active September 20, 2018 10:08
JSONStringify with mods for Swift 2.0
import Foundation
let jsonObject: [AnyObject] = [
["name": "John", "age": 21],
["name": "Bob", "age": 35],
]
func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> String{
let options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)
@jaisontj
jaisontj / JSONParserArray.swift
Last active August 29, 2015 14:23
JSONParser with mods for Swift 2.0
import Foundation
let string = "[ {\"name\": \"John\", \"age\": 21}, {\"name\": \"Bob\", \"age\": 35} ]"
func JSONParseArray(string: String) -> [AnyObject]{
if let data = string.dataUsingEncoding(NSUTF8StringEncoding){
do{
if let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? [AnyObject] {
@jaisontj
jaisontj / JSONParseDictionary.swift
Created June 29, 2015 07:22
JSONParser with mods for Swift 2.0
import Foundation
let string = "{\"name\": \"John\", \"age\": 35, \"children\": [\"Jack\", \"Jill\"]}"
func JSONParseDictionary(string: String) -> [String: AnyObject]{
if let data = string.dataUsingEncoding(NSUTF8StringEncoding){
@jaisontj
jaisontj / HTTPGet.swift
Last active October 14, 2015 03:47
HTTPGet from Google with mods for swift 2.0
import Foundation
func HTTPsendRequest(request: NSMutableURLRequest,callback: (String, String?) -> Void) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler :
{
data, response, error in
if error != nil {
callback("", (error!.localizedDescription) as String)
} else {
@jaisontj
jaisontj / HTTPGetJSON.swift
Created June 29, 2015 09:44
HTTPGetJSON with mods for swift 2.0
import Foundation
func JSONParseDict(jsonString:String) -> Dictionary<String, AnyObject> {
if let data: NSData = jsonString.dataUsingEncoding(
NSUTF8StringEncoding){
do{
if let jsonObj = try NSJSONSerialization.JSONObjectWithData(
data,
@jaisontj
jaisontj / MyJSONStringify.swift
Last active August 29, 2015 14:23
Groundup JSON Stringify in swift
func jsonStringify(jsonObject: AnyObject) -> String {
var jsonString: String = ""
switch jsonObject {
case _ as [String: AnyObject] :
let tempObject: [String: AnyObject] = jsonObject as! [String: AnyObject]
jsonString += "{"
@jaisontj
jaisontj / SimpleProgressView
Created September 1, 2015 04:50
Progress View
//Mark:- Fake functions to show a loading bar when loading webView
func startedLoadingWebView() {
self.progressView.progress = 0.0
self.finishedLoadingPage = false
//0.01667 is approximately 1/60 to lead at 60fps
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01667, target: self, selector: "timerCallback", userInfo: nil, repeats: true)
}
func timerCallback() {
if self.finishedLoadingPage {
if self.progressView.progress >= 1 {
/*For an input of 1, 2, 3, 4, 7, 6, 5, 9, 2, 1, 4, 11, 3
Should print out -> (1,1),(2,2),(3,3),(4,4),(6,7),(9,11)*/
import java.util.Collections;
public class PatternExtractor {
List<Integer> array = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 7, 6, 5, 9, 2, 1, 4, 11, 3));
public static void main(String[] args) {
for (int i = 0; i < array.size(); i++) {
int value1 = array.get(i);
public class JSONStringValidator {
public static void main(String[] args) {
String jsonString = "{\"name\": \"John\",\"age\":\"23\"}";
System.out.println("JSON: " + jsonString);
System.out.println("Validity: " + isJsonValid(jsonString));
}
public static boolean isJsonValid(String jsonString) {
//Removing whitespaces
jsonString = jsonString.replaceAll("\\s+","");
import UIKit
class AuthenticationViewController: UIViewController {
@IBOutlet weak var username: UITextField!
@IBOutlet weak var password: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}