How can I call an Objective C method from a Swift 3 app? - ios

I'm trying to use wit-ai SDK, written in Objective C in a Swift 3 app.
The documentation in the GitHub repo explains how to use a particular Objective C method to send text string to Wit, but I'm not sure how to use that method in my app, and could use some guidance.
InterpretString
Sends an NSString to wit.ai for interpretation. Same as sending a voice input, but with text.
- (void) interpretString: (NSString *) string customData:(id)customData;
This is what I have so far:
import UIKit
class ViewController: UIViewController, WitDelegate {
/**
* Called when the Wit request is completed.
* param outcomes a NSDictionary of outcomes returned by the Wit API. Outcomes are ordered by confidence, highest first. Each outcome contains (at least) the following keys:
* intent, entities[], confidence, _text. For more information please refer to our online documentation: https://wit.ai/docs/http/20141022#get-intent-via-text-link
*
* param messageId the message id returned by the api
* param customData any data attached when starting the request. See [Wit sharedInstance toggleCaptureVoiceIntent:... (id)customData] and [[Wit sharedInstance] start:... (id)customData];
* param error Nil if no error occurred during processing
*/
public func witDidGraspIntent(_ outcomes: [Any]!, messageId: String!, customData: Any!, error e: Error!) {
if ((e) != nil) {
print("\(e.localizedDescription)")
return
}
let outcomes : NSArray = outcomes! as NSArray
let firstOutcome : NSDictionary = outcomes.object(at: 0) as! NSDictionary
let intent : String = firstOutcome.object(forKey: "intent") as! String
labelView!.text = intent
labelView!.sizeToFit()
}
var labelView : UILabel?
var witButton : WITMicButton?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// set the WitDelegate object
Wit.sharedInstance().delegate = self
// create the button
let screen : CGRect = UIScreen.main.bounds
let w : CGFloat = 100
let rect : CGRect = CGRect(x: screen.size.width/2 - w/2, y: 60, width: w, height: 100)
witButton = WITMicButton(frame: rect)
self.view.addSubview(witButton!)
// create the label
labelView = UILabel(frame: CGRect(x: 0, y: 200, width: screen.size.width, height: 50))
labelView!.textAlignment = .center
labelView!.text = "intent"
labelView!.textColor = UIColor.black
labelView!.sizeToFit()
self.view.addSubview(labelView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

textLabel.text in UIViewController comes up nil while assigning string from NSObject

I'm an iOS and programming noob so I apologize for any bad phrasing or mistakes.
I'm parsing quotes from an API for my app which displays it on a textLabel each time a UIButton is clicked. In order to keep the string from going off the textLabel or be resized to an unreadable font, I'm trying to request a new quote if the string character count is too high by calling a function in my NSObject. I set up a NSObject to do the refetching but whenever I try to reassign the the string to the textLabel.text from the NSObject or try to send the string back to the ViewController the qouteLabel.text comes back nil
Here is my viewcontroller where I'm making the initial request for the quote
import UIKit
import Alamofire
class RSQuotesViewController: RSViewController {
var ronImageView: UIImageView!
var quoteLabel = UILabel!()
override func loadView() {
let frame = UIScreen.mainScreen().bounds
let view = UIView(frame: frame)
view.backgroundColor = UIColor.grayColor()
ronImageView = UIImageView(frame: CGRectMake(frame.width/2-160, frame.height-600, 320, 600))
let ron = "ron.png"
let ronImage = UIImage(named: ron)
ronImageView.image = ronImage
view.addSubview(ronImageView);
let labelWidth = ronImageView.frame.width/2
let quoteLabelX = labelWidth-40
quoteLabel = UILabel(frame: CGRect(x: quoteLabelX, y: ronImageView.frame.height/4+15, width: labelWidth, height: 160))
quoteLabel.textAlignment = .Center
quoteLabel.text = "Click to Start"
quoteLabel.shadowColor = UIColor.grayColor()
quoteLabel.adjustsFontSizeToFitWidth = true
quoteLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
quoteLabel.numberOfLines = 0
view.addSubview(quoteLabel)
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
let frame = UIScreen.mainScreen().bounds
let getQuote = UIButton(frame: CGRect(x: 0, y: 0, width: frame.size.width+50, height: frame.size.height))
getQuote.backgroundColor = UIColor.clearColor()
getQuote.setTitle("", forState: UIControlState.Normal)
getQuote.addTarget(self, action: #selector(RSQuotesViewController.getQuote(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(getQuote)
}
// Gets quote when button is pressed
func getQuote(sender: UIButton){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
let quoteDict = RSQoute()
// if quote is too large get another one
if (JSON[0].characters.count > 120){
print("greater than 120")
quoteDict.fetchQuote()
} else {
self.quoteLabel.text = JSON[0]
}
}
}
}
This is my model where I'm trying to reassign the quoteLabel.text and getting nil
import UIKit
import Alamofire
class RSQoute: NSObject {
var newQuote = String()
// fetchs new quote if quote is too large
func fetchQuote(){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
self.newQuote = JSON[0]
if (self.newQuote.characters.count > 120) {
print("Try Again: ---->\(self.newQuote)")
return self.fetchQuote()
} else {
let quoteVC = RSQuotesViewController()
print("Retry was less than 120: ---->\(self.newQuote)")
print("quoteLabelText: ---->\(RSQuotesViewController().quoteLabel.text)")// comes back nil
RSQuotesViewController().quoteLabel.text = self.newQuote
}
}
}
}
}
Please let me know if there something I'm missing or an easier/better way of trying to fetch a new quote from the API :)
In your function fetchQuote(), you set quoteVC as a new instantiation of RSQuotesViewController() with let quoteVC = RSQuotesViewController(). Instead you should be setting the quoteLabel.text for the applications instance of RSQuotesViewController(). You are also making two API requests. Once inside the fetchQuote() function for RSQuotesViewController and once inside your fetchQuote() function for RSQuotes
I think what you are looking for would involve closures. Try this out for your fetchQuote() function in your RSQuotes class
func fetchQuote(completion: (result:String)){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
self.newQuote = JSON[0]
if (self.newQuote.characters.count > 120) {
print("Try Again: ---->\(self.newQuote)")
completion(result: self.newQuote)
} else {
print("Retry was less than 120: ---->\(self.newQuote)")
print("quoteLabelText: ---->\(RSQuotesViewController().quoteLabel.text)")// comes back nil
completion(result: self.newQuote)
}
}
Then, I would have a setQuote function RSQuotesViewController where you could just do something like this
func setQuote() {
let quoteObj = RSQuote()
quoteObj.fetchQuote() {
result in
quoteLabel.text = result
}
}
I would take a look at some posts related to swift closures and also check out. http://goshdarnclosuresyntax.com/
On a side note, I'm not sure if you were planning to manipulate the quoteString within your RSQuote class. If not, it might be better for fetchQuote() to be a static func. This way you can just call it without initializing the object in RSQuoteViewController. It'd be something like RSQuote.fetchQuote()

