how to fix "missing argument label in call" in swift 3 [duplicate] - ios

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 6 years ago.
i'm a totally beginner of swift and ios programming
and i met a question by following a coding lesson video which was written by swift 1 and xcode 6 beta.
i know the version of swift had changed ,and the syntax had been changed a lot two.
and i have fix some problems but there is still one that i can't deal with.
That's "missing argument label in call"
the following is my code:
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManger:CLLocationManager = CLLocationManager()
#IBOutlet weak var location: UILabel!
#IBOutlet weak var icon: UIImageView!
#IBOutlet weak var temperature: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
if(ios10()) {
locationManger.requestWhenInUseAuthorization()
}
locationManger.startUpdatingLocation()
}
func ios10() ->Bool {
return UIDevice.current.systemVersion == "10.2"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let location:CLLocation = locations[locations.count-1] as CLLocation
if(location.horizontalAccuracy > 0) {
print(location.coordinate.latitude)
print(location.coordinate.longitude)
self.updateWeatherInfo(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
locationManger.stopUpdatingLocation()
}
}
func updateWeatherInfo(latitude:CLLocationDegrees,longitude:CLLocationDegrees){
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather?APPID=c5a8f49ee6e86f1aaa2be178f25f37f2"
let params = ["lat":latitude,"lon":longitude,"cnt":0]
manager.get(url,
parameters: params,
success: { (operation:AFHTTPRequestOperation!, responseObject: Any!) in
print("JSON:" + (responseObject as AnyObject).description!)
updateUISuccess(responseObject as! NSDictionary!)
//that's where my error1 :missing argument label 'jsonResult' in call
}
)
}
func updateUISuccess(jsonResult:NSDictionary){
if let tempResult = (jsonResult["main"] as? [String:Double])?["type"] {
//if let tempResult = (jsonResult["main"] as? [String:Double])?["type"] current
//if let tempResult = jsonResult["main"]?["temp"]? as? Double pre
var temperature:Double
if ((jsonResult["sys"] as? [String:String])?["country"] == "US"){
//CONVERT TO FAHRENHEIT IF USER IN US
temperature = round(((temperature - 273.15) * 1.8) + 32)
}
else{
//CONVERT TO CELSIUS
temperature = round(temperature - 273.15)
}
self.temperature.text = "\(temperature)°"
print(temperature)
var name = jsonResult["name"] as! String
self.location.text = "\(name)"
var conditionArray = (jsonResult["weather"] as! NSArray)[0] as! NSDictionary
var condition = conditionArray["id"] as! Int
var sunrise = (jsonResult["sys"] as? [String:Double])?["sunrise"]
var sunset = (jsonResult["sys"] as? [String:Double])?["sunset"]
var nightTime = false
var now = NSDate().timeIntervalSince1970
if (now < sunrise! || now > sunset!) {
nightTime = true
}
//self.icon.image = UIImage(named:"sunny")
updateWeatherIcon(condition,nightTime: nightTime)
//that's where my error2 :missing argument label 'condition:' in call
}
else {
print("error!")
}
}
func updateWeatherIcon(condition: Int,nightTime: Bool){
//thunderstorm
if(condition < 300) {
if nightTime {
self.icon.image = UIImage(named:"tstorm1_night")
}
else {
self.icon.image = UIImage(named:"tstorm1")
}
}
//drizzle
else if (condition < 500) {
}
//rain
else if (condition < 600) {
}
//snow
else if (condition < 700) {
}
//fog
else if (condition < 771) {
if nightTime {
self.icon.image = UIImage(named: "fog_night")
}
else {
self.icon.image = UIImage(named: "fog")
}
}
//tornado
else if (condition < 800) {
self.icon.image = UIImage(named:"tstorm3")
}
//clear
else if (condition == 800) {
if nightTime {
self.icon.image = UIImage(named:"sunny_night")
}
else {
self.icon.image = UIImage(named:"sunny")
}
}
//few clouds
else if (condition < 804) {
if nightTime {
self.icon.image = UIImage(named:"cloudy2_night")
}
else {
self.icon.image = UIImage(named:"cloudy2")
}
}
//overcast
else if (condition == 804) {
self.icon.image = UIImage(named:"overcast")
}
//extreme
else if ((condition >= 900 && condition < 903) || (condition >= 904 && condition < 1000)){
self.icon.image = UIImage(named:"tstorm3")
}
//cold
else if (condition == 903) {
self.icon.image = UIImage(named:"snow5")
}
//hot
else if (condition == 904) {
self.icon.image = UIImage(named:"sunny")
}
//dont know
else {
self.icon.image = UIImage(named:"dono")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
print(error)
}
}
i want to know how to fix it
thank you very much!

The error is because your function requires that you name the argument you are passing in. Try changing:
updateUISuccess(responseObject as! NSDictionary!)
To:
updateUISuccess(jsonResult: responseObject as! NSDictionary!)
Alternatively, you can define the function like this to not require the parameter to be named:
func updateUISuccess(_ jsonResult:NSDictionary){
Note the underscore.
Your second error has a similar cause, so change:
updateWeatherIcon(condition,nightTime: nightTime)
to:
updateWeatherIcon(condition: condition, nightTime: nightTime)

Related

Swift CMMotionActivityManager not displaying correct authorization Status

I am trying to create a very simple step counter and am following Kamil Wysocki's article at https://brightinventions.pl/blog/coremotion-pedometer-swift/. However, my .isActivityAvailable() is returning False preventing any of my other functions from initiating. Also, my CMMotionActivityManager.authorizationStatus() call is returning 3 (denied). I cannot for the life of me figure out why this is happening.
I have added Motion Usage Description to my info.plist with a description and have enabled authorization on my iPhone simulator. I have also tried simulating the "City walk" mode in the debug menu, which doesn't help. My code is below.
import UIKit
import CoreMotion
import CoreLocation
class ViewController: UIViewController {
#IBOutlet weak var startButton: UIButton!
#IBOutlet weak var activityTypeLabel: UILabel!
#IBOutlet weak var stepsCountLabel: UILabel!
private let activityManager = CMMotionActivityManager()
private let pedometer = CMPedometer()
private var shouldStartUpdating: Bool = false
private var startDate: Date? = nil
override func viewDidLoad() {
super.viewDidLoad()
startButton.addTarget(self, action: #selector(didTapStartButton), for: .touchUpInside)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let startDate = startDate else { return }
updateStepsCountLabelUsing(startDate: startDate)
}
#objc private func didTapStartButton() {
shouldStartUpdating = !shouldStartUpdating
shouldStartUpdating ? (onStart()) : (onStop())
}
}
extension ViewController {
private func onStart() {
startButton.setTitle("Stop", for: .normal)
startDate = Date()
checkAuthorizationStatus()
startUpdating()
}
private func onStop() {
startButton.setTitle("Start", for: .normal)
startDate = nil
stopUpdating()
}
private func startUpdating() {
if CMMotionActivityManager.isActivityAvailable() {
startTrackingActivityType()
} else {
activityTypeLabel.text = "Not available"
}
if CMPedometer.isStepCountingAvailable() {
startCountingSteps()
} else {
stepsCountLabel.text = "Not available"
}
}
private func checkAuthorizationStatus() {
switch CMMotionActivityManager.authorizationStatus() {
case CMAuthorizationStatus.denied:
onStop()
activityTypeLabel.text = "Not available"
stepsCountLabel.text = "Not available"
default:break
}
}
private func stopUpdating() {
activityManager.stopActivityUpdates()
pedometer.stopUpdates()
pedometer.stopEventUpdates()
}
private func on(error: Error) {
//handle error
}
private func updateStepsCountLabelUsing(startDate: Date) {
pedometer.queryPedometerData(from: startDate, to: Date()) {
[weak self] pedometerData, error in
if let error = error {
self?.on(error: error)
} else if let pedometerData = pedometerData {
DispatchQueue.main.async {
self?.stepsCountLabel.text = String(describing: pedometerData.numberOfSteps)
}
}
}
}
private func startTrackingActivityType() {
activityManager.startActivityUpdates(to: OperationQueue.main) {
[weak self] (activity: CMMotionActivity?) in
guard let activity = activity else { return }
DispatchQueue.main.async {
if activity.walking {
self?.activityTypeLabel.text = "Walking"
} else if activity.stationary {
self?.activityTypeLabel.text = "Stationary"
} else if activity.running {
self?.activityTypeLabel.text = "Running"
} else if activity.automotive {
self?.activityTypeLabel.text = "Automotive"
}
}
}
}
private func startCountingSteps() {
pedometer.startUpdates(from: Date()) {
[weak self] pedometerData, error in
guard let pedometerData = pedometerData, error == nil else { return }
DispatchQueue.main.async {
self?.stepsCountLabel.text = pedometerData.numberOfSteps.stringValue
}
}
}
}
Here is my viewController upon running and clicking start
Big thanks in advance for anyone who can offer any knowledge!!!
Use this code to check authorization status :
let manager = CMMotionActivityManager()
let today = Date()
manager.queryActivityStarting(from: today, to: today, to: OperationQueue.main, withHandler: { (activities: [CMMotionActivity]?, error: Error?) -> () in
if error != nil {
let errorCode = (error! as NSError).code
if errorCode == Int(CMErrorMotionActivityNotAuthorized.rawValue) {
print("NotAuthorized")
}
} else {
print("Authorized")
//Start Tracking Activity
}
manager.stopActivityUpdates()
})
Hope this will work for you.

Variable in renderer(_:didAdd:for:) not updating

I am working on a IPhone app that uses CoreML and ARKit simultaneously. The CoreML is supposed to recognize a number and the ARKit should detect a vertical plane (aka wall) and add some planes over that same wall with the content displayed on those planes depending on the recognised number.
So, the CoreML is working 100%. Everytime I "change" the number the topPrediction variable updates automatically ( so far so good ). The thing is that my variable in func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor does not update! What I mean is that the first number recognized by the CoreML is correctly sent to the renderer func and it works like a charm but if I turn the camera to another number it stills assumes that it's the first number! As you may see in the code, I even tried making a func getGabNum() -> Int and then calling it in the renderer func ( var num = getGabNum() ) but I continue getting the warning "Variable 'num' was never mutated; consider changing to 'let' constant" which means that something is not right. So guys, here is my code! Hope you can help me and thank you!
struct Room : Decodable {
let id : Int?
let num : Int?
//Adicionar Schedules
var horario = [Schedule]()
}
struct Schedule : Decodable {
let id : Int?
let hora_ini : Date?
let hora_fim : Date?
let descr : String?
private enum CodingKeys: String, CodingKey {
case id
case hora_ini
case hora_fim
case descr
}
}
class ViewController: UIViewController, ARSCNViewDelegate {
#IBOutlet weak var debugLabel: UILabel!
#IBOutlet weak var debugTextView: UITextView!
#IBOutlet weak var sceneView: ARSCNView!
let dispatchQueueML = DispatchQueue(label: "com.hw.dispatchqueueml") // A Serial Queue
var visionRequests = [VNRequest]()
var room: Room?
var room_array: [[Int]] = [[17, 0], [43, 0], [120,0]]
var teste = 0
var num = -1
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.showsStatistics = true
let scene = SCNScene()
sceneView.scene = scene
configureLighting()
guard let selectedModel = try? VNCoreMLModel(for: SalasMLv6().model) else {
fatalError("Could not load model.")
}
let classificationRequest = VNCoreMLRequest(model: selectedModel, completionHandler: classificationCompleteHandler)
classificationRequest.imageCropAndScaleOption = VNImageCropAndScaleOption.centerCrop // Crop from centre of images and scale to appropriate size.
visionRequests = [classificationRequest]
loopCoreMLUpdate()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setUpSceneView()
}
func setUpSceneView()
{
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .vertical
sceneView.session.run(configuration)
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
func configureLighting()
{
sceneView.automaticallyUpdatesLighting = true
sceneView.autoenablesDefaultLighting = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
// Do any desired updates to SceneKit here.
}
}
func loopCoreMLUpdate() {
dispatchQueueML.async {
self.updateCoreML()
self.loopCoreMLUpdate()
}
}
func updateCoreML() {
// Get Camera Image as RGB
let pixbuff : CVPixelBuffer? = (sceneView.session.currentFrame?.capturedImage)
if pixbuff == nil { return }
let ciImage = CIImage(cvPixelBuffer: pixbuff!)
// Prepare CoreML/Vision Request
let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage, options: [:])
// Run Vision Image Request
do {
try imageRequestHandler.perform(self.visionRequests)
} catch {
print(error)
}
}
func classificationCompleteHandler(request: VNRequest, error: Error?) {
// Catch Errors
if error != nil {
print("Error: " + (error?.localizedDescription)!)
return
}
guard let observations = request.results else {
print("No results")
return
}
// Get Classifications
let classifications = observations[0...2] // top 3 results
.compactMap({ $0 as? VNClassificationObservation })
.map({ "\($0.identifier) \(String(format:" : %.2f", $0.confidence))" })
.joined(separator: "\n")
// Render Classifications
DispatchQueue.main.async {
// Display Debug Text on screen
self.debugTextView.text = "TOP 3 PROBABILITIES: \n" + classifications
// Display Top Symbol
var symbol = "❌"
var gabNum: Int?
let topPrediction = classifications.components(separatedBy: "\n")[0]
let topPredictionName = topPrediction.components(separatedBy: ":")[0].trimmingCharacters(in: .whitespaces)
// Only display a prediction if confidence is above 90%
let topPredictionScore:Float? = Float(topPrediction.components(separatedBy: ":")[1].trimmingCharacters(in: .whitespaces))
if (topPredictionScore != nil && topPredictionScore! > 0.05) {
if (topPredictionName == "120") {
symbol = "1️⃣2️⃣0️⃣"
gabNum = 120
self.teste = gabNum!
}
if (topPredictionName == "43") {
symbol = "4️⃣3️⃣"
gabNum = 43
self.teste = gabNum!
}
if (topPredictionName == "17") {
symbol = "1️⃣7️⃣"
gabNum = 17
self.teste = gabNum!
}
}
if let gn = gabNum {
// get room from REST
let jsonURL = "someURL\(gn)"
guard let url = URL(string: jsonURL) else {
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil{
print("error)")
return
}
do {
self.room = try JSONDecoder().decode(Room.self, from: data!)
}catch{
print(“Decoder Error”)
}
}.resume()
}
self.debugLabel.text = symbol
}
}
// MARK: - HIDE STATUS BAR
override var prefersStatusBarHidden : Bool { return true }
func getGabNum() -> Int {
return self.teste
}
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
{
guard room != nil else {
print("room == nil")
return
}
guard let planeAnchor = anchor as? ARPlaneAnchor else {
return
}
num = getGabNum()
if( num == room_array[0][0] && room_array[0][1] == 1 ){
return
}else{
if( num == room_array[1][0] && room_array[1][1] == 1 ){
return
}else{
if( num == room_array[2][0] && room_array[2][1] == 1 ){
return
}else{
var i = 0
for horario in (self.room?.horario)!{
// Planes and Nodes Stuff Right Here
}
switch self.room?.num{
case 17: room_array[0][1] = 1
case 43: room_array[1][1] = 1
case 120:room_array[2][1] = 1
}
}
}
}
}
}

Trigger action from another swift class

I'm making a TextField which takes a proper date only. Now when I'm using the text field delegate and action methods in the same ViewController class in which my UITextField is, it is working fine. But, I have to make it like a reusable component which can be used in any project by drag and drop. I have two files - MakeDate.swift and ViewController.swift
MakDate.swift --
import Foundation
import UIKit
class MakeDate: NSObject, UITextFieldDelegate {
var textField: UITextField!
var string: String!
var viewController: UIViewController!
let characterset = NSCharacterSet(charactersInString: "0123456789")
init?(textField: UITextField!) {
self.textField = textField
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
self.string = string
return true
}
func takeProperDateInput() {
textField.addTarget(textField, action: #selector(self.textChange(_:)), forControlEvents: .EditingChanged)
}
#IBAction func textChange(sender: UITextField) {
print("\(sender.text!)")
print("\(sender.text!) && \(self.string)")
if self.string == "" {
if sender.text?.characters.count == 2 {
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
}
else if sender.text?.characters.count == 5 {
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
}
}
if sender.text?.characters.count == 1 && self.string != "" {
if self.string.rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
else if Int(sender.text!) != 1 && Int(sender.text!) != 0 {
sender.text = "0" + sender.text! + "/"
}
}
else if sender.text?.characters.count == 2 && self.string != "" {
if Int(string) != 1 && Int(string) != 2 && Int(string) != 0{
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}else {
sender.text = sender.text! + "/"
}
}
else if sender.text?.characters.count == 4 && self.string != "" {
if self.string.rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
else if sender.text?.characters.count == 5 && self.string != "" {
if self.string == "/" {
var yearComponent = sender.text?.componentsSeparatedByString("/")
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
sender.text = sender.text! + "0" + yearComponent![1] + "/"
}else {
var yearComponent = sender.text?.componentsSeparatedByString("/")
if Int(yearComponent![1]) > 31 {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}else if Int(yearComponent![1]) > 0 && Int(yearComponent![1]) < 32 {
sender.text = sender.text! + "/"
}else {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
else if sender.text?.characters.count == 10 && self.string != "" {
let index = sender.text?.endIndex.advancedBy(-4)
if sender.text?.substringFromIndex(index!).rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-4)
sender.text = sender.text?.substringToIndex(index!)
} else {
var yearComponent = sender.text?.componentsSeparatedByString("/")
if Int(yearComponent![2]) == 0 {
let index = sender.text?.endIndex.advancedBy(-4)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
else if sender.text?.characters.count > 10 && self.string != "" {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
and ViewController.swift --
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var dateInsert: UITextField!
var dateMaker: MakeDate!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
dateMaker = MakeDate(textField: self.dateInsert)
self.dateInsert.delegate = self.dateMaker
self.dateMaker.takeProperDateInput()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The error i get is unrecognized selector sent to instance
Create a textfield class with the default properties and implement in the view controller not a nsobject to send a textfield as parameter:
class baseTextfield: UITextField, UITextFieldDelegate {
}
class ViewController: UIViewController{
#IBOutlet weak var dateInsert: UIBaseTextField!
}
The properties are of the textfield, not of the view.

how it is possible without using button directly click on the cell it will shows the address?

This is a code which is give me the address after click the button get my location but i want the code for without using button directly click on the cell it will shows the address
soucre code:
let locationManager = CLLocationManager()
var location: CLLocation?
var updatingLocation = false
var lastLocationError: NSError?
let geocoder = CLGeocoder()
var placemark: CLPlacemark?
var performingReverseGeocoding = false
var lastGeocodingError: NSError?
var timer: NSTimer?
override func viewDidLoad()
{
super.viewDidLoad()
updateLabels()
configureGetButton()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func getLocation()
{
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined
{
locationManager.requestWhenInUseAuthorization()
return
}
if updatingLocation
{
stopLocationManager()
} else
{
location = nil
lastLocationError = nil
placemark = nil
lastGeocodingError = nil
startLocationManager()
}
updateLabels()
configureGetButton()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("didFailWithError \(error)")
if error.code == CLError.LocationUnknown.rawValue
{
return
}
lastLocationError = error
stopLocationManager()
updateLabels()
configureGetButton()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let newLocation = locations.last
print("didUpdateLocations \(newLocation)")
if newLocation!.timestamp.timeIntervalSinceNow < -5
{
return
}
if newLocation!.horizontalAccuracy < 0
{
return
}
var distance = CLLocationDistance(DBL_MAX)
if let location = location
{
distance = newLocation!.distanceFromLocation(location)
}
if location == nil || location!.horizontalAccuracy > newLocation!.horizontalAccuracy
{
lastLocationError = nil
location = newLocation
updateLabels()
if newLocation!.horizontalAccuracy <= locationManager.desiredAccuracy
{
print("*** We're done!")
stopLocationManager()
configureGetButton()
if distance > 0
{
performingReverseGeocoding = false
}
}
if !performingReverseGeocoding
{
print("*** Going to geocode")
performingReverseGeocoding = true
geocoder.reverseGeocodeLocation(location!, completionHandler: {
placemarks, error in
print("*** Found placemarks: \(placemarks), error: \(error)")
self.lastGeocodingError = error
if error == nil && !placemarks!.isEmpty
{
self.placemark = placemarks!.last
}
else
{
self.placemark = nil
}
self.performingReverseGeocoding = false
self.updateLabels()
})
}
}
else if distance < 1.0
{
let timeInterval = newLocation!.timestamp.timeIntervalSinceDate(location!.timestamp)
if timeInterval > 10
{
print("**Force done!")
stopLocationManager()
updateLabels()
configureGetButton()
}
}
}
var datePickerVisible = false
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("DatePickerCell")
if indexPath.section == 2 && indexPath.row == 1
{
if cell == nil {
cell = UITableViewCell(style: .Default,
reuseIdentifier: "DatePickerCell")
cell.selectionStyle = .None
// 3
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 320, height: 216))
datePicker.tag = 100
cell.contentView.addSubview(datePicker)
// 4
datePicker.addTarget(self, action: Selector("getLocation"), forControlEvents: .ValueChanged)
}
return cell
}else
{
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}
}
func showLocationServiceDeniedAlert()
{
let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app in settings.", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(okAction)
presentViewController(alert, animated: true, completion: nil)
}
func startLocationManager()
{
if CLLocationManager.locationServicesEnabled()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
updatingLocation = true
}
}
func stopLocationManager()
{
if updatingLocation
{
if let timer = timer
{
timer.invalidate()
}
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
updatingLocation = false
}
}
func configureGetButton()
{
if updatingLocation
{
getButton.setTitle("Stop", forState: .Normal)
}
else
{
getButton.setTitle("GetMyLocation", forState: .Normal)
}
}
func stringFromPlacemark(placemark: CLPlacemark) -> String
{
return "\(placemark.subThoroughfare) \(placemark.thoroughfare)\n" +
"\(placemark.locality) \(placemark.administrativeArea)" +
"\(placemark.postalCode)"
}
func didTimeOut()
{
print("*** Time Out")
if location == nil
{
stopLocationManager()
lastLocationError = NSError(domain: "MyLocationsErrorDomain", code: 1, userInfo: nil)
updateLabels()
configureGetButton()
}
}
func updateLabels()
{
if let location = location
{
if let placemark = placemark
{
addressLabel.text = stringFromPlacemark(placemark)
}else if performingReverseGeocoding
{
addressLabel.text = "Eror Finding Address"
}
else
{
addressLabel.text = "No Address Found"
}
}
else
{
addressLabel.text = ""
var statusMessage: String
if let error = lastLocationError
{
if error.domain == kCLErrorDomain && error.code == CLError.Denied.rawValue
{
statusMessage = "Location Services Disabled"
}
else
{
statusMessage = "Error Getting Location"
}
}
else if !CLLocationManager.locationServicesEnabled()
{
statusMessage = "Location Services Disabled"
}
else if updatingLocation
{
statusMessage = "Searching.."
}
else
{
statusMessage = "Tap 'Get My Location' to start"
}
}
}
}
just add this method
func tableView(_ tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if(indexPath.section == 0)
{
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined
{
locationManager.requestWhenInUseAuthorization()
return
}
if updatingLocation
{
stopLocationManager()
} else
{
location = nil
lastLocationError = nil
placemark = nil
lastGeocodingError = nil
startLocationManager()
}
updateLabels()
configureGetButton()
}
else
{
//that is second section
}
}
You have three options:
You can call the button action after a delay, using the following code:
let seconds = 1.0
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
// here code perfomed with delay
})
You can add tapGestureRecognizer like:
let backgoroundTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
tableView.addGestureRecognizer(backgoroundTap)
and call the function as :
func DismissKeyboard(){
view.endEditing(true)
}
Or you can fake the button press as
self.button.sendActionsForControlEvents(UIControlEvents.TouchUpInside)

How can I make these two functions work well together?

I want to randomize the image background of weather depending on the weather information. I want to make them work one after the other;
the first function will make a string and the second function use the string produced by the first one.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var cityNameTextField: UITextField!
#IBOutlet weak var weatherFact: UILabel!
#IBOutlet weak var weatherImage: UIImageView!
var weather = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.cityNameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func weatherButton(sender: UIButton) {
weatherInformation()
randomWeatherImage()
}
func showError() {
self.weatherFact.text = "we can't load the weather of \(cityNameTextField.text). please try again"
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
func randomWeatherImage() {
if weatherFact.text != nil {
let string = weather
let regex = NSRegularExpression(pattern: "-?[0-9]{1,3}", options: NSRegularExpressionOptions(), error: nil)
if let matches = regex?.matchesInString(string, options: NSMatchingOptions(), range: NSRange(location: 0, length: count(string))){
let degrees = matches.map { return(string as NSString).substringWithRange($0.range) }
var maxDegree = degrees[0].toInt()
var minDegree = degrees[1].toInt()
var averageWeather:Int = (maxDegree! + minDegree!) / 2
if averageWeather < 0 {
self.weatherImage.image = UIImage(named: "Cold.jpg")
}
else if averageWeather > 20 {
self.weatherImage.image = UIImage(named: "Warm.jpg")
}
else {
self.weatherImage.image = UIImage(named: "Mild.jpg")
}
}
}
}
func weatherInformation() {
let url = NSURL(string: "http://www.weather-forecast.com/locations/" + cityNameTextField.text.stringByReplacingOccurrencesOfString(" ", withString: "-") + "/forecasts/latest")
if url != nil {
let task = NSURLSession.sharedSession().dataTaskWithURL(url!){ (data, response, error) in
var urlError = false
if error == nil {
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
var urlContentArray = urlContent!.componentsSeparatedByString("<span class=\"phrase\">")
if urlContentArray.count > 0 {
var weatherArray = urlContentArray[1].componentsSeparatedByString("</span>")
self.weather = weatherArray[0] as! String
self.weather = self.weather.stringByReplacingOccurrencesOfString("°", withString: "º")
}
else {
urlError = true
}
}
else {
urlError = true
}
dispatch_async(dispatch_get_main_queue()) {
if urlError == true {
self.showError()
}
else {
self.weatherFact.text = self.weather
}
}
}
task.resume()
}
else {
showError()
}
}
}
Just call randomWeatherImage after you set weatherFact.text:
dispatch_async(dispatch_get_main_queue()) {
if urlError == true {
self.showError()
}
else {
self.weatherFact.text = self.weather
self.randomWeatherImage()
}
}

Resources