Read and write a String from text file

前端 未结 21 1469
别跟我提以往
别跟我提以往 2020-11-22 00:02

I need to read and write data to/from a text file, but I haven\'t been able to figure out how.

I found this sample code in the Swift\'s iBook, but I still don\'t kno

21条回答
  •  星月不相逢
    2020-11-22 00:46

    Latest swift3 code
    You can read data from text file just use bellow code This my text file

         {
    "NumberOfSlices": "8",
    "NrScenes": "5",
    "Scenes": [{
               "dataType": "label1",
               "image":"http://is3.mzstatic.com/image/thumb/Purple19/v4/6e/81/31/6e8131cf-2092-3cd3-534c-28e129897ca9/mzl.syvaewyp.png/53x53bb-85.png",
    
               "value": "Hello",
               "color": "(UIColor.red)"
               }, {
               "dataType": "label2",
               "image":"http://is1.mzstatic.com/image/thumb/Purple71/v4/6c/4c/c1/6c4cc1bc-8f94-7b13-f3aa-84c41443caf3/mzl.hcqvmrix.png/53x53bb-85.png",
               "value": "Hi There",
               "color": "(UIColor.blue)"
               }, {
               "dataType": "label3",
               "image":"http://is1.mzstatic.com/image/thumb/Purple71/v4/6c/4c/c1/6c4cc1bc-8f94-7b13-f3aa-84c41443caf3/mzl.hcqvmrix.png/53x53bb-85.png",
    
               "value": "hi how r u ",
               "color": "(UIColor.green)"
               }, {
               "dataType": "label4",
               "image":"http://is1.mzstatic.com/image/thumb/Purple71/v4/6c/4c/c1/6c4cc1bc-8f94-7b13-f3aa-84c41443caf3/mzl.hcqvmrix.png/53x53bb-85.png",
               "value": "what are u doing  ",
               "color": "(UIColor.purple)"
               }, {
               "dataType": "label5",
              "image":"http://is1.mzstatic.com/image/thumb/Purple71/v4/6c/4c/c1/6c4cc1bc-8f94-7b13-f3aa-84c41443caf3/mzl.hcqvmrix.png/53x53bb-85.png",
               "value": "how many times ",
               "color": "(UIColor.white)"
               }, {
               "dataType": "label6",
               "image":"http://is1.mzstatic.com/image/thumb/Purple71/v4/5a/f3/06/5af306b0-7cac-1808-f440-bab7a0d18ec0/mzl.towjvmpm.png/53x53bb-85.png",
               "value": "hi how r u ",
               "color": "(UIColor.blue)"
               }, {
               "dataType": "label7",
               "image":"http://is5.mzstatic.com/image/thumb/Purple71/v4/a8/dc/eb/a8dceb29-6daf-ca0f-d037-df9f34cdc476/mzl.ukhhsxik.png/53x53bb-85.png",
               "value": "hi how r u ",
               "color": "(UIColor.gry)"
               }, {
               "dataType": "label8",
               "image":"http://is2.mzstatic.com/image/thumb/Purple71/v4/15/23/e0/1523e03c-fff2-291e-80a7-73f35d45c7e5/mzl.zejcvahm.png/53x53bb-85.png",
               "value": "hi how r u ",
               "color": "(UIColor.brown)"
               }]
    

    }

    You can use this code you get data from text json file in swift3

         let filePath = Bundle.main.path(forResource: "nameoftheyourjsonTextfile", ofType: "json")
    
    
        let contentData = FileManager.default.contents(atPath: filePath!)
        let content = NSString(data: contentData!, encoding: String.Encoding.utf8.rawValue) as? String
    
        print(content)
        let json = try! JSONSerialization.jsonObject(with: contentData!) as! NSDictionary
        print(json)
        let app = json.object(forKey: "Scenes") as! NSArray!
        let _ : NSDictionary
        for dict in app! {
            let colorNam = (dict as AnyObject).object(forKey: "color") as! String
            print("colors are \(colorNam)")
    
           // let colour = UIColor(hexString: colorNam) {
           // colorsArray.append(colour.cgColor)
           // colorsArray.append(colorNam  as! UIColor)
    
            let value = (dict as AnyObject).object(forKey: "value") as! String
            print("the values are \(value)")
            valuesArray.append(value)
    
            let images = (dict as AnyObject).object(forKey: "image") as! String
            let url = URL(string: images as String)
            let data = try? Data(contentsOf: url!)
            print(data)
            let image1 = UIImage(data: data!)! as UIImage
            imagesArray.append(image1)
             print(image1)
                }
    

提交回复
热议问题