In rare cases, Core Foundation function may return a C-pointer or an object reference embedded into an Unmanaged wrapper. You must transfer Unmanaged references into the normal memory management system before working with them in Swift.
An Unmanaged wrapper, like an Optional wrapper, provides a layer of safety between your code and a potentially nasty crash. The Unmanaged<T> type stores a pointer whose memory is not controlled by the Swift runtime system. Before using this data, you take responsibility for how this memory should stay alive.
Ans : In Swift, there are multiple ways to write utility functions.
Static Functions
Static functions are invoked by the class itself, not by an instance. This makes it simple to invoke utility functions without having to manage an object to do that work for you.
We can access static function as AppUtils.appUtility()
Static functions can not be overridden.
Class Functions
Class functions (not instance methods) are also static functions but they are dynamically dispatched and can be overridden by subclasses unlike static functions.
We can access them similar to static functions as AppUtils.appUtility() and AppOtherUtils.appUtility().
static is same as class final.
Global Functions
But then you can also just do a stand alone function in Swift which is not within a class and access it anywhere in the project. The global functions can be kept in a separate file that we can import into any project as per requirement.
We can just access them as appUtility() anywhere in the project. If you have method with same name as of global function, then access global function with MyAppName.appUtility()
In case of static functions, if we access one of the static member, entire class gets loaded in memory. But in case of global function, only that particular function will be loaded in memory.
So, which one is better to use?
It is largly subjective why we pick one over another. Some prefer static method approach as a form of namespacing. For example, using the static method approach also allows us to have a method ClassA.appUtility() and a method named ClassB.appUtility(), which is useful while developing a library or framework.
Global functions are more modular and factor out single tasks for a single function - a good global utility function that does exactly one thing and does it perfectly can also sometimes be abstracted or made generic and used in other context as well.
We can just create a AppUtility.swift file and put all the utility functions in it. Later this file can be used across multiple projects.
Ans : Force unwrapping : It's the action of extracting the value contained inside an Optional. This operation is dangerous because you are telling the compiler: I am sure this Optional value does contain a real value, extract it!
let anOptionalInt: Int? = 1
let anInt: Int = anOptionalInt!
But here if, anOptionalInt has nil value, then fatal error will be generated. Unexpectedly found nil while unwrapping an Optional value.
Optional Binding: Using if let, we can store value in one variable if value is not nil. By this way, fatal error never be generated. This way will be used when we are not sure that variable has value or nil. This method is better because no chances of crashing app.
if let tempStockCode = stockCode {
let message = text + tempStockCode
println(message)
}
Implicitly unwrapped optionals :
let anOptionalInt: Int! = 1
let anInt: Int = anOptionalInt
anOptionalInt is never be optional. But it can has value of nil.
Encapsulation hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.
Encapsulation: Wrapping code and data together into a single unit. Class is an example of encapsulation, because it wraps the method and property.
Abstraction: Hiding internal details and showing functionality only. Abstraction focus on what the object does instead of how it does. It provides generalized view of classes.
int number = 5;
string aStringNumber = number.ToString();
Here, ToString() is abstraction. And how this mechanism number variable converted to string and initialize into aStringNumber is encapsulation.
We can achieve abstraction using protocol in iOS, achieve encapsulation using access control in class or struct.
Ans : Geo-Fence : A Geo-fence is a technology that defines a virtual boundary around a real world geographical area. Every time the user enters or exits the boundary of a certain geofence, actions can be triggered in a location enabled device (often a smartphone). Usually the user will receive a notification with certain information based on its location in real time.
Let's implement :
In order to work with geofences we need to import CoreLocationframework. After that, we define a locationManager in our viewController and we make the viewController a CLLocationManagerDelegate:
Next, we have to define a region with a center (latitude and longitude) and a radius to monitor. As an example, we are going to define a circular region around the beach in Playa Grande, Mar del Plata with a radius of 400 metres:
funcsetUpGeofenceForPlayaGrandeBeach() {
let geofenceRegionCenter = CLLocationCoordinate2DMake(-38.028308, -57.531508);
let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius: 400, identifier: "PlayaGrande");
After creating the region, we are going to set the flags notifyOnExit and notifyOnEntry to true . This will make the CLLocationManagerDelegate trigger a notification each time the user enters or exits the geofenceRegionin the following methods:
print("Bye! Hope you had a great day at the beach!")
//Good place to schedule a local notification
}
We are still missing the part when the user has to give authorization to the app to track it’s location. So in our viewController we add the following lines to the viewDidLoad method:
overridefuncviewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.delegate = self
}
When the user gives authorization to the app, a delegate method is triggered and if the user has given authorization, we should set up the geofence and start monitoring the region.
Ans : After first time run pod install, Podfile.lock will be generated.
The purpose of Podfile.lock is tracking of every version of every library that Cocoapods has installed for you, especially working together with a team.
In team there are two guys working, Narendra and Amit.
Narendra has made project and install library named Demo Library Version 1.3.3
After some days, Demo Library is updated to 1.4.0. and Narendra not hit command pod update as he is happy with 1.3.3
Amit copy narendra's project and hit command pod install, then 1.4.0 will be installed(if Podfile.lock is not there).
So here is how Podfile.lock work, record the version of what Narendra had installed, then cocoapods will check what version Narendra had installed and then install on Amit's project. (1.3.0)
But if Amit hit command pod update, then in pod.lock file, version changed to 1.4.0 .
So Podfile.lock file should not be deleted if we work on team. And so Podfile.lock file should be checked while using source control.
Everyone should take care while hitting command pod install and pod update. Choose wisely between those. While update pod update, we should ask to our teammate.
Another nice feature is cocoapods will create a snapshot of every library while using source control.
To remove pods from a project completely you need to install two thing first...those are follows(Assuming you have already cocoa-pods installed in your system.)...
Cocoapods-Deintegrate Plugin
Cocoapods-Clean Plugin
Installation
Cocoapods-Deintegrate Plugin
Use this following command on your terminal to install it.
sudo gem install cocoapods-deintegrate
Cocoapods-Clean Plugin
Use this following command on your terminal to install it.
sudo gem install cocoapods-clean
Usage
First of all goto your project folder by using the as usual command like..
cd (path of the project)//Remove the braces after cd
Now use those two plugins to remove it completely as follows..
Cocoapods-Deintegrate Plugin
Use this following command on your terminal to deintegrate the pods from your project first.
pod deintegrate
Cocoapods-Clean Plugin
After deintegration of pod from your project use this following command on your terminal to clean it completely.
pod clean
After completing the above tasks there should be the Podfile still remaining on your project directory..Just delete that manually or use this following command on the terminal..
rm Podfile
Thats it...Now you have your project free from pods...Cleaned.
Removing Cocoapods from the system.
Any way try to use the following command on your terminal to uninstall/remove the coca-pods from your system.