-2

I m having errors in swift project like consecutive statement need to be separated by ;

var request: NSURLRequest(NSURL(String: "https://portal.pfs-ltd.org/SyncCharityData"))
    var response: NSData(NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil))
    var jsonParsingError: NSError?
    var jsonResponse: NSMutableArray(NSJSONSerialization.JSONObjectWithData(response, options:0, error:&jsonParsingError))

somebody please help me why i m having this error, today is my first practical day at SWIFT, i have been doing Objective- c until now

Muhammad Waqas
  • 904
  • 2
  • 10
  • 21

2 Answers2

1

Try the below snippet

var error : NSError?
var responseCode : NSURLResponse? = nil;
var url  = NSURL(string: "https://portal.pfs-ltd.org/SyncCharityData")
var request = NSURLRequest(URL: url!)
var response = NSURLConnection.sendSynchronousRequest(request, returningResponse: &responseCode,  error: &error) as NSData?
println("response \(response)")

if let responseData = response {
    var jsonResponse = NSJSONSerialization.JSONObjectWithData(responseData, options: nil, error: &error) as [AnyObject]!
    println("*** \(jsonResponse)")
}
Suresh Kumar Durairaj
  • 2,104
  • 16
  • 24
  • thank u so much suresh, it did solve my problem, but i have one thing to ask where u used [AnyObject] why did u use '!' as i did not got any error when i didn't coded ! at the end of line – Muhammad Waqas Dec 31 '14 at 11:15
  • It's a vast thing to explain in comment. Please refer answer http://stackoverflow.com/questions/26053406/swift-difference-in-and-in-swift/26054288#26054288 – Suresh Kumar Durairaj Dec 31 '14 at 11:21
  • Note that using the forced unwrapping operator `!` is to be avoided, and optional binding should be preferred, unless you are 100% sure the optional variable contains a non nil value, or you want the app to crash if the variable contains a nil value (in some cases that can be useful for debugging) – Antonio Dec 31 '14 at 12:57
1

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.

Antonio
  • 71,651
  • 11
  • 148
  • 165