How do i use mapbox's new MGLOfflinePackDelegate correctly?

I'm creating an app which needs an offline map. I'm testing with MapBox, which supports offline maps since today (yay!). The code I have now seems to work for downloading the map, but the delegate to report on progress never triggers, and I don't have a clue why this is.
I have this class for my mapView:
import UIKit
import Mapbox
class MapController: UIViewController, MGLMapViewDelegate, UIPopoverPresentationControllerDelegate {
#IBOutlet var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
downloadIfNeeded()
mapView.maximumZoomLevel = 18
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func downloadIfNeeded() {
MGLOfflineStorage.sharedOfflineStorage().getPacksWithCompletionHandler { (packs, error) in guard error == nil else {
return
}
for pack in packs {
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
if userInfo["name"] == "London" {
// allready downloaded
return
}
}
// define the download region
let sw = CLLocationCoordinate2DMake(51.212120, 4.415906)
let ne = CLLocationCoordinate2DMake(51.223781, 4.442401)
let bounds = MGLCoordinateBounds(sw: sw, ne: ne)
let region = MGLTilePyramidOfflineRegion(styleURL: MGLStyle.streetsStyleURL(), bounds: bounds, fromZoomLevel: 10, toZoomLevel: 12)
let userInfo = ["name": "London"]
let context = NSKeyedArchiver.archivedDataWithRootObject(userInfo)
MGLOfflineStorage.sharedOfflineStorage().addPackForRegion(region, withContext: context) { (pack, error) in
guard error == nil else {
return
}
// create popup window with delegate
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let downloadProgress: MapDownloadController = storyboard.instantiateViewControllerWithIdentifier("MapDownloadController") as! MapDownloadController
downloadProgress.modalPresentationStyle = .Popover
downloadProgress.preferredContentSize = CGSizeMake(300, 150)
let popoverMapDownloadController = downloadProgress.popoverPresentationController
popoverMapDownloadController?.permittedArrowDirections = .Any
popoverMapDownloadController?.delegate = self
popoverMapDownloadController?.sourceView = self.mapView
popoverMapDownloadController?.sourceRect = CGRect(x: self.mapView.frame.midX, y: self.mapView.frame.midY, width: 1, height: 1)
self.presentViewController(downloadProgress, animated: true, completion: nil)
// set popup as delegate <----
pack!.delegate = downloadProgress
// start downloading
pack!.resume()
}
}
}
}
And the MapDownloadController is a View which is displayed as popup (see code above) and has the MGLOfflinePackDelegate:
import UIKit
import Mapbox
class MapDownloadController: UIViewController, MGLOfflinePackDelegate {
#IBOutlet var progress: UIProgressView!
#IBOutlet var progressText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func offlinePack(pack: MGLOfflinePack, progressDidChange progress: MGLOfflinePackProgress) {
// this function is never called, but why? <----
let completed = progress.countOfResourcesCompleted
let expected = progress.countOfResourcesExpected
let bytes = progress.countOfBytesCompleted
let MB = bytes / 1024
let str: String = "\(completed)/\(expected) voltooid (\(MB)MB)"
progressText.text = str
self.progress.setProgress(Float(completed) / Float(expected), animated: true)
}
func offlinePack(pack: MGLOfflinePack, didReceiveError error: NSError) {
// neither is this one... <----
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
let strError = error.localizedFailureReason
}
func offlinePack(pack: MGLOfflinePack, didReceiveMaximumAllowedMapboxTiles maximumCount: UInt64) {
// .. or this one <----
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
}
}
This is all pretty much taken from the documentation, so why are the delegate's functions (func offlinePack) never called? I did test with breakpoints so i am sure it is not. Still, the popup is shown and the region gets downloaded. (Checked with observing network traffic and with other code which lists the offline packs.)
Here’s an extremely simple implementation of Minh’s answer, using the current v3.2.0b1 example code. Expect this answer to become outdated quickly, as we’re still working on the v3.2.0 release.
import UIKit
import Mapbox
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, MGLOfflinePackDelegate {
#IBOutlet var mapView: MGLMapView!
// Array of offline packs for the delegate work around (and your UI, potentially)
var offlinePacks = [MGLOfflinePack]()
override func viewDidLoad() {
super.viewDidLoad()
mapView.maximumZoomLevel = 2
downloadOffline()
}
func downloadOffline() {
// Create a region that includes the current viewport and any tiles needed to view it when zoomed further in.
let region = MGLTilePyramidOfflineRegion(styleURL: mapView.styleURL, bounds: mapView.visibleCoordinateBounds, fromZoomLevel: mapView.zoomLevel, toZoomLevel: mapView.maximumZoomLevel)
// Store some data for identification purposes alongside the downloaded resources.
let userInfo = ["name": "My Offline Pack"]
let context = NSKeyedArchiver.archivedDataWithRootObject(userInfo)
// Create and register an offline pack with the shared offline storage object.
MGLOfflineStorage.sharedOfflineStorage().addPackForRegion(region, withContext: context) { (pack, error) in
guard error == nil else {
print("The pack couldn’t be created for some reason.")
return
}
// Set the pack’s delegate (assuming self conforms to the MGLOfflinePackDelegate protocol).
pack!.delegate = self
// Start downloading.
pack!.resume()
// Retain reference to pack to work around it being lost and not sending delegate messages
self.offlinePacks.append(pack!)
}
}
func offlinePack(pack: MGLOfflinePack, progressDidChange progress: MGLOfflinePackProgress) {
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
let completed = progress.countOfResourcesCompleted
let expected = progress.countOfResourcesExpected
print("Offline pack “\(userInfo["name"])” has downloaded \(completed) of \(expected) resources.")
}
func offlinePack(pack: MGLOfflinePack, didReceiveError error: NSError) {
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
print("Offline pack “\(userInfo["name"])” received error: \(error.localizedFailureReason)")
}
func offlinePack(pack: MGLOfflinePack, didReceiveMaximumAllowedMapboxTiles maximumCount: UInt64) {
let userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context) as! [String: String]
print("Offline pack “\(userInfo["name"])” reached limit of \(maximumCount) tiles.")
}
}
(Cross-posted from this GitHub issue.)
This is a bug in the SDK. The workaround is for the completion handler to assign the passed-in MGLOfflinePack object to an ivar or other strong reference in the surrounding MapDownloadController class (example).

Extracting string values from this json in IOS swift

I am using swiftyjson to extract the json but i cant able to extract specification json that is label and value inside specification. I need it quickly.
((
{
code = "NPR 1515";
description = "With its enhanced power and performance the NPR1515 provides a genuine \"workhorse\" that will take almost anything in its stride.
\nThe full 1500 Watt motor unit packs almost 50% more power than normally required and this power is transmitted to the floor through our long established 150rpm, oil filled, low load, planetary gearbox .
\nThe big advantage of excess power is to provide scope for many additional tasks where the excess power is both advantageous and needed.";
id = 16;
name = "Scrubbers & Polisher - NPR 1515";
specification = "[{\"label\":\"Model No\",\"value\":\"PR 1515\\t\\t\\r\"},{\"label\":\"\\nMotor\",\"value\":\"500W\\t\\r\"},{\"label\":\"\\nPad\",\"value\":\"00mm\\t\\r\"},{\"label\":\"\\nPower\",\"value\":\"30V AC 50Hz\\r\"},{\"label\":\"\\nBrush\",\"value\":\"50mm\\t\\r\"},{\"label\":\"\\nSpeed\",\"value\":\"50 rpm\\r\"},{\"label\":\"\\nVacuum\",\"value\":\"T130\\t\\r\"},{\"label\":\"\\nRange\",\"value\":\"2m\\t\\t\\r\"},{\"label\":\"\\nWeight\",\"value\":\"0 Kgs\\t\\r\"},{\"label\":\"\\nSize\",\"value\":\"185 x 580x 450mm\"}]";
"video_url" = "<null>";
}
))
let json2 = JSON(data3!)
for (index, object) in json2 {
let name = object["name"].stringValue
let code = object["code"].stringValue
let description = object["description"].stringValue
let specification = object["specification"].stringValue
does not run this part.
let json3 = JSON(specification)
for (index, object3) in json3 {
println("in this loop")
if let specification2 = object3["label"].string {
println(specification2)
}
else {
println(object3["label"].error)
}
let specification3 = object3["value"].stringValue
println(specification3)
}
ok now it is working but label are not able to print in label they are working in println but if i do this value works but not label.
let str = self.labelArray[i]
let label8 = UILabel(frame: CGRectMake(2, 0, 0, 0))
label8.backgroundColor = UIColor.whiteColor()
label8.textColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
label8.frame = CGRect(x: 10, y: setheight , width: screenWidth/2, height: 25)
label8.textAlignment = NSTextAlignment.Left
label8.text = str
self.scrollview_add.addSubview(label8)
var label7 = UILabel(frame: CGRectMake(2, 0, 0, 0))
label7.backgroundColor = UIColor.whiteColor()
label7.textColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
label7.frame = CGRect(x: screenWidth/2, y: setheight , width: screenWidth/2, height: 25)
label7.textAlignment = NSTextAlignment.Right
label7.text = self.valueArray[i]
self.scrollview_add.addSubview(label7)
setheight += 25
As posted in your question value of specification is a string so first you have to convert it into NSData then convert it into json:
if let jsonData = specification.dataUsingEncoding(NSUTF8StringEncoding){
let json = JSON(data:jsonData)
for (index, object3) in json {
println("in this loop")
if let specification2 = object3["label"].string {
println(specification2)
}
else {
println(object3["label"].error)
}
let specification3 = object3["value"].stringValue
println(specification3)
}
}
According to the data structure you posted, the value inside "specification" is a string, not a JSON object. There're two ways to do it properly:
1, format the JSON data correctly, i.e, instead of a string, the "specification" should also be a JSON object (array with dictionaries), depending on where you get this JSON data, you need to modify the generation logic of this data to accomplish this.
2, you can manually parse the specification using JSON parser, for example:
let error: NSErrorPointer = nil;
let specificationString = "[{\"label\":\"Model No\",\"value\":\"PR 1515\\t\\t\\r\"},{\"label\":\"\\nMotor\",\"value\":\"500W\\t\\r\"},{\"label\":\"\\nPad\",\"value\":\"00mm\\t\\r\"},{\"label\":\"\\nPower\",\"value\":\"30V AC 50Hz\\r\"},{\"label\":\"\\nBrush\",\"value\":\"50mm\\t\\r\"},{\"label\":\"\\nSpeed\",\"value\":\"50 rpm\\r\"},{\"label\":\"\\nVacuum\",\"value\":\"T130\\t\\r\"},{\"label\":\"\\nRange\",\"value\":\"2m\\t\\t\\r\"},{\"label\":\"\\nWeight\",\"value\":\"0 Kgs\\t\\r\"},{\"label\":\"\\nSize\",\"value\":\"185 x 580x 450mm\"}]"
if let specificationData = specificationString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) {
let specificationObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(specificationData, options: .MutableContainers, error: error)
//Now you have a correct Array, do whatever you want with it.
}

Cannot index empty buffer in Swift

I'm following along with Rob Percival's tutorial on Udemy and my app crashes with a fatal error: cannot index empty buffer.
The error happens on the following line in the ViewdidLoad method:
userImage.image = UIImage(data: self.userImages[0])
Using println(self.userImages) right above that erroneous line I found out the array is empty. The console outputs a []. This must be why it's crashing. Correct?
My Parse.com dashboard shows that all my users have images. I'm not sure why the array would be empty. Any ideas?
import UIKit
class SwipeViewController: UIViewController {
var xFromCenter: CGFloat = 0
var usernames = [String]()
var userImages = [NSData]()
var currentUser = 0
override func viewDidLoad() {
super.viewDidLoad()
println(PFUser.currentUser()["first_name"])
PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint: PFGeoPoint!, error: NSError!) -> Void in
println(error)
if error == nil {
println(geopoint)
var user = PFUser.currentUser()
user["location"] = geopoint
var query = PFUser.query()
query.whereKey("location", nearGeoPoint:geopoint)
query.limit = 10
query.findObjectsInBackgroundWithBlock({ (users, error) -> Void in
for user in users {
var gender1 = user["gender"] as? NSString
var gender2 = PFUser.currentUser()["interestedIn"] as? NSString
if gender1 == gender2 && PFUser.currentUser().username != user.username {
self.usernames.append(user.username)
// Update - chaned as to as!
self.userImages.append(user["image"] as NSData)
}
}
var userImage: UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
println("images:")
println(userImage)
println(self.userImages)
userImage.image = UIImage(data: self.userImages[0])
userImage.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(userImage)
var gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
userImage.addGestureRecognizer(gesture)
userImage.userInteractionEnabled = true
})
user.save()
}
}
}
func wasDragged(gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
var label = gesture.view!
xFromCenter += translation.x
var scale = min(100 / abs(xFromCenter), 1)
label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)
gesture.setTranslation(CGPointZero, inView: self.view)
var rotation:CGAffineTransform = CGAffineTransformMakeRotation(xFromCenter / 200)
var stretch:CGAffineTransform = CGAffineTransformScale(rotation, scale, scale)
label.transform = stretch
if label.center.x < 100 {
println("Not Chosen")
} else if label.center.x > self.view.bounds.width - 100 {
println("Chosen")
}
if gesture.state == UIGestureRecognizerState.Ended {
self.currentUser++
label.removeFromSuperview()
if self.currentUser < self.userImages.count {
var userImage: UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
userImage.image = UIImage(data: self.userImages[self.currentUser])
userImage.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(userImage)
var gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
userImage.addGestureRecognizer(gesture)
userImage.userInteractionEnabled = true
xFromCenter = 0
} else {
println("No more users")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Looks like a data issue. The problem must comes from the loop just above the failing line.
Conditions are not met, and so no image is pushed into the array. Reasons can be:
You only have 1 user (your current user)
There is no other users with a gender that meets the "interested in" condition of your current user
Your users (current or not) doesn't have the "gender" or "interestedIn" attributes set
The best way is to put breakpoint (or log) inside your loop in order to determine users gender and "interestedIn" attributes and see if there is some matches.

How to get a value from NSValue in Swift?

Here's what I've tried so far
func onKeyboardRaise(notification: NSNotification) {
var notificationData = notification.userInfo
var duration = notificationData[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
var frame = notificationData[UIKeyboardFrameBeginUserInfoKey]! as NSValue
var frameValue :UnsafePointer<(CGRect)> = nil;
frame.getValue(frameValue)
}
But I always seem to crash at frame.getValue(frameValue).
It's a little bit confusing because the documentation for UIKeyboardFrameBeginUserInfoKey says it returns a CGRect object, but when I log frame in the console, it states something like NSRect {{x, y}, {w, h}}.
getValue() must be called with a pointer to an (initialized) variable
of the appropriate size:
var frameValue = CGRect(x: 0, y: 0, width: 0, height: 0)
frame.getValue(&frameValue)
But it is simpler to use the convenience method:
let frameValue = frame.CGRectValue() // Swift 1, 2
let frameValue = frame.cgRectValue() // Swift 3

Resources