Swift is brand new programming language introduced by Apple at World Wide Developer Conference 2013 for iOS and OS X apps development. It build on the best of C and Objective-C without constrain of C compatibility. According to apple swift was in many years of development. It has been revealed that apple began working on swift in mid-2010 with team of coders. To make learning easy apple has released two iBooks named “The Swift Programming Language” and “Using swift with cocoa and Objective-C” to its iTunes store which are available to download for free. Using swift require 6.0 or higher version of Xcode. There is also swift playground feature in Xcode which help a lot to learn swift. It would be great for all developers to start using swift as soon as possible. So here are some tips to using swift for starters.
Factory Methods
All Objective-C lovers know about factory methods. These are the class methods which gives easy instance of class as our requirement.
[NSString stringWithFormat:@"Swift Language”]
In swift there are no factory methods so you think in swift version of NSString you can find this method then you are totally wrong. Swift version of this method is like this
NSString(format: "Swift Language”)
So as we can see swift converts all factory methods to initialising method in this case above method is convenience initialiser.
So if you don’t found any class method which you used to write in Objective-C than don’t panic
just try using init methods you will find one over there.
Optionals
Optionals are one of the biggest feature in swift. Optional is way to defining object that you are using can be nil or it can have a value.Optionals are an example of the fact that Swift is a type safe language. Swift helps you to declare optionals like this
Var myStr :NSString?
So myStr is optional type we can’t use it directly. In order to use it we must unwrap the option type like this
if let mySting = myStr {
// myString is unwrapped and can be used directly.
} else {
// myStr is nil. and performing any operation on that will throw an exception.
}
Closures
Closures are same as blocks in objective-C which are functional pointer in C. lets compare Blocks and Closures with its language perspective.
as Variable
NSString* (^myBlock)(NSSting* str1) = ^NSString* (var_type NSSting* str1)
{
// body
};
var myBlock = { (str1: String) -> String in
// body
}
as Property
@property (copy) NSString* (^myBlock) (NSSting* str1);
var myBlock : ((str1: String) -> String)
as Method
(void)myMethod:(NSString* (^)(NSSting* str1))myBlock;
func myMethod(myBlock: ((str1: String) -> String)) -> ()
as Argument
[obj1 doSomethingWithBlock: ^NSString* (NSString* str1)
{
//body
}];
obj1.doSomething(block: { (str1: String) -> Sting in /* body */ })
as Anonymous
^NSString* (NSString* str1)
{
//body
};
{ (str1: String) -> Sting in /* body */ }
as typedef
typedef NSString* (^myBlockType)(NSString* str1);
typealias myBlockType = ((str1: String) -> String)
as Inline
^NSString* (NSString *str1)
{
// body
}(str1);
{ (str1: String) -> Sting in /* body */ }(myStr1)
as Recursive
__block NSString* (^myBlock)(NSString* str1) = [^NSString* (NSString* str1)
{
if (returnCondition)
{
myBlock = nil;
return;
}
// body
} copy];
myBlock(str1);
var myBlock: ((str1: String) -> String)!
myBlock = { (str1: String) -> String in
if returnCondition {
self.myBlock = nil;
return;
}
// body
}
myBlock(Str1)
as Returning
(NSString* (^) (str1: String)) myMehodName
{
// body
}
func myMethodName () -> ((str1: String) -> String) {
// method body
}
Optional Chaining
Optional chaining in Swift is similar to messaging nil in Objective-C, but in a way that works for any type, and that can be checked for success or failure.
Optional chaining will of course only work with optional types. we can access its property and methods with using “?” in syntax. for example lets say we have String object as of optional type.
var str : String?
var aBool : Bool? = str?.hasPrefix(“my”)
hasPrefix will give true or false if str has a value in it other wise it will give nil. that will make Bool,
which is its return type a option type. although in library hasPrefix gives Bool return type no
Bool?(Optional).
extension String {
/// Return `true` iff `self` begins with `prefix`
func hasPrefix(prefix: String) -> Bool
/// Return `true` iff `self` ends with `suffix`
func hasSuffix(suffix: String) -> Bool
}
But while it is being used in optional chaining, the result will always come as optional.
Type Casting
Type casting is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy.
Casting can be of two types, upcasting and downcasting. upcasting is implicit. Casting can be done by “as” syntax. So lets say you have a AnyObject and you want to teat it as String then you shall be doing this.
var obj : AnyObject
var aBool = (obj as String).hasPrefix
This type of casting is known as downcasting where we explicitly need to downcast the object. Casting doesn’t not change the object or value it is just for compiler satisfaction if any thing goes wrong it would throw exception runtime.
To make code safe from this exception we can use optional casting(as?) which would work like this
var aBool :Bool? = (obj as? String).hasPrefix
now the hasPrefix will only get called if obj is actually of String type. So return type would be option.