이미지 자동 스크롤 라이브러리, JSON stringify 함수

2018. 1. 30. 23:55아이폰 개발


Auk, an image slideshow for iOS / Swift

https://github.com/evgenyneu/Auk

// Show remote images
scrollView.auk.show(url: "https://bit.ly/auk_image")
scrollView.auk.show(url: "https://bit.ly/moa_image")

// Show local image
if let image = UIImage(named: "bird.jpg") {
  scrollView.auk.show(image: image)
}


// Scroll images automatically with the interval of 3 seconds
scrollView.auk.startAutoScroll(delaySeconds: 3)


Array or Dictionary Object to JSON String

/**
 JSON Object(NSArray, NSDictionary) -> String
 
 ```
 let inputJsonDictionary = ["first": true, "second": false]
 let resultDictionaryString = JSONStringify(value: inputJsonDictionary as AnyObject)
 print(resultDictionaryString)
 // {"second":false,"first":true}
 
 let inputJsonArray = [["first": true], ["second": false]]
 let resultArrayString = JSONStringify(value: inputJsonArray as AnyObject)
 print(resultArrayString)
 // [{"first":true},{"second":false}]
 ```
 
 - Parameter value: 변환할 Object(NSArray, NSDictionary)
 - Parameter prettyPrinted: 이쁘게 표현할지 여부 (default = false)
 - Returns : JSON 문자열
 */
func JSONStringify(value: AnyObject, prettyPrinted: Bool = false) -> String {
    
    let options = prettyPrinted ? JSONSerialization.WritingOptions.prettyPrinted : []
    
    if JSONSerialization.isValidJSONObject(value) {
        do {
            let data = try JSONSerialization.data(withJSONObject:value, options:options)
            if let jsonString = String(data: data, encoding: String.Encoding.utf8) {
                return jsonString
            }
        } catch {
            print("JSON serialization failed:  \(error)")
        }
    }
    return ""
}


http://kka7.tistory.com/33