Wednesday, 21 February 2018

Get the document path and random document name generate - Swift

Get the document path and random document name generate - Swift

func get_document_path(str_doc_name: String) -> URL {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documents: String = paths[0]
    let docPath:URL = URL(fileURLWithPath: documents).appendingPathComponent(str_doc_name)
    return docPath
}

func generate_doc_name(extension_str: String) -> String {
    let aRandomInt = randomNumber()
    let dateformate = DateFormatter()
    dateformate.dateFormat = "ddMMyyyyHHmmss"
    let date: String = dateformate.string(from: Date())
    let trans_id = "doc-\(date)\(aRandomInt).\(extension_str)"
    return trans_id
}

func randomNumber(range: ClosedRange<Int> = 1000...9999) -> Int {
    let min = range.lowerBound
    let max = range.upperBound
    return Int(arc4random_uniform(UInt32(1 + max - min))) + min
}

No comments: