How do I view the actual string variable of a variable/constant? - debugging

Environment: Debugging Parse return object within Xcode 6.1
I can see the text within the object structure but can't adequately view its assigned variable.
Here's the code:
func retrieveAllMediaFromParse() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let myData:PFQuery = PFQuery(className: kParseMedia)
myData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!)->Void in
if !(error != nil){
// for object in objects {
let myObject = objects[0] as PFObject
let format = myObject.objectForKey("format") as String
let access = myObject.objectForKey("access") as String
let media = myObject.objectForKey("media") as String
let owner = myObject.objectForKey("owner") as String
let text = myObject.objectForKey("text") as String
let thumbNail = myObject.objectForKey("thumbnail") as NSString
}
}
});
}
let text = myObject.objectForKey("text") as String
When I take the 'po' of the command string I get the correct interpretation:
However, when I do the same for the assigned variable (constant), I get the following:
How do I view the variable/constant to display the actual string?

When program is paused in the debugger, you can find the values of PFObject fields by going to the estimatedData line in the debugger.

Related

Swift2 access component with string-name

Im more familiar with ActionScript3 and see many similarities in Swift2, kind of why i am trying out basic coding in Swift2 and Xcode.
Here's my example:
#IBOutlet weak var b1CurrSpeed: NSTextField!
I want to store b1CurrSpeed as a string so i could access the actual textfield component to set its default value when application is loaded.
I'm aiming for Swift2 for osx apps.
Here is a fictional example, not related to any actual code:
var tf:NSTextField = this.getItem("b1CurrSpeed");
tf.stringValue = "Hello world";
Reason to this approach is following...
I would like to store textfield value in NSUserDefaults, the key for defaults would be name of that textfield. So when looping thru the defaults, i would like to get key as string and when ive got that i'd have access to actual component to set its stringvalue property.
Tho, is that good approach in Swift / xCode ?
If you want to create a function for it, do someting like this:
func getStringForKey(key: String) -> String {
guard let result = NSUserDefaults.standardUserDefaults().objectForKey(key) as! String else { return "" }
return result
}
You can set the TextFields value with myTextField.text
Swift's Mirror type can get you close to it but it is limited to NSObject subclasses, can only access stored properties and is read-only.
Yet, there are ways around these limitations if your requirements will allow.
For example, here's an extension that will save and restore defaults values for all UITextfields on a view controller using the name you gave to each IBOutlet.
extension UIViewController
{
func loadDefaults(userDefaults: NSUserDefaults)
{
for prop in Mirror(reflecting:self).children
{
// add variants for each type/property you want to support
if let field = prop.value as? UITextField,
let name = prop.label
{
if let defaultValue = userDefaults.objectForKey(name) as? String
{ field.text = defaultValue }
}
}
}
func saveDefaults(userDefaults: NSUserDefaults)
{
for prop in Mirror(reflecting:self).children
{
if let field = prop.value as? UITextField,
let name = prop.label
{
if let currentValue = field.text
{ userDefaults.setObject(currentValue, forKey: name) }
}
}
}
}

Get Contact Image Data using ABPersonCopyImageData

I'm trying to get the contact details out of the address book on the Mac. I can get the first name and last name fields etc, but I'm struggling with the syntax for ABPersonCopyImageData.
Now according to the documentation ABPersonCopyImageData takes a single parameter of type ABPerson.
Here is my code:
import AddressBook
let thisPerson : ABPerson
let addressBook = ABAddressBook.sharedAddressBook()
rec = addressBook.recordForUniqueId("0005A360-327F-4E12-BBB9-24A842497E12:ABPerson")
let firstName = rec.valueForProperty(kABFirstNameProperty) as! String
let lastName = rec.valueForProperty(kABLastNameProperty) as! String
println("\(firstName) \(lastName)")
let contactImage = ABPersonCopyImageData(thisPerson)
The last line stops the compiler with an error: Cannot invoke 'ABPersonCopyImageData' with an argument list of type (ABPerson). As far as I can tell thisPerson is of type ABPerson. What is going wrong?
I found out how to do this in ElCapitan:
import Contacts
func getContactImage(name:String) -> NSImage?
{
let store = CNContactStore()
do
{
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(name), keysToFetch:[CNContactImageDataKey])
if contacts.count > 0
{
if let image = contacts[0].imageData
{
return NSImage.init(data: image)
}
}
}
catch
{
}
return nil
}

casting arrays returned by NSURL.getResourceValue in Swift

I have spent all day trying to get useable results from NSURL.getResourceValue for NSURLTagNamesKey in swift. The function should take the path name as a string and return an array of strings for the user tags. I have a version of this that works in Objective C, but have not been able to re-write in Swift.
This is the current version of the code:
func listTags(filePath:String)->[String]{
//convert path string to NSURL
let theURL : NSURL = NSURL.fileURLWithPath(filePath)!
//get tags for NSURL -- should be NSArray of NSStrings hiding in an AnyObject?
var tags : AnyObject?
var anyError: NSError?
tags = theURL.getResourceValue(&tags, forKey:NSURLTagNamesKey, error: &anyError)
//unwrap tags object? This part never works
let tagArray = tags! as [AnyObject]
//insert every item in tag array into results array as a String
var results = [String]()
for object in tagArray{
results.append(object as String)
}
return results
}
The code will compile but breaks when it tries to convert the AnyObject to any other type. I have tried every combination I can think of -- [AnyObject], [String], NSArray, with/without exclamation points and question marks.
Am on verge of giving up on Swift.
You're going to kick yourself...
The method getResourceValue:forKey:error returns a value - a Bool, indicating whether the container you passed in as the first argument has been populated. Unfortunately you're assigning the value of this boolean to tags - your container! - which means that whatever was passed in to this container by Cocoa is immediately over-written. This worked for me...
var tags : AnyObject?
var anyError: NSError?
var success = theURL.getResourceValue(&tags,
forKey:NSURLTagNamesKey,
error: &anyError)
if success {
println("container contents \(tags as [String])") // -> [AutoLayout, Swift]
}
With Swift 2, getResourceValue(:forKey:) returns () throws, i.e., void type which throws errors, so the answer above will no longer work. It needs to be wrapped in a do {try} catch{} construction without the anyError variable:
do {
try theURL.getResourceValue(&tags, forKey: NSURLTagNamesKey)
return tags as! [String]
} catch {
// process the error here
}

