macOS
and iOS
(iPad and iPhone).C/C++
. Apple evolved its programing language to be easy to use and syntaxes are taken from programming languages such as C#
, Python, and Ruby. These simple syntax of this programing language make it more meaningful. In swift, all the content of the implementation (.m
) and header (.h
) files are combined in a single file that is (.swift
).?
” mark.?
" is used in property declaration. It tells the compiler that this property is optional. The property may hold a value or not. It avoids runtime errors when accessing that property by using ?. This is useful in optional chaining and a variant of this example is in conditional clauses.var optionalName : String? = "Chanti"
if optionalName != nil {
print("Your name is \(optionalName!)")
}
a<b
) where b is not included. It is known as half open range operator because it contains its first value only not the final value. func thisIsAFunction1() {
}
struct Person {
func thisIsAMethod1() {
}
}
==
" operator and the "===
" operator in ios Swift is that the equal to "==
" operator compares value types to see if the values are the same while the equivalent to "===
" operator compares reference types to see if the references point to the same instance (both point to the same memory address) or not. Let us consider the following example for understanding the difference between the two in a better way :class Human: Equatable {
let id: Int
let nameOfPerson: String
init(id: Int, nameOfPerson: String) {
self.id = id
self.nameOfPerson = nameOfPerson
}
static func == (left: Human, right: Human) -> Bool {
return left.id == right.id
}
}
let human1 = Human(id: 2, nameOfPerson: "Janvi")
let human2 = Human(id: 2, nameOfPerson: "Janvi")
if human1 == human2 {
print("Equal Instances!")
}else{
print("Instances Not Equal!")
}
if human1 === human2 {
print("Equivalent Instances!")
}else{
print("Instances are not Equivalent!")
}
func function1() {
//statements of outer function
func function2() {
//statements of inner function
}
}
enum enum_name
{
// values are described here
}
var freetimelearn = "Free Time Learning"
let fees = 100
String? = nil
String = “Vikas”
String = missingN1 ?? realN1
let
’ keyword. Below is the example where we use declare street as a constant in lieu of a variable. For this, we have to use let keyword :let street: String = “7th Avenue”
var number: Int
street = “Side Street”
number = 10
let street: String = “7th Avenue”
var number: Int
// street = “Side Street”
number = 10
var product = ("MacBook", 1099.99)​
Here, product is a tuple with a string value Macbook and integer value 1099.99.// access the first element
product.0
// access second element
product.1
// create tuple with two elements
var product = ("MacBook", 1099.99)
// access tuple elements
print("Name:", product.0)
print("Price:", product.1)
Name: MacBook
Price: 1099.99
Comparison Parameter | ios Swift | Objective C |
---|---|---|
Type of Programming Language. | ios Swift is an object-oriented and functional programming language. | Objective C is a class-based object-oriented programming language. |
Dynamic Libraries | Dynamic Libraries are supported by ios Swift. For instance, system iOS and macOS libraries are dynamic. In other words, these applications will receive improvements from Apple’s updates without new build submission. | Dynamic Libraries are not supported by Objective C. |
Tuples | Tuples are supported by ios Swift. | Tuples are not supported by Objective C. |
Usage of Semicolons | Usage of Semicolons is not mandatory in ios Swift. | Usage of Semicolons is mandatory in Objective C. |
Method definition in Classes, Structures and Enumerations | The definition of methods in Classes, Structures and Enumerations is allowed in ios Swift. | The definition of methods in Classes, Structures and Enumerations is not allowed in Objective C. |
var firstArray = ["Ramana", "Raani"]
let secondArray = ["Srikanth", "Anusha"]
firstArray.append(contentsOf: secondArray)
firstArray += secondArray
let thirdArray = firstArray + secondArray
init()
{
// New Instances are initialized here
}
UIViewController
. This cannot be achieved using Swift structs.deinit()
function. This cannot be done for Swift structs.//
).// This is a single line comment. ​
/*
) and end with an asterisk followed by a forward-slash (*/
)./* this is multi
Line comment*/
let emptyIntArr:[Int] = []
print(emptyIntArr)
Output :
[]
let someIntArr = [1, 2, 3, 4, 5]
print(someIntArr)
[1, 2, 3, 4, 5]
let emptyDictionary:[Int:String] = [:]
print(emptyDictionary)
[:]
let valDictionary = ["a":10, "b":20, "c":30, "d":40, "e":50, "f":60, "g":70, "h":80, "i":90]
print(valDictionary)
["c": 30, "d": 40, "g": 70, "b": 20, "a": 10, "f": 60, "h": 80, "i": 90, "e": 50]
3.25169
and -238.21
. Floating point types can represent a wider range of values than integer types. There are two signed floating point number64 bit
floating point number, it is used when floating point values must be very large32 bit
floating point number, it is used when floating point values does not need 64 bit
precisiondeinit {
// perform the deinitialization
}
typealias AudioS1 = UInt16
@property (nonatomic, retain) NSButton *someButton1;
…
@synthesize someButton1;
@property (nonatomic, retain) IBOutlet NSButton *someButton1;
…
@dynamic someButton1;
let
,’ and ‘var
’ are used to declare the functions in JavaScript. The main difference in both of them is that ‘let
’ is block-scoped, whereas ‘var
’ is the functional scope. As variable is represented by ‘var
,’ and it is defined in the whole program in relation to ‘let
.’console.log(y);
var y=7;
console.log(y);
undefined 7
console.log(y);
let x=7; console.l
og(y);
Error
@ symbol
. There are different arguments with different attributes.@available(<attribute_name1>)
@available(<attribute_name1>)(<arguments>)
@available
guard expression else {
//statements
//must contain a control statement:return, break, continue or throw.
}
let intNum: Int? = 1
// intSecNum have value 1.
let intSecNum: Int = intNum!
let intNum: Int? = nil
let intSecNum= intNum! // force unwarp - fatal error
------------------------------
fatal error: unexpectedly found nil while unwrapping an Optional value​
var strGreet: String! = "Welcome"
/* it converts it into plain String as strGreet
automatically unwrapped it's content. */
let userName = strGreet
//assign nil
strGreet = nil
// it will throw fatal error
// fatal error: unexpectedly found nil while unwrapping an Optional value
let user2Name = strGreet​
subscript(index: Int) -> Int {
get {
// subscript value declarations
}
set(newValue) {
// definitions
}
}
struct demoStruct {
var foo: String = "Initial String"
func transformString() {
foo = "Transformed String".
//The above results in a compile time error: Cannot assign to property: 'self' is immutable.
//We need to mark the method 'mutating' to make 'self' mutable.
}
}
foo
" inside a function declared in the struct itself.struct demoStruct {
var foo: String = "Initial String"
mutating func transformString() {
foo = "Transformed String".
}
}
func RunClosureAfterGreeting(name: String, closure: () -> ()) {
print("Hi, \(name)!!!")
closure()
}
RunClosureAfterGreeting(name: "Danial") {
print("The closure has been run")
}
func Vector3D(x: [T], y: [T], z: [T])
{ self.xCord = x
self.yCord = y
self.zCord = z}
UIViewController
class is the superclass of all the view controller objects. The functionality for presenting them, loading views, rotating them is a response to the device rotations. All the standard system behavior is provided by the UIViewController
class. Model-View-Controller
is an important fundamental concept to be understood in iOS
development. Many iOS frameworks, like UIKit
, Cocoa Touch
, use the MVC pattern for messaging and structured data flow.func greet(_ completion: () -> ()) {
print("Hello world!")
completion()
}
greet({
print("Task finished.")
})
Hello world!
Task finished.