如何使用随机字符串让或var到url链接

时间:2016-09-23 07:31:24

标签: swift random ios10 xcode8

如何使用随机字符串让或var到url链接 我想为url制作随机字符串 let url = URL(字符串:" https://www.pallive.net/random.json")

或者当我在应用程序中更改网站linke中的值时看到代码没有更改,但如果我更改了网址的名称,则更改 如果我更改json中的值并保留相同的文件,则代码不会重新加载 如果我想重新加载我必须更改文件的名称如何做一些尝试 auotmatic更改URL并保留ftp服务器中的原始

import Foundation


class Episode
{
    var title: String?
    var description: String?
    var thumbnailURL: URL?


    var url: URL?
    var episodes = [Episode]()

    init(title: String, description: String, thumbnailURL: URL, createdAt: String, author: String)
    {
        self.title = title
        self.description = description
        self.thumbnailURL = thumbnailURL


    }

    init(espDictionary: [String : AnyObject])
    {
        self.title = espDictionary["title"] as? String

       // description = espDictionary["description"] as? String
        thumbnailURL = URL(string: espDictionary["thumbnailURL"] as! String)


        self.url = URL(string: espDictionary["link"] as! String)
    }

    static func downloadAllEpisodes(completion: @escaping ([Episode]) -> ()) {
        var episodes = [Episode]()

        let url = URL(string:"https://www.pallive.net/random.json")


        URLSession.shared.dataTask(with: url!) { (data, response, error) in

            if error != nil {
                print(error)
                completion(episodes)
            }
            else {
                if let jsonData = data ,let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {

                    let espDictionaries = jsonDictionary["episodes"] as! [[String : AnyObject]]
                    for espDictionary in espDictionaries {

                        let newEpisode = Episode(espDictionary: espDictionary)
                        episodes.append(newEpisode)


                    }
                }
               completion(episodes)

                DispatchQueue.main.async(execute: {
                    completion(episodes)
                })


            }

            }.resume()
    }

    func randomString(_ length: Int) -> String {

        let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        let len = UInt32(letters.length)

        var randomString = ""

        for _ in 0 ..< length {
            let rand = arc4random_uniform(len)
            var nextChar = letters.character(at: Int(rand))
            randomString += NSString(characters: &nextChar, length: 1) as String
        }

        return randomString

    }


}

0 个答案:

没有答案