Last active
August 29, 2015 14:02
-
-
Save below/9c280bd9be1ab35c1053 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var str = "https://github.com/robelkin/altconf-data/raw/master/sessions.json" | |
let url = NSURL(string:str) | |
var error : NSErrorPointer = nil | |
if (url != nil) { | |
let jsonData = NSData(contentsOfURL: url!, options: .Data, error: error) | |
if jsonData?.length != 0 { | |
let array : NSArray! = NSJSONSerialization.JSONObjectWithData (jsonData!, | |
options: .AllowFragments, error: error) as NSArray | |
let desc = array.description | |
for item in array { | |
println("item \(item)") | |
} | |
} | |
} |
Some things are not correct:
- Optionals cannot be evaluated as true/false because they could conflict with
Bool!
. - Arrays have to be declared as
[T]
- You should not use
NSErrorPointer
. Usevar error:NSError?
and&error
instead.
Anyway, it seems not to be working. I get an Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7fb103c167c0 {NSURL=https://github.com/robelkin/altconf-data/raw/master/sessions.json}
. Maybe the sandbox preventing networking?
This works for me in an Swift-only iOS-App:
let str = "http://github.com/robelkin/altconf-data/raw/master/sessions.json"
let url = NSURL.URLWithString(str)
var error:NSError?
let jsonData = NSData.dataWithContentsOfURL(url, options: .DataReadingUncached, error: &error)
if error != nil {
println(error!.debugDescription!)
}
if jsonData != nil {
println("JSON: \(NSString(data:jsonData, encoding:NSUTF8StringEncoding))")
var error:NSError?
let array = NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers, error: &error) as? [NSDictionary]
if error != nil {
println(error!.debugDescription!)
}
if array != nil {
for dictionary in array! {
let allKeys = dictionary.allKeys as [String]
for key in allKeys {
println("\(key) = \(dictionary.objectForKey(key))")
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have updated the source …