XCode Debugger viewing variables - ios

I'm attempting to step through some code where I have a variable:
view.annotation.coordinate.latitude
Looking at the variable in XCode I see:
Since this is an instance of a UIView there are structures that I'm unfamiliar with (e.g subViewCache). Is there some document explaining what these other structures are for? Where can I drill down to to find the annotation object (view.annotation) that I want to view?

By convention, anything start with underscore means it is private variable, which (normally) won't be documented and subject to change. The best you can do is guess the meaning from the name and hope there is something called _annotation.
However, you can use lldb command po view.annotation to ask debugger to print that property for you

Or you could create a temporary variable where you are trying to debug:
CLLocationCoordinate2D tempCoord = annotation.coordinate;
You should be able to see the long and lat from tempCoord if you hover over it while debugging.

Related

Swift instance members & functions calls, could someone please clarify?

I'm practicing functions in an Xcode project (not playground, if that matters) and when I set the constant's function name to the above function's name, Xcode won't let me call it.
In other functions I've done the same thing I haven't had this error, I don't know why it's being triggered. I've tried putting the "let fun" constant into it's own struct but that just throws up a different error.
Your let fun = playstation(... is code. It is an executable command. Code consisting of an executable command cannot just live anywhere. It cannot exist just free-floating the way you have it. It must live in a method. For example you could put it inside your viewDidLoad method.
I am sure you know this, but I would like to say that learning the basics/fundamentals of the Swift language is really good if you use playgrounds for that or any other online IDEs which support Swift language.
Swift Playgrounds Experience is very, very different than what an iOS dev has to do later, i.e. when it actually comes to building an app.
Of course in app making, the Swift language is used, and one uses the knowledge he practiced using playgrounds. BUT!
When it comes to storyboards, ViewControllers, etc., one has to understand that this time, it is all about OOP (classes and structs).
It is about managing views, loading them, displaying UIView objects, implementing logic, saving/loading data...
So as people mentioned above, here you are creating an instance method and trying to use it as a regular function inside that Class.
To be able to use this function as you expect, you have to create an object/instance of this class, then you can call this function.
In this picture, you may see that using a static keyword also might be a solution. static func(){} means that this function belongs to the class itself. You access that func by using the full name of the type/class.
You cannot call an instance method to initialize this same exact instance property, so you will need to convert to a lazy var like so:
lazy var fun = playStation(game: "Monsters")
This way, the playStation(game: "Monsters") call will be triggered lazily, in the exact moment when the fun property will be called for the first time. This can be very useful, especially when performing more intensive tasks. The quirk with using lazy keyword is that it can only be used with var, which means the property can be mutated or set again - we lose immutability of let constants.
But maybe you do not want to store the fun value as an instance property and only want to get it once? You could then move it into the viewDidLoad implementation, so the fun is no longer an instance property but a local one, available only within the viewDidLoad method scope.
override func viewDidLoad() {
super.viewDidLoad()
let fun = playStation(game: "Monsters")
// do what you want with the `fun` value.
// maybe set it as text to some label?
myLabel.text = fun
}
You can read more about Lazy Initialization in this blogpost, also make sure to read the official documentation.

Easy way to check reference count for an object

In an iOS project using Xcode and Swift, is there a simple way to check the reference count for an object? Automatic Reference Counting (ARC) usually handles memory management for us, but I'm having trouble tracking down a memory leak in my app. I want an easy way to examine the reference count of any object at any given point in the app's lifecycle. Is there a simple way to do that?
I found an easy way using a command for the LLDB debugger console. If anyone knows another good way to examine the reference count of an object, feel free to leave another answer.
Steps
First, use a breakpoint to pause execution at a point in your code when you want to check the reference count of some object. The console will display (lldb). Click next to it to insert your cursor.
Type language swift refcount array (where "array" is the name of the object that I wanted to examine; substitute the name of your own object instead), and then press Return. The console will output the object's reference count in this format:
refcount data: (strong = 1, unowned = 0, weak = 0)
More About Debugger Commands
Enter help to see more console commands. There are a lot of them. I've been using Xcode for five years and I only just learned of them today. This can be a super useful debugging tool. FYI, here are a few of the most useful ones:
po self
Stands for "print-object". Prints a nice description of an object. I'm just using "self" here as an example. You can substitute the name of your own object.
p self
The "print" command. As print-object, but more verbose. Using "self" here as an example again.
step
Advance one line of code.
continue
Resume program execution.
expr
The "expression" command. Lets you enter Swift code to modify variables. For example, add some data to an array object:
expr array.insert(343, at: 0)
You can even change UI elements this way, as in this example:
expr self.view.tintColor = UIColor.red
Just type po CFGetRetainCount(someVariable) on lldb
Source: https://developer.apple.com/documentation/corefoundation/1521288-cfgetretaincount

How to read Apple developer document

I have been very confuse with how to read apple developer document. It has restrict me to learn deeper into ios coding environment beyond what Youtuber have taught me. One of the example is this
Declaration:
func run(_ configuration: ARConfiguration,
options: ARSession.RunOptions = [])
From this apple link
https://developer.apple.com/documentation/arkit/arsession/2875735-run
It suppose to get me started on how to start ARKit but i cant understand what it wants me to do.
What is that line of code for?
How do i use that line of code?
I dont get it at all. I cant find any tutorial that explain apple documentation. Someone please explain it to me i need to know this
A declaration example just shows the parameters of the method (their types). Bellow the declaration, it is explained where that method can be used, in more detail. You asked about the purpose of the declaration; after the name of the method (the first line) it is written that it
Starts AR processing for the session with the specified configuration and options.
So, this part answers to your first question. What's going after the example of a method declaration, answers to your second question.
Always try to read the Discussion section of the documentation and don't miss it. It will help you to have a basic idea what the method, property, etc. is doing.
However, there are some cases when there is almost no information is provided except a declaration. But those cases are quite rare.
The line of code is for the following as mentioned in the documentation:
Starts AR processing for the session with the specified configuration and options.
To answer your question of how you may use that in context we can look at the Overview for ARSession.
Every AR experience built with ARKit requires a single ARSession object. If you use an ARSCNView or ARSKView object to easily build the visual part of your AR experience, the view object includes an ARSession instance. If you build your own renderer for AR content, you'll need to instantiate and maintain an ARSession object yourself.
If we use the first suggestion we can do the following.
// Create an instance of ARSKView
let view = ARSKView()
// We can get the session from the view as per documentation:
// A view creates its own session object; use this property
// to access and configure the view's session.
let session = view.session
// We now need to create an instance of a ARConfiguration subclass.
// Looking at the documentation we have the possibility of
// ARWorldTrackingConfiguration, AROrientationTrackingConfiguration
// or ARFaceTrackingConfiguration.
let configuration = ARWorldTrackingConfiguration()
// We can now call our function because we have all that's necessary.
// You'll notice we're not passing a value for the second parameter because it has a default parameter of an empty array.
session.run(configuration)
To improve on your ability of understanding documentation I recommend to identify the moment you don't have enough information for an implementation. In this case you want to call the function
func run(_ configuration: ARConfiguration, options: ARSession.RunOptions = []). Notice that that function is called on a receiver of type ARSession and requires an ARConfiguration be passed in. Your next step would be to look into those classes, specifically looking for an initializer of each and have enough of an understanding to then return to that above function and use them in it.

What does this block mean in Swift - Alamofire?

This is a code block in Alamofire/Manager.swift:
It is inside a class named "public class Manager"
public static let sharedInstance: Manager = {
let configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = Manager.defaultHTTPHeaders
return Manager(configuration: configuration)
}()
Obviously, this is "let something equals the closure," but what I don't get is what does the "()" in the end of the line mean?
To defer the execution of the code? How? (It seems to me that the execution won't be deferred as it has the "()"? And I am wondering why this is not causing the existing of a memory circle? It returns the Manger itself and the returned new instance will also do the same, returning a new self...
Why not a key word "lazy"?
I have a strange feeling that i've seem this usage somewhere else too. I'm learning Swift for 60 days now. Am I fast or slow? How to be a iOS developer also?(I mean how to be a real one, not enroll the program)
For now I'm kind of confused and reading all the source code I could get. Sometimes I feel the official docs and Xcode sucks and I don't know how to read a source code scratch. Any tips and advice?
This initializes sharedInstance with the result of the closure (without the parens at the end it would just initialize it to the closure itself) It's a standard syntax for initializing variable with an initializer too complex for a simple expression. In this multiple statements are required so that the HTTPAdditionalHeaders can be initialized.
Okay, I've made a mistake. The trick lies that the sharedInstance used a key word "static" This is common, but in python, which I am most familiar with, do not use this key word.
According to the official guide:
“You can also define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties, no matter how many instances of that type you create. These kinds of properties are called type properties.”
Apple Inc. “The Swift Programming Language”. iBooks. https://itun.es/cn/jEUH0.l
the answer is at the session name "Setting a Default Property Value with a Closure or Function": https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID231

In Xcode, how can I get the instance properties of self in the debug area?

I'm using Xcode 6 (and Swift, but I don't think it really matters in this case) and trying to use breakpoints to debug my iOS application. Specifically, I want to inspect certain properties of the current instance of the UIKit object.
So for example, let's say I place a breakpoint inside a view controller. In the debug area, I see self. Expanding that gives me the UIKit class I'm working with. Looks like this:
What I need is to be able to inspect certain properties on that object. Let's say, for instance, I want to see the headerReferenceSize property. I can't find it anywhere. My current theory is that I'm seeing the class, not the instance of the class? Is that a possibility? If so, is there any way to inspect the instance of, in this case, FeedFlowLayout?
I'm able to get by with just using println the check the value, or move breakpoints around, but it would be really helpful to inspect all of the properties at once.
You should be able to see the properties of self in the debug area you've shown. Just scroll down. If that doesn't work for some reason, you can type debug commands into the debug console.
If you want to look at headerReferenceSize, type:
expr headerReferenceSize
Into the debugger console and press enter. It should display the value of that variable on the next line.

Resources