Hi ,
If you are starting with swift and want to learn following things:
1) how to declare IBOutlet
2) how to declare property
3) How to declare class variable
4) how to hit http server request
5) how to parse the JSON
Then go through the following UIviewController code and you can find your first iOS app launch in swift.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var artistName: UILabel?
@IBOutlet weak var logintextfield: UITextField?
@IBOutlet weak var artistGenre: UILabel?
@IBOutlet weak var searchbtt: UIButton?
var responseStringparse: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func loginbuttonpressed(sender: UIButton) {
//print("checking login text \(logintextfield?.text)")
let request = NSMutableURLRequest(URL: NSURL(string: "https://itunes.apple.com/lookup?id=909253")!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
self.titlesFromJSON(data)
}
}
func titlesFromJSON(data: NSData) {
var jsonError: NSError?
let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as! NSDictionary
if let unwrappedError = jsonError {
println("json error: \(unwrappedError)")
} else {
let results: AnyObject? = json["results"]
let name=results?.valueForKey("artistName")as! [String]
let genere=results?.valueForKey("primaryGenreName")as! [String]
artistName!.text=name[0]
artistGenre?.text=genere[0]
println("artist name: \(name[0])")
}
}
}
0 Comment(s)