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.