-2

hi i am trying to get the son response from the server and the following is the response i get from server while using postman

{
  "stories": [
{
  "image_url": "http://storybox.dk/wp-content/uploads/2016/11/Hansa.jpg",
  "name": "New story",
  "story_key": "ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgIC8oYIKDA"
},
{
  "image_url": "http://storybox.dk/wp-content/uploads/2016/11/Hansa.jpg",
  "name": "Story of Foo the fox.",
  "story_key": "ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA"
}]
}

but when i try to get the values inside the stories tag as array it gets only one value as
self.StoryListArr = (reponsedict.value(forKey: "stories") as? NSArray)!

 (
   {
    "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png";
    name = "Story of Foo";
    "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA;
   }
)

please advice how to deal this

when i try to print response dict it shows only one value in it

{
stories =     (
            {
        "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png";
        name = "Story of Foo";
        "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA;
    }
);
}
Mohanraj
  • 587
  • 4
  • 21

1 Answers1

1

There is no reason to use NSArray in Swift. Simply cast the object to [Dictionary].

if let stories = responsedict["stories"] as? [Dictionary<String,String>] {
    print(stories) 
}
Frederik
  • 384
  • 3
  • 7
  • when i try to print the response dict it shows only one value in it `print(reponsedict) { stories = ( { "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png"; name = "Story of Foo"; "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA; } ); }` – Mohanraj Mar 22 '17 at 06:58
  • Try printing responsedict["stories"] to make sure that you have more than one value in the response in swift – Frederik Mar 22 '17 at 07:00
  • Is the request in postman the exact same as the one in your code? – Frederik Mar 22 '17 at 07:01
  • responsedict["stories"] prints `( { "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png"; name = "Story of Foo"; "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA; } )` contains only one value – Mohanraj Mar 22 '17 at 07:26
  • Are you sure that the request is the exact same as the one you did in Postman? – Frederik Mar 22 '17 at 07:29
  • sorry its my mistake with the url. thanks for your help – Mohanraj Mar 22 '17 at 08:28
  • Sure, glad you found the cause – Frederik Mar 22 '17 at 08:38
  • i am creating a story app with list of stories in home screen and details page on selecting the story i have array of stories on clicking the next story i want to load the contents of the next story with some animation like flipping the page suggest any ideas in swift 3 – Mohanraj Mar 22 '17 at 08:45
  • There are lot of ways to animate that. You can look at custom segue animations or just the `UIView.animate` function – Frederik Mar 22 '17 at 09:01