0

This is My Code:

  urll = NSURL(string: "http://xxxxxxxxxxx.com/api/?slider=uij6sdnb")
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(urll) {(NSData, response, error) -> Void in
        do {
            let records = try NSJSONSerialization.JSONObjectWithData(NSData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
            for record in records {
                //                    let urlid = Int(record["slide_id"] as! String)
                let urimage = record["slide_url"] as! String
                self.urls = [urimage]
                print(self.urls.count)
                                }
        }

        catch {
            print("Json Error")

        }
    }

    task.resume()

When I Print :

              print(urimage)

it gaves me 4 url like this:

http://sdkladlkasjd1.jpg

http://sdkladlkasjd2.jpg

http://sdkladlkasjd3.jpg

http://sdkladlkasjd4.jpg

When I print:

              print(urimage[1])

It gaves me : 'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion

When i put it in another value :

var urls = [String]()
self.urls = [urimage]

and I print:

                print(self.urls.count)

it gaves me

1

1

1

1

How on earch I can access one of this urls !?

I want to Show them on imageview but I can !

2
  • 1
    You get multiple URLs because of your loop over records. Each record has a slide_url which you are saving in urimage and print it out. It is unclear which one you want, but if the first one is okay, why not just replace your loop with something like let urimagae = records[0]["slide_url"] as! String? Commented Jul 24, 2016 at 9:54
  • Thank you ! I want All of them seperatly ! How can I get Them All !? Commented Jul 24, 2016 at 10:06

1 Answer 1

1

As Julian Kniephoff rightly mentioned, you are printing each URL in the for loop, thus you cannot access one particular one. However, there is also another issue, in that you are replacing the urls array with the latest url each time.

To solve this, simply replace the line self.urls = [urimage] with self.urls.append(urimage).

You can then access a particular image outside the for loop by doing something like self.urls[1].

This is also why printing the count of the array returns 1, since each time around you are setting the array to just the one latest element in the loop.

In the end, your code may look something like this

url = NSURL(string: "http://xxxxxxxxxxx.com/api/?slider=uij6sdnb")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) {(data, response, error) -> Void in
    do {
        let records = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
        for record in records {
            //let urlid = Int(record["slide_id"] as! String)
            let urimage = record["slide_url"] as! String
            self.urls.append(urimage)
        }
        print(self.urls[1]) //Prints http://sdkladlkasjd2.jpg
    }
    catch {
        print("Json Error")
    }
    //print(self.urls[1])
}

task.resume()
Sign up to request clarification or add additional context in comments.

8 Comments

@Alfi can you do print(self.urls) and tell me what it says?
@Alfi are you sure that you're printing outside of the for loop?
@Alfi try copying my code exactly, and tell me whether it gives you an error
@Alfi replace data! with what you had before
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.