Type 'String' does not confirm to protocol 'NSCopying' with NSDictionary

I'm trying to use the Google Geolocation api, within Xcode using Swift, to allow the conversion of text address to latitude and longitude.
http://maps.googleapis.com/maps/api/geocode/json?address=washington%20dc is an example JSON call.
the error: Type 'String' does not confirm to protocol 'NSCopying', appears when trying to access elements within the dictionary of results.
#IBAction func submitButtonPressed(sender: AnyObject) {
var address = addAddressTextField.text
var escapedAddress = address.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlpath = "http://maps.googleapis.com/maps/api/geocode/json?address=" + escapedAddress!
println(urlpath)
let url = NSURL(string: urlpath)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { (data, responce, error) -> Void in
if error != nil{
println("there is an error")
}
var err : NSError?
var results = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if err != nil{
println("there is a second error")
}
let formattedAddress: AnyObject! = results["results"]![0]!["formatted_address"]!
let latitude: AnyObject! = results["results"]![0]!["geometry"]!["location"]!["lat"]!
let longitude: AnyObject! = results["results"]![0]!["geometry"]!["location"]!["lng"]!
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.fullAddressLabel.text = "\(formattedAddress)"
self.latLabel.text = "\(latitude)"
self.longLabel.text = "\(longitude)"
})
})
jsonQuery.resume()
}
The error only occurs on the lines:
let latitude: AnyObject! = results["results"]![0]!["geometry"]!["location"]!["lat"]!
let longitude: AnyObject! = results["results"]![0]!["geometry"]!["location"]!["lng"]!
and the let formattedAddress: AnyObject! = ... line works perfectly.
I have tried defining the variables as Strings and NSStrings and using '... as String' within the definition but to no luck.
The problem is not the returned variables but the keys. NSDictionary expects its keys to conform to NSCopying and presumably a Swift string literal does not.
I would try to figure out exactly which access is causing the issue by not trying to get the whole thing in one line (your program will crash btw if you get an unexpected response because you assume all the keys exist in all the dictionaries).
On the assumption that you know you have at least one result and you have put it in a variable called results0 (this comes from external data so you must check it), I'd do this
var latitude: Double?
var longitude: Double?
if let geometry = results0["geometry"] as? NSDictionary
{
if let location = geometry["location"] as? NSDictionary
{
latitude = location["lat"] as? Double
longitude = location["lon"] as? Double
}
}
Either you will get the same error on one of the lines accessing the dictionaries or something else may go wrong. You might have to fiddle about with the casting too to get it right (I haven't even tried to compile the above code). But the point is that now the code is spread out over several lines, it should be much easier to isolate the error.
I ran into the same issue with json try this
let latitude = results["results"]![0]!["geometry"]!!["location"]!!["lat"]!! as String
let longitude = results["results"]![0]!["geometry"]!!["location"]!!["lng"]!! as String
note, This does not include any error checking at all also if the labels you are updating are standard labels you can just do
mylabel.stringValue = latitude
or something like that :P
Here is a direct copy of how i phrase my json objects without error checking
var FilePath = object["main"]![row]!["Paths:"]!!["FilePath:"]!! as String

How to write an array to NSUserDefaults in Swift

I'm trying to write the contents of an array to NSUserDefaults, but the app hangs when I call setObject:withKey with the array as the object. Here's the relevant code:
class Contact: NSObject {
var name:String = ""
var number:String = ""
}
var contacts:[Contact]?
contacts = [Contact]()
let contact = Contact()
contact.name = "Joe"
contact.number = "123-4567"
contacts.append(contact)
let defaults = NSUserDefaults.standardUserDefaults()
// Never returns from this when I step over it in the debugger
defaults.setObject(contacts, forKey: "contacts")
defaults.synchronize()
What am I doing wrong?
Not sure exactly how it works on OS X, but if it's anything like it is on iOS, you can't store custom classes in NSUserDefaults, only Strings, Ints, Data, etc... so one work around is to convert your array to NSData and then store it as data and when you retrieve it cast it to be [Contact]. It may look something like this:
let data = NSKeyedArchiver.archivedDataWithRootObject(contacts)
let defaults = NSUserDefaults.standardUserDefaults()
// Storing the data
defaults.setObject(data, forKey: "contacts")
// Retrieving the data
if let data = defaults.objectForKey("contacts") as? NSData {
if let contacts = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [Contact] {
// Now you have your contacts
}
}
However, if you are storing large amounts of data, you should probably consider other forms of data management.

Resources