0

I used the Haneke Framework to get Data from a Site. With the Haneke Framework i also can get Images from a site, and these Images i can present on a UIImageView. Now i wanted to get some text from a site.

I did it like this:

 cache.fetch(URL: URL).onSuccess { JSON in
            println(JSON.dictionary?["index"])

It printed me all the data from "Index".

Now i want, that all the data from "Index" should be presented on a UITextView.

  self.textView.text = JSON.dictionary["index"]

But it doesn't work. I get the error:

Cannot assign a value of type 'AnyObject' to a value of type 'String!'

I have to unwrap it or?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
iOS Koray
  • 41
  • 1
  • 9

3 Answers3

1

Finally, this prints out all records of the JSON text. The structure is an array of dictionaries. The text is very simply formatted (two tab characters between key and value).

  cache.fetch(URL: url).onSuccess { JSON in
    if let index = JSON.dictionary?["index"] as? [Dictionary<String,String>] {
      var resultString = ""
      for anItem in index {
        for (key, value) in anItem {
          resultString += "\(key)\t\t\(value)\n"
        }
        resultString += "\n\n"
      }
      self.textView.text = resultString
    }
  }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

the compiler doesn't know the type of the dictionary item. If you know it's always a String make a forced downcast from AnyObject to String

self.textView.text = JSON.dictionary["index"] as! String
vadian
  • 274,689
  • 30
  • 353
  • 361
  • But now the app freezes, after i try to run it. I get following message: Could not cast value of type '__NSCFArray' (0x110306650) to 'NSString' (0x1107968e0). – iOS Koray Jun 28 '15 at 14:45
  • then the object is an array not a string. What kind of array is it? – vadian Jun 28 '15 at 14:48
  • Its actually a String.. Hm, let cache = Cache(name: "test") let URL = NSURL(string: "myurl")! – iOS Koray Jun 28 '15 at 14:55
  • the compiler complains that the object returned from `JSON.dictionary["index"]` is an NSArray object and the compiler is right ;-) – vadian Jun 28 '15 at 14:57
  • :D so what should i do? Do i have to unwrapp it? – iOS Koray Jun 28 '15 at 14:58
  • that depends how many objects the array contains. If it's only one write `self.textView.text = (JSON.dictionary["index"] as! [String]).first!`. But with this syntax the app will crash if the array is empty – vadian Jun 28 '15 at 15:01
0

as we've revealed that JSON.dictionary["index"] is an Array, this is a safe syntax assuming that the array contains only one item

if let indexTextArray = JSON.dictionary?["index"] as? [String] {
  if !indexTextArray.isEmpty {
      self.textView.text = indexTextArray.first!
  }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Okay the App works now :) But it doesn't show me anything in my UITextView, is it because, the array only contains one item? – iOS Koray Jun 28 '15 at 15:13
  • `println()` the object or set a breakpoint to figure out what kind of array it is – vadian Jun 28 '15 at 15:16
  • I did println(index) and i got a lot of "Array of Strings" – iOS Koray Jun 28 '15 at 15:25
  • then you could flatten the array by inserting line breaks `if let indexTextArray = JSON.dictionary?["index"] as? [String] { self.textView.text = "\n".join(indexTextArray) }` – vadian Jun 28 '15 at 15:32
  • Okay i can run the app now, but my TextView isn't changing.. Hmm – iOS Koray Jun 28 '15 at 19:07
  • do you get a result by `println()` the `"\n".join(indexTextArray)` expression? – vadian Jun 28 '15 at 19:15
  • If i do it like this: cache.fetch(URL: URL).onSuccess { JSON in println(JSON.dictionary?["index"]) i get lot of results – iOS Koray Jun 28 '15 at 19:20
  • sorry, you have to find the proper type of the object and its contents. Look at the printout: `[value, value]` is Array, `[key:value, key:value]` is dictionary – vadian Jun 28 '15 at 19:31
  • So how should i do it? – iOS Koray Jun 28 '15 at 19:37
  • read the printout as mentioned, log values, set breakpoints, cast to something "impossible" to read the compiler error messages – vadian Jun 28 '15 at 19:40
  • What do you mean with printout [value, value] ? Why is it actually so hard, to just change the text of the TextView to something on a Json Site? I just want, that the textview should show everything that is inside of "index".. :/ – iOS Koray Jun 28 '15 at 19:46
  • the text view needs explicit plain text (String), you have to convert the returned JSON collection type (obviously Array) into String – vadian Jun 28 '15 at 19:49
  • Whats the easiest way to convert a array into a string? – iOS Koray Jun 28 '15 at 19:51
  • But didn't we actually converted it already with: if let indexTextArray = JSON.dictionary?["index"] as? [String] { self.textView.text = "\n".join(indexTextArray) println(["index"]) } ? – iOS Koray Jun 28 '15 at 20:05
  • yes, we did, there must be still another issue. Please use the options I gave you to identify the proper types. There could be other nested arrays or dictionaries which causes the conversion to fail – vadian Jun 28 '15 at 20:09
  • Okay but i don't know what you mean... How can check the types of "index"? I already did it with println() and it printed me a lot of "String Arrays" If you want, i can give you the URL, that i want to parse... – iOS Koray Jun 28 '15 at 20:12
  • Only if you have time, here is the URL: http://www.berlin.de/sen/wirtschaft/service/maerkte-feste/strassen-volksfeste/index.php/index/all.json?q= – iOS Koray Jun 29 '15 at 16:51
  • oh, deutsch, gut. :-) Das ist ein Haufen Zeugs. Bist du per email erreichbar? (Oh, german, good. That's a bunch of stuff) – vadian Jun 29 '15 at 17:04
  • You are also german :D ? I also can speak in english :) btw: It works now! Thank you for your help !! :D – iOS Koray Jun 29 '15 at 17:57
  • Yep Thanks! Works now perfectly :) – iOS Koray Jun 29 '15 at 18:01
  • Hey i got another question: With SwiftyJson i can perfectly parse the context of "Index" like this: func ParseJSON(json: JSON) { for result in json["index"].arrayValue { let title = result["badname"].stringValue let body = result["profil"].stringValue let sigs = result["id"].stringValue let obj = ["title": title, "body": body, "sigs": sigs] objects.append(obj) } } Is there also a way to do this with Haneke? – iOS Koray Jul 02 '15 at 17:17