There are a few mistakes in your code, mostly about initialization and optionals - this is a compilable version:
if let url = NSURL(string: "https://portal.pfs-ltd.org/SyncCharityData") {
let request = NSURLRequest(URL: url)
if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {
var response = NSData(data: data)
var jsonParsingError: NSError?
if let res = NSJSONSerialization.JSONObjectWithData(response, options: nil, error:&jsonParsingError) as? NSArray {
var jsonResponse = NSMutableArray(array: res)
}
}
}
The name: Type
syntax is used to declare a variable type, but that doesn't initialize it. You should use the assignment operator =
like you do in objective C - but since swift has type inference, you don't need to specify a variable type if it can be inferred by the value you are initializing it with.
NSURL
, NSURLRequest
and NSMutableArray
require non optional data in their respective initializer, so I have used optional binding to feed them with non optional data.
Also, in the above cases you have missed to specify the external parameter name (respectively URL
, data
and array
, like in NSMutableArray(array: res)
.
Personal suggestion: I recommend you to read the Language Guide. I think it's not useful for you to develop in a language without understanding some basic concepts - although the swift syntax is similar to other development languages, it has unusual features like optionals, type inference etc. which you need to fully understand in order to write code that compiles first and then does what you expect to do.