diff --git a/Algorithms/TwoSum.playground/Contents.swift b/Algorithms/TwoSum.playground/Contents.swift index 30cc77a..c9f0ae3 100644 --- a/Algorithms/TwoSum.playground/Contents.swift +++ b/Algorithms/TwoSum.playground/Contents.swift @@ -13,11 +13,36 @@ let integers: [Int] = [2, 5, 4, 12, 7] Фукция должна возвращать индексы двух чисел из массива array, сумма которых равна переданному значению target */ +//func findTarget(_ target: Int, in array: [Int]) -> [Array.Index] { +// // Ваша реализация +// for i in 0.. [Array.Index] { // Ваша реализация - - return [0, 1] + var dict = [Int: Int]() + + for i in 0.. + + + + SchemeUserState + + Alamofire.xcscheme + + orderHint + 1 + + Pods-Rainy.xcscheme + + orderHint + 2 + + SwiftyJSON.xcscheme + + orderHint + 3 + + + + diff --git a/SwiftStyle/Rainy/Rainy.xcodeproj/xcuserdata/hollywood.xcuserdatad/xcschemes/xcschememanagement.plist b/SwiftStyle/Rainy/Rainy.xcodeproj/xcuserdata/hollywood.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..0f90625 --- /dev/null +++ b/SwiftStyle/Rainy/Rainy.xcodeproj/xcuserdata/hollywood.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + Rainy.xcscheme + + orderHint + 0 + + + + diff --git a/SwiftStyle/Rainy/Rainy.xcworkspace/xcuserdata/hollywood.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftStyle/Rainy/Rainy.xcworkspace/xcuserdata/hollywood.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..7b68345 Binary files /dev/null and b/SwiftStyle/Rainy/Rainy.xcworkspace/xcuserdata/hollywood.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SwiftStyle/Rainy/Rainy/AppDelegate.swift b/SwiftStyle/Rainy/Rainy/AppDelegate.swift index 9acb4d2..023bde5 100644 --- a/SwiftStyle/Rainy/Rainy/AppDelegate.swift +++ b/SwiftStyle/Rainy/Rainy/AppDelegate.swift @@ -10,43 +10,10 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - + // MARK: UIApplicationDelegate methods func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { - // Override point for customization after application launch. return true } - - func applicationWillResignActive(_ application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(_ application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - } - - func applicationWillEnterForeground(_ application: UIApplication) { - // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. - } - - func applicationDidBecomeActive(_ application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - - - - func applicationWillTerminate(_ application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - } - - - - - diff --git a/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift b/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift index 89abfb3..88654b3 100644 --- a/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift +++ b/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift @@ -11,9 +11,7 @@ import CoreLocation import Alamofire import SwiftyJSON - - -struct coords{ +struct coords { var lat: Double = 0 var lon: Double = 0 } @@ -27,9 +25,10 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate @IBOutlet weak var imageWeather: UIImageView! private var locationManager: CLLocationManager! - // private let constrain: Constants = Constants() + + // private let constrain: Constants = Constants() private var currentForecast: WeatherForecast? { - didSet{ + didSet { reloadUI() } } @@ -54,65 +53,80 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate "50d":#imageLiteral(resourceName: "fog"), "50n":#imageLiteral(resourceName: "fog")] - private var myCoords: coords = coords() - + // MARK: UIViewController methods override func viewDidLoad() { super.viewDidLoad() + locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers - if(CLLocationManager.authorizationStatus() == .notDetermined){ + + if(CLLocationManager.authorizationStatus() == .notDetermined) { locationManager.requestWhenInUseAuthorization() } - if CLLocationManager.locationServicesEnabled(){ + if CLLocationManager.locationServicesEnabled() { locationManager.startUpdatingLocation() } - DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: { + + DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { self.updateCurrentForecast() - }) + } } + // MARK: IBActions @IBAction func reloadButtonPressed(_ sender: AnyObject) { - if(CLLocationManager.authorizationStatus() == .notDetermined){ + if(CLLocationManager.authorizationStatus() == .notDetermined) { locationManager.requestWhenInUseAuthorization() } - if CLLocationManager.locationServicesEnabled(){ + if CLLocationManager.locationServicesEnabled() { locationManager.startUpdatingLocation() } + updateCurrentForecast() } - - - internal func locationManager(_ manager: CLLocationManager, - didFailWithError error: Error) { - print("error: ", error) + + // MARK: Internal methods + internal func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { + print("error: \(error)") } internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { self.myCoords.lat = locations[0].coordinate.latitude self.myCoords.lon = locations[0].coordinate.longitude + locationManager.stopUpdatingLocation() } - - private func updateCurrentForecast(){ - Alamofire.request("http://api.openweathermap.org/data/2.5/weather",method: .get,parameters: ["lat":myCoords.lat,"lon": myCoords.lon,"APPID": "","units":"metric"],encoding: JSONEncoding.default,headers: nil).responseJSON{response in - guard response.result.isSuccess else{ - return - } - let json = JSON(response.result.value!) - self.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json["main"]["temp"].doubleValue) / 10, - timeStamp: self.getCurrentTime(), - imageName: json["weather"][0]["icon"].string!, - locationCoordinates: (self.myCoords.lat, self.myCoords.lon), - humidity: json["main"]["humidity"].int, - pressure: json["main"]["pressure"].int, - wind: round(10 * json["wind"]["speed"].doubleValue) / 10, cityName: json["name"].string, - stateWeather: json["weather"][0]["description"].string) + // MARK: Private methods + private func updateCurrentForecast() { + Alamofire.request("http://api.openweathermap.org/data/2.5/weather", + method: .get, + parameters: ["lat":myCoords.lat, "lon": myCoords.lon, "APPID": "", "units":"metric"], + encoding: JSONEncoding.default, + headers: nil).responseJSON { response in + guard response.result.isSuccess else { + return + } + + guard let resultValue = response.result.value else { + return + } + + let json = JSON(resultValue) + + self.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json["main"]["temp"].doubleValue) / 10, + timeStamp: self.getCurrentTime(), + imageName: json["weather"][0]["icon"].string!, + locationCoordinates: (self.myCoords.lat, self.myCoords.lon), + humidity: json["main"]["humidity"].int, + pressure: json["main"]["pressure"].int, + wind: round(10 * json["wind"]["speed"].doubleValue) / 10, + cityName: json["name"].string, + stateWeather: json["weather"][0]["description"].string) } } @@ -120,49 +134,58 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate Alamofire.request("http://www.mapquestapi.com/traffic/v2/incidents", parameters: ["boundingBox": "\(myCoords.lat),\(myCoords.lon),\(myCoords.lat - 1),\(myCoords.lon - 1)", "key": ""]) - .responseJSON{response in - guard response.result.isSuccess else{ + .responseJSON { response in + guard response.result.isSuccess else { + return + } + + guard let resultValue = response.result.value else { return } - _ = JSON(response.result.value!) + _ = JSON(resultValue) } } - private func getCurrentTime() -> String{ + private func getCurrentTime() -> String { let date = NSDate() let calendar = NSCalendar.current var currentTime: String let components = calendar.dateComponents([.hour, .minute], from: date as Date!) var hour: String = String(describing: components.hour!) var minute: String = String(describing: components.minute!) - if hour.characters.count == 1{ + + if hour.characters.count == 1 { hour = "0" + hour } - if(minute.characters.count == 1){ + if(minute.characters.count == 1) { minute = "0" + minute } + currentTime = "\(hour):\(minute)" + return currentTime } - - private func reloadUI(){ - timeLabel.text = "Updated: \(currentForecast!.timeStamp)" - if let temp = currentForecast?.currentWeatherTempurature{ + guard let currentForecast = currentForecast else { + return + } + + timeLabel.text = "Updated: \(currentForecast.timeStamp)" + + if let temp = currentForecast.currentWeatherTempurature { temperatureLabel.text = "\(temp)℃" } - if let city = currentForecast?.cityName{ + if let city = currentForecast.cityName { cityNameLabel.text = city } - if let wi = currentForecast?.wind{ + if let wi = currentForecast.wind { windLabel.text = "\(wi)" } - if let st = currentForecast?.stateWeather{ + if let st = currentForecast.stateWeather { stateLabel.text = st } - imageWeather.image = photoResources[(currentForecast?.imageName)!] + + imageWeather.image = photoResources[currentForecast.imageName] } } - - diff --git a/SwiftStyle/Rainy/Rainy/HomeWeatherViewController.swift b/SwiftStyle/Rainy/Rainy/HomeWeatherViewController.swift index 046675e..40dbfd1 100644 --- a/SwiftStyle/Rainy/Rainy/HomeWeatherViewController.swift +++ b/SwiftStyle/Rainy/Rainy/HomeWeatherViewController.swift @@ -19,19 +19,21 @@ class HomeWeatherViewController: UIViewController { @IBOutlet weak var cityNameLabel: UILabel! private var setts = UserDefaults.standard - private var cityName: String{ - get{ - if let name = setts.value(forKey: "cityName"){ + private var cityName: String { + get { + if let name = setts.value(forKey: "cityName") { return name as! String } - else{ + else { return "Saint Petersburg" } } - }//"Saint Petersburg" -// private let constrain: Constants = Constants() + } + + // "Saint Petersburg" + // private let constrain: Constants = Constants() private var currentForecast: WeatherForecast? { - didSet{ + didSet { reloadUI() } } @@ -57,86 +59,83 @@ class HomeWeatherViewController: UIViewController { "50n":#imageLiteral(resourceName: "fog") ] - - + // MARK: UIViewController methods override func viewDidLoad() { super.viewDidLoad() updateCurrentForecast() } - + // MARK: IBActions @IBAction func refreshButtonPressed(_ sender: Any) { updateCurrentForecast() } - - private func updateCurrentForecast(){ + // MARK: Private methods + private func updateCurrentForecast() { Alamofire.request("http://api.openweathermap.org/data/2.5/weather", - parameters: ["q": cityName, - "APPID": "","units":"metric"]) - .responseJSON{response in - guard response.result.isSuccess else{ + parameters: ["q": cityName, "APPID": "","units":"metric"]).responseJSON { response in + guard response.result.isSuccess else { return } - let json = JSON(response.result.value!) + + guard let resultValue = response.result.value else { + return + } + + let json = JSON(resultValue) + self.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json["main"]["temp"].doubleValue) / 10, timeStamp: self.getCurrentTime(), imageName: json["weather"][0]["icon"].string!, locationCoordinates: (0, 0), humidity: json["main"]["humidity"].int, pressure: json["main"]["pressure"].int, - wind: round(10 * json["wind"]["speed"].doubleValue) / 10, cityName: json["name"].string, + wind: round(10 * json["wind"]["speed"].doubleValue) / 10, + cityName: json["name"].string, stateWeather: json["weather"][0]["description"].string) } } - private func getCurrentTime() -> String{ + private func getCurrentTime() -> String { let date = NSDate() let calendar = NSCalendar.current var currentTime: String let components = calendar.dateComponents([.hour, .minute], from: date as Date!) var hour: String = String(describing: components.hour!) var minute: String = String(describing: components.minute!) + if hour.characters.count == 1{ hour = "0" + hour } if(minute.characters.count == 1){ minute = "0" + minute } + currentTime = "\(hour):\(minute)" + return currentTime } - private func reloadUI(){ - timeLabel.text = "Updated: \(currentForecast!.timeStamp)" - if let temp = currentForecast?.currentWeatherTempurature{ + private func reloadUI() { + guard let currentForecast = currentForecast else { + return + } + + timeLabel.text = "Updated: \(currentForecast.timeStamp)" + + if let temp = currentForecast.currentWeatherTempurature{ temperatureLabel.text = "\(temp)℃" } - if let city = currentForecast?.cityName{ + if let city = currentForecast.cityName{ cityNameLabel.text = city } - if let wi = currentForecast?.wind{ + if let wi = currentForecast.wind{ windLabel.text = "\(wi)" } - if let st = currentForecast?.stateWeather{ + if let st = currentForecast.stateWeather{ stateLabel.text = st } - imageWeatherView.image = photoResources[(currentForecast?.imageName)!] + + imageWeatherView.image = photoResources[currentForecast.imageName] } - - - } - - - - - - - - - - - - - diff --git a/SwiftStyle/Rainy/Rainy/SettingsViewController.swift b/SwiftStyle/Rainy/Rainy/SettingsViewController.swift index 87461a0..a92a02f 100644 --- a/SwiftStyle/Rainy/Rainy/SettingsViewController.swift +++ b/SwiftStyle/Rainy/Rainy/SettingsViewController.swift @@ -12,25 +12,28 @@ class SettingsViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var swifthLocation: UISwitch! - @IBAction func saveButtonPressed(_ sender: Any) { - self.view.endEditing(true) - setts.set(textField.text!, forKey: "cityName") - } - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - textField.resignFirstResponder() - } - private var setts = UserDefaults.standard + + // MARK: UIViewController methods override func viewDidLoad() { super.viewDidLoad() - if let name = setts.value(forKey: "cityName"){ + + if let name = setts.value(forKey: "cityName") { textField.text = String(describing: name) } - else{ + else { textField.text = "Saint Petersburg" } } - + // MARK: UIResponser methods + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + textField.resignFirstResponder() + } + + // MARK: IBActions + @IBAction func saveButtonPressed(_ sender: Any) { + self.view.endEditing(true) + setts.set(textField.text!, forKey: "cityName") + } } diff --git a/SwiftStyle/Rainy/Rainy/WeatherForecast.swift b/SwiftStyle/Rainy/Rainy/WeatherForecast.swift index 47152bc..8571e11 100644 --- a/SwiftStyle/Rainy/Rainy/WeatherForecast.swift +++ b/SwiftStyle/Rainy/Rainy/WeatherForecast.swift @@ -8,7 +8,6 @@ import Foundation - class WeatherForecast{ var currentWeatherTempurature: Double? var timeStamp: String @@ -20,9 +19,10 @@ class WeatherForecast{ var cityName: String? var stateWeather: String? - init(currentWeatherTempurature: Double?, + init(currentWeatherTempurature: Double?, timeStamp: String, imageName: String, - locationCoordinates: (Double, Double)?, humidity: Int?, pressure: Int?, + locationCoordinates: (Double, Double)?, + humidity: Int?, pressure: Int?, wind: Double?, cityName: String?, stateWeather: String?) { self.currentWeatherTempurature = currentWeatherTempurature