Subscript error when trying to get keyboard frame? - swift

I am trying to get the keyboard frame from keyboard notfication. But I am getting the error "[NSObject:AnyObject] does not have any membership subscript"
I am using the following code.
if keyboardNotification != nil {
let userInfo = keyboardNotification!.userInfo
let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSNumber).doubleValue()
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
}

notification.userInfo is [NSObject : AnyObject]?.
So, try this
if let userInfo = keyboardNotification.userInfo {
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue()
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
}

Related

How to calculate ETA time with Google Maps API - Swift

I am drawing a route with Google map. I calculate the km distance on the route I drew. I also want to calculate how long it will go. How can I calculate how many minutes the route will take? I calculated the speed in the code below and when I tried to calculate the time using the speed I could not get any output. How can I calculate the duration?
func drowRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D) {
self.mapView.clear()
let origin = "\(source.latitude),\(source.longitude)"
let destinationn = "\(destination.latitude),\(destination.longitude)"
guard let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destinationn)&mode=driving&key=..") else {
let error = NSError(domain: "LocalDomain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to create object URL"])
print("Error: \(error)")
return
}
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
else {
do {
if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{
guard let routes = json["routes"] as? NSArray else {
DispatchQueue.main.async {
}
return
}
if (routes.count > 0) {
let overview_polyline = routes[0] as? NSDictionary
let dictPolyline = overview_polyline?["overview_polyline"] as? NSDictionary
let points = dictPolyline?.object(forKey: "points") as? String
DispatchQueue.main.async {
//
let legs = overview_polyline?["legs"] as! Array<Dictionary<String, AnyObject>>
let distance = legs[0]["distance"] as? NSDictionary
let distanceValue = distance?["value"] as? Int ?? 0
let distanceDouleValue = distance?["value"] as? Double ?? 0.0
let duration = legs[0]["duration"] as? NSDictionary
let totalDurationInSeconds = duration?["value"] as? Int ?? 0
let durationDouleValue = duration?["value"] as? Double ?? 0.0
if(distanceValue != 0) {
self.speed = distanceDouleValue / durationDouleValue
print("speed", self.speed)
}
let miles = Double(distanceValue) / 1609.344
print("\(miles)")
let km = Double(distanceValue) * 0.001609
self.kmLabel.text = ("\(Int(km))" + " " + "KM")
if distanceValue > Int(32186.9){
}else{
self.showPath(polyStr: points!)
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<String, AnyObject>
let originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<String, AnyObject>
let destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude:destinationCoordinate.latitude, longitude: destinationCoordinate.longitude)
marker1.icon = UIImage(named: "placeholder")
marker1.map = self.mapView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude:originCoordinate.latitude, longitude: originCoordinate.longitude)
marker2.icon = UIImage(named: "location")
marker2.map = self.mapView
}
}
}
else {
print(json)
DispatchQueue.main.async {
// SVProgressHUD.dismiss()
}
}
}
}
catch {
print("error in JSONSerialization")
DispatchQueue.main.async {
// SVProgressHUD.dismiss()
}
}
}
})
task.resume()
}
func showPath(polyStr :String){
// SVProgressHUD.dismiss()
let path = GMSPath(fromEncodedPath: polyStr)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 5.0
polyline.strokeColor = UIColor.red
polyline.map = mapView
DispatchQueue.main.async {
let bounds = GMSCoordinateBounds(path: path!)
let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 170, left: 30, bottom: 30, right: 30))
self.mapView.moveCamera(update)
}
}

Moving windows programmatically on MacOS in Swift

I'm trying to move a window programmatically on my desktop. For this I've tried:
let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let windowsList = windowsListInfo as NSArray? as? [[String: AnyObject]]
let visibleWindows = windowsList?.filter{ $0["kCGWindowLayer"] as! Int == 0 }
for window in visibleWindows! {
let windowTitle = window["kCGWindowOwnerName"] as! String
let windowNumber = window["kCGWindowNumber"] as! Int32
if windowNumber == 124 { // Safari
let nsWindow = NSApp.window(withWindowNumber: Int(windowNumber))
nsWindow?.cascadeTopLeft(from: NSPoint(x: 100.0, y: 100.0))
nsWindow?.setFrameTopLeftPoint(NSPoint(x: 100.0, y: 100.0))
}
}
}
but when I'm trying to get NSWindow by using windowNumber:
NSApp.window(withWindowNumber: Int(windowNumber))
I get nil.
So my question is, how can I move any window programmatically by knowing its windowNumber?
I've even tried to bring that window to front and
let pid = window["kCGWindowOwnerPID"] as? Int32 {
let app = NSRunningApplication(processIdentifier: pid)
app?.activate(options: .activateIgnoringOtherApps)
doing this but nothing helped me.
Could someone help me to find out the way how I can move the Safari window(for example) programmatically?
This should get the trick done:
let owner = entry[kCGWindowOwnerName as String] as! String
var bounds = entry[kCGWindowBounds as String] as? [String: Int]
let pid = entry[kCGWindowOwnerPID as String] as? Int32
if owner == "Safari"
{
let appRef = AXUIElementCreateApplication(pid!);
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)
if let windowList = value as? [AXUIElement] {
print ("windowList #\(windowList)")
if let window = windowList.first
{
var position : CFTypeRef
var size : CFTypeRef
var newPoint = CGPoint(x: 0, y: 0)
var newSize = CGSize(width: 300, height: 800)
position = AXValueCreate(AXValueType(rawValue: kAXValueCGPointType)!,&newPoint)!;
AXUIElementSetAttributeValue(windowList.first!, kAXPositionAttribute as CFString, position);
size = AXValueCreate(AXValueType(rawValue: kAXValueCGSizeType)!,&newSize)!;
AXUIElementSetAttributeValue(windowList.first!, kAXSizeAttribute as CFString, size);
}
}
}
By the way, I found this code in another discussion which I can't find at the moment, sorry about that
Oh and you need this code to request the accessibility access:
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String : true]
let accessEnabled = AXIsProcessTrustedWithOptions(options)
if !accessEnabled {
print("Access Not Enabled")
}
Hope this helps, even if it's been a year and five days since you posted this question :)

Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails

This line let userInfo = notification.userInfo as! NSDictionary I get a warning: Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails
I try to use let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject> replace let userInfo = notification.userInfo as! NSDictionary. But I get an error :Expected '>' to complete generic argument list. How to fix the warning.
Xcode 7.1 OS X Yosemite
This is my code:
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo as! NSDictionary //warning
let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)
let keyboardInputViewFrame = self.finishView!.frame
let deltaY = keyboardBoundsRect.size.height
let animations: (()->Void) = {
self.finishView?.transform = CGAffineTransformMakeTranslation(0, -deltaY)
}
if duration > 0 {
} else {
animations()
}
}
NSNotification's userInfo property is already defined as a(n optional) dictionary.
So you don't need to cast it at all, just unwrap it.
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
...
}
}
all the rest of your code should work as is.
You are trying to force cast an optional to an NSDictionary. Try:
let userInfo = notification.userInfo! as NSDictionary
This worked for me.

MapKit adding multiple annotations from core data

This is my code. It loops for the number records in the database but only retrieves the first records lat and lon.
func fetch() {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "Mappoints")
let fetchResults = try! context.executeFetchRequest(freq) as! [NSManagedObject]
self.mapView.delegate = self
myData = fetchResults
myData.count
for _ in myData {
let data: NSManagedObject = myData[row]
lat = (data.valueForKey("latitude") as? String)!
lon = (data.valueForKey("longitude") as? String)!
let latNumb = (lat as NSString).doubleValue
let longNumb = (lon as NSString).doubleValue
let signLocation = CLLocationCoordinate2DMake(latNumb, longNumb)
addAnnotaion(signLocation)
}
}
I am sure I am missing something simple but just keep missing it.
Your loop looks weird. You say myData[row], but you don't seem to increment the row. If the row does not increment, the data variable will always be the same.
You could do for example for data in myData { ...
This is the code I ended up with to fix the issue.
func fetch() {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "Mappoints")
let fetchResults = try! context.executeFetchRequest(freq) as! [NSManagedObject]
self.mapView.delegate = self
myData = fetchResults
let ct = myData.count // Add this line
// Then changed the for statement from for _ in myData
// To the line below and now all map points show up.
for row in 0...ct-1 {
let data: NSManagedObject = myData[row]
lat = (data.valueForKey("latitude") as? String)!
lon = (data.valueForKey("longitude") as? String)!
let latNumb = (lat as NSString).doubleValue
let longNumb = (lon as NSString).doubleValue
let signLocation = CLLocationCoordinate2DMake(latNumb, longNumb)
addAnnotaion(signLocation)
}

download and save a PFFile (UIImage) to show in a UIImageView : SWIFT

I have a large class called "Goal" in parse. This class has multiple elements, one of which is a PFFile, that is always a UIImage.
When I perform my query for the "Goal" class, I cannot figure out how to take the PFFile, and change it to a UIImage for use.
var query = PFQuery(className:"Goal")
let currentUser = PFUser.currentUser()!.username
query.whereKey("creator", equalTo: currentUser!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects?.count) goals for the TableView.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
let goalType = object["type"] as! String
let goalPeriod = object["period"] as! String
let goalCategory = object["category"] as! String
let goalShortDescription = object["shortDescription"] as! String
let goalLongDescription = object["longDescription"] as! String
let goalPointvalue = object["pointValue"] as! Int
let goalSharedSetting = object["shared"] as! Bool
let goalAdoptionCount = object["adoptionCount"] as! Int
let goalIsComplete = object["isComplete"] as! Bool
let goalSuccessImageData = object["image"] as! PFFile
goalSuccessImageData.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
self.imageQuery = image
}
}
}
let goalSuccessImage : UIImage = self.imageQuery
let goalObjectID = object.objectId
let goalSpreadCount = object["spreadCount"] as! Int
let goalSpreadTotal = object["spreadTotal"] as! Int
let goalTotalCompletions = object["totalCompletions"] as! Int
let thisGoal = GoalModel(period: goalPeriod, type: goalType, category: goalCategory, shortDescription: goalShortDescription, longDescription: goalLongDescription, pointValue: goalPointvalue, shared: goalSharedSetting, adoptionCount: goalAdoptionCount, isComplete: goalIsComplete, successImage: goalSuccessImage, goalID: goalObjectID!, spreadCount: goalSpreadCount, spreadTotal: goalSpreadTotal, totalCompletions: goalTotalCompletions ) as GoalModel
any tips on how to modify the "success image" part? I added a space before and after to make it easier to find.
Thank you in advance!
I'm using this way in my projects, if it help's you :
func performSave(sender: UIBarButtonItem){
affichageActivityIndicator()
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos,0)) { () -> Void in
dispatch_async(dispatch_get_main_queue()){
if let updateObject = self.currentObject as PFObject? {
let imageData = UIImageJPEGRepresentation(imageToSave, 0.1)
let imageFile = PFFile(name:"image.png", data:imageData)
updateObject["imageFile"] = imageFile
// Save the data back to the server in a background task
updateObject.saveInBackgroundWithBlock{(success: Bool, error: NSError!) -> Void in
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if success == false {
println("Error")
}
}
}
}
}
}