Swift – Assignment Operators ”; Previous Next Assignment Operators are the special operators. They are used to assign or update values to a variable or constant. In the assignment operators, the right-hand side of the assignment operator is the value and the left-hand side of the assignment operator should be the variable to which the value will be assigned. The data type of both sides should be the same, if they are different we will get an error. The associativity of assignment operators is from right to left. Swift supports two types of assignment operators − Simple Assignment Operator − It is the most commonly used operator in Swift. It is used to assign value to a variable or constant. Compound Assignment Operators − They are the combination of assignment operator (=) with other operators like +, *, /, etc. The following table shows all the assignment operators supported by Swift − Operators Name Example (=) Assignment var x = 10 += Add and Assignment A += B is equivalent to A = A + B -= Subtract and Assignment A -= B is equivalent to A = A – B *= Multiply and Assignment A *= B is equivalent to A = A * B /= Divide and Assignment A /= B is equivalent to A = A / B %= Modulo and Assignment A %= B is equivalent to A = A % B &= Bitwise AND and Assignment A &= B is equivalent to A = A & B |= Bitwise Inclusive OR and Assignment A += B is equivalent to A = A | B ^= Bitwise Exclusive OR and Assignment A ^= B is equivalent to A = A ^B <<= Left Shift and Assignment A <<= B is equivalent to A = A << B >>= Right Shift and Assignment A >>= B is equivalent to A = A >> B Assignment Operator in Swift An assignment operator “=” is the most straightforward and commonly used operator in Swift. It is used to assign value to a constant or variable. The left-hand side of the assignment operator contains the variable name and the right-hand side contains the value. While using the assignment operator always remember the data type of both the operands should be the same. Syntax Following is the syntax of the assignment operator − var number = 10 Example Swift program to assign a string to a variable. import Foundation // Defining a variable of string type let mystring : String // Assigning a value to the variable using the assignment operator mystring = “Tutorialspoint” // displaying result print(“String = “, mystring) Output String = Tutorialspoint Add and Assignment Operator in Swift An Add and Assignment Operator “+=” is used to perform addition between the left variable and right variable and then assign the result to the left variable. Suppose we have two variables A = 10 and B = 20. A += B => A = 10 + 20 => A = 30. Syntax Following is the syntax of the add and assignment operator − X += Y Example Swift program to find the sum of two variables using add and assignment operator “+=”. import Foundation var num1 = 10 var num2 = 80 // Calculating sum num1 += num2 print(“Sum = “, num1) Output Sum = 90 Subtract and Assignment Operator in Swift A Subtract and Assignment Operator “-=” is used to perform a subtraction between the left variable and the right variable and then assign the result to the left variable. Suppose we have two variables A = 10 and B = 20. A -= B => A = 10 – 20 => A = -10. Syntax Following is the syntax of the subtract and assignment operator − X -= Y Example Swift program to subtract two variables using subtract and assignment operator “-=”. import Foundation var num1 = 34 var num2 = 21 // Subtracting num1 from num2 num1 -= num2 print(“Result = “, num1) Output Result = 13 Multiply and Assignment Operator in Swift A Multiply and Assignment Operator “*=” is used to perform a multiplication between the left operand and the right operand and then assign the result to the left operand. Suppose we have two variables A = 10 and B = 20. A *= B => A = 10 * 20 => A = 200. Syntax Following is the syntax of the multiply and assignment operator − X *= Y Example Swift program to find the product of two variables using multiply and assignment operator “*=”. import Foundation var num1 = 12 var num2 = 2 // Product of two numbers num1 *= num2 print(“Result = “, num1) Output Result = 24 Divide and Assignment Operator in Swift A Divide and Assignment Operator “/=” is used to divide the left operand by the right operand and then assign the result to the left operand. Suppose we have two variables A = 20 and B = 5. A /= B => A = 20 / 5 => A = 4. Syntax Following is the syntax of the divide and assignment operator − X
Category: Computer Programming
Solidity – Arrays
Solidity – Arrays ”; Previous Next Array is a data structure, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index. In Solidity, an array can be of compile-time fixed size or of dynamic size. For storage array, it can have different types of elements as well. In case of memory array, element type can not be mapping and in case it is to be used as function parameter then element type should be an ABI type. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays To declare an array of fixed size in Solidity, the programmer specifies the type of the elements and the number of elements required by an array as follows − type arrayName [ arraySize ]; This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid Solidity data type. For example, to declare a 10-element array called balance of type uint, use this statement − uint balance[10]; To declare an array of dynamic size in Solidity, the programmer specifies the type of the elements as follows − type[] arrayName; Initializing Arrays You can initialize Solidity array elements either one by one or using a single statement as follows − uint balance[3] = [1, 2, 3]; The number of values between braces [ ] can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array − If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write − uint balance[] = [1, 2, 3]; You will create exactly the same array as you did in the previous example. balance[2] = 5; The above statement assigns element number 3rd in the array a value of 5. Creating dynamic memory arrays Dynamic memory arrays are created using new keyword. uint size = 3; uint balance[] = new uint[](size); Accessing Array Elements An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example − uint salary = balance[2]; The above statement will take 3rd element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays − Members length − length returns the size of the array. length can be used to change the size of dynamic array be setting it. push − push allows to append an element to a dynamic storage array at the end. It returns the new length of the array. Example Try the following code to understand how the arrays works in Solidity. pragma solidity ^0.5.0; contract test { function testArray() public pure{ uint len = 7; //dynamic array uint[] memory a = new uint[](7); //bytes is same as byte[] bytes memory b = new bytes(len); assert(a.length == 7); assert(b.length == len); //access array variable a[6] = 8; //test array variable assert(a[6] == 8); //static array uint[3] memory c = [uint(1) , 2, 3]; assert(c.length == 3); } } Print Page Previous Next Advertisements ”;
Swift – Misc Operators
Swift – Misc Operators ”; Previous Next Miscellaneous Operators Swift Swift supports different types of operators like arithmetic, comparison, logical, bitwise, assignment and range operators. Apart from these operators it has miscellaneous operators are they are − Operator Name Example – Unary Minus -23 + Unary Plus 32 Condition ? X : Y Ternary operator X>Y ? 43 : 21= 43 Unary Minus Operator in Swift A unary minus operator is used to represent a negative(-) sign that is placed before the numeric value. It converts a positive number into a negative and a negative number into a positive. It is a prefix operator, which means it is placed before the value without any white space. Syntax Following is the syntax of the unary minus operator − -x Example Swift program to find the sum of two numbers using the unary minus operator. import Foundation let x = 23 // Specifying sign using unary minus operator let y = -2 var sum = 0 sum = x + y // 23 + (-2) print(“Sum of (x) and (y) = (sum)”) Output Sum of 23 and -2 = 21 Unary Plus Operator in Swift A unary plus operator is used to make a numeric expression positive. It only adds a positive (+) sign before the numeric value but does not change the value. It is also a prefix operator. Syntax Following is the syntax of the unary plus operator − +x Example Swift program to find the sum of two numbers using the unary plus operator. import Foundation let x = 20 // Specifying sign using unary plus operator let y = +2 var sum = 0 sum = x + y // 23 + (+2) print(“Sum of (x) and (y) = (sum)”) Output Sum of 20 and 2 = 22 Ternary Conditional Operator in Swift A ternary conditional operator is a shorthand of an if-else statement. It has three parts: condition ? expression1 : espression2. It is the most effective way to decide between two expressions. Because it evaluates one of the two given expressions according to whether the given condition is true or false. If the given condition is true, then it evaluates expression1. Otherwise, it will evaluate expression2. Syntax Following is the syntax of the ternary conditional operator − Condition ? Expression1 : Expression2 Example Swift program to explain ternary conditional operator. import Foundation let x = 20 let y = 2 // If x is greater than y then it will return 34 // Otherwise return 56 var result = x > y ? 34 : 56 print(result) Output 34 swift_operators.htm Print Page Previous Next Advertisements ”;
Socket.IO – Event Handling
Socket.IO – Event Handling ”; Previous Next Sockets work based on events. There are some reserved events, which can be accessed using the socket object on the server side. These are − Connect Message Disconnect Reconnect Ping Join and Leave. The client-side socket object also provides us with some reserved events, which are − Connect Connect_error Connect_timeout Reconnect, etc. Now, let us see an example to handle events using SocketIO library. Example 1 In the Hello World example, we used the connection and disconnection events to log when a user connected and left. Now we will be using the message event to pass message from the server to the client. To do this, modify the io.on (”connection”, function(socket)) as shown below –var app = require(”express”)(); var http = require(”http”).Server(app); var io = require(”socket.io”)(http); app.get(”/”, function(req, res){ res.sendFile(”E:/test/index.html”); }); io.on(”connection”, function(socket){ console.log(”A user connected”); // Send a message after a timeout of 4seconds setTimeout(function(){ socket.send(”Sent a message 4seconds after connection!”); }, 4000); socket.on(”disconnect”, function () { console.log(”A user disconnected”); }); }); http.listen(3000, function(){ console.log(”listening on *:3000”); }); This will send an event called message(built in) to our client, four seconds after the client connects. The send function on socket object associates the ”message” event. Now, we need to handle this event on our client side, to do so, replace the contents of the index.html page with the following − <!DOCTYPE html> <html> <head><title>Hello world</title></head> <script src=”/socket.io/socket.io.js”></script> <script> var socket = io(); socket.on(”message”, function(data){document.write(data)}); </script> <body>Hello world</body> </html> We are now handling the ”message” event on the client. When you go to the page in your browser now, you will be presented with the following screenshot. After 4 seconds pass and the server sends the message event, our client will handle it and produce the following output − Note − We sent a string of text here; we can also send an object in any event. Message was a built-in event provided by the API, but is of not much use in a real application, as we need to be able to differentiate between events. To allow this, Socket.IO provides us the ability to create custom events. You can create and fire custom events using the socket.emit function. Following code emits an event called testerEvent − var app = require(”express”)(); var http = require(”http”).Server(app); var io = require(”socket.io”)(http); app.get(”/”, function(req, res){ res.sendFile(”E:/test/index.html”); }); io.on(”connection”, function(socket){ console.log(”A user connected”); // Send a message when setTimeout(function(){ // Sending an object when emmiting an event socket.emit(”testerEvent”, { description: ”A custom event named testerEvent!”}); }, 4000); socket.on(”disconnect”, function () { console.log(”A user disconnected”); }); }); http.listen(3000, function(){ console.log(”listening on localhost:3000”); }); To handle this custom event on client we need a listener that listens for the event testerEvent. The following code handles this event on the client − <!DOCTYPE html> <html> <head><title>Hello world</title></head> <script src=”/socket.io/socket.io.js”></script> <script> var socket = io(); socket.on(”testerEvent”, function(data){document.write(data.description)}); </script> <body>Hello world</body> </html> This will work in the same way as our previous example, with the event being testerEvent in this case. When you open your browser and go to localhost:3000, you”l be greeted with − Hello world After four seconds, this event will be fired and the browser will have the text changed to − A custom event named testerEvent! Example 2 We can also emit events from the client. To emit an event from your client, use the emit function on the socket object. <!DOCTYPE html> <html> <head><title>Hello world</title></head> <script src=”/socket.io/socket.io.js”></script> <script> var socket = io(); socket.emit(”clientEvent”, ”Sent an event from the client!”); </script> <body>Hello world</body> </html> To handle these events, use the on function on the socket object on your server. var app = require(”express”)(); var http = require(”http”).Server(app); var io = require(”socket.io”)(http); app.get(”/”, function(req, res){ res.sendFile(”E:/test/index.html”); }); io.on(”connection”, function(socket){ socket.on(”clientEvent”, function(data){ console.log(data); }); }); http.listen(3000, function(){ console.log(”listening on localhost:3000”); }); So, now if we go to localhost:3000, we will get a custom event called clientEvent fired. This event will be handled on the server by logging − Sent an event from the client! Print Page Previous Next Advertisements ”;
Swift – Constants
Swift – Constants ”; Previous Next What is Constant in Swift? Constants refer to fixed values that a program may not alter during its execution. Constants can be of any data type like an integer, float, character, double, or a string literal. There are enumeration constants as well. They are treated just like regular variables except for the fact that their values cannot be modified after their definition. Declaring Swift Constant Constants are used to store values that won’t change throughout the whole execution of the program. They are always declared before their use and they are declared using the let keyword. Syntax Following is the syntax of constant − let number = 10 Example Swift program to demonstrate how to declare constants. import Foundation // Declaring constant let constA = 42 print(“Constant:”, constA) Output Constant: 42 If we try to assign value to a constant, then we will get an error just like in the below example − import Foundation // Declaring constant let constA = 42 print(“Constant:”, constA) // Assigning value to a constant constA = 43 print(“Constant:”, constA) Output main.swift:8:1: error: cannot assign to value: ”constA” is a ”let” constant constA = 43 ^~~~~~ main.swift:4:1: note: change ”let” to ”var” to make it mutable let constA = 42 ^~~ var We can also declare multiple constants in a single line. Where each constant has its values and is separated by commas. Syntax Following is the syntax of multiple constants − let constantA = value, constantB = value, constantC = value Example Swift program to demonstrate how to declare multiple constants in a single line. import Foundation // Declaring multiple constants let constA = 42, constB = 23, constC = 33 print(“Constant 1:”, constA) print(“Constant 2:”, constB) print(“Constant 3:”, constC) Output Constant 1: 42 Constant 2: 23 Constant 3: 33 Type Annotations with constants Type annotation is used to define what type of value should be stored in the constant at the time of declaration. While declaring a constant we can specify type annotation by placing a colon after the constant name followed by the type. Type annotation is rarely used if we provide an initial value at the time of declaring a constant because Swift will automatically infer the type of the constant according to the assigned value. For example, let myValue = “hello”, so the type of myValue constant is String because we assigned a string value to the constant. Syntax Following is the syntax of type annotations − let constantName : Type = Value Example Swift program to demonstrate how to specify type annotation. import Foundation // Declaring constant with type annotation let myValue : String = “hello” print(“Constant:”, myValue) Output Constant: hello We can also define multiple constants of the same type in a single line. Where each constant name is separated by a comma. Syntax Following is the syntax of multiple constants − let constantA, constantB, constantC : Type Example Swift program to demonstrate how to specify multiple constants in single-type annotation. import Foundation // Declaring multiple constants in single-type annotation let myValue1, myValue2, myValue3 : String // Assigning values myValue1 = “Hello” myValue2 = “Tutorials” myValue3 = “point” print(“Constant Value 1:”, myValue1) print(“Constant Value 2:”, myValue2) print(“Constant Value 3:”, myValue3) Output Constant Value 1: Hello Constant Value 2: Tutorials Constant Value 3: point Naming Constants in Swift Naming a constant is very important. They should have a unique name. You are not allowed to store two constants with the same name if you try to do you will get an error. Swift provides the following rules for naming a constant − Constant names can contain any character including unicode characters. For example, let 你好 = “你好世界”. The constant name should not contain whitespace characters, mathematical symbols, arrows, private-se Unicode scalar values, or line and box drawing characters. The name of a constant can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. For example, let myValue = 34. Upper and lowercase letters are distinct because Swift is case-sensitive. For example, let value and let Value are two different constants. Constant names should not begin with a number. You are not allowed to re-declare a constant with the same name. Or cannot change into another type. You are not allowed to change a constant into a variable or vice versa. If you want to declare a constant name the same as a reserved keyword, then use backticks(`) before the name of the constant. For example, let ”var = “hello”. Printing Swift Constants You can print the current value of a constant or variable using the print() function. You can interpolate a variable value by wrapping the name in parentheses and escaping it with a backslash before the opening parenthesis. Example Swift program to print constant. import Foundation // Declaring constants let constA = “Godzilla” let constB = 1000.00 // Displaying constant print(“Value of (constA) is more than (constB) millions”) Output Value of Godzilla is more than 1000.0 millions Print Page Previous Next Advertisements ”;
Swift – Operators
Swift – Operators ”; Previous Next What is an Operator in Swift? An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Or we can say that operators are special symbols that are used to perform specific operations between one or more operands. For example, to add two numbers such as 34 and 21 we will use the + operator (34 + 21 = 55). Type of Operators in Swift Swift supports the following types of operators − Arithmetic Operators Comparison Operators Logical Operators Bitwise Operators Assignment Operators Range Operators Misc Operators Let”s discuss each operator separately in detail. Swift Arithmetic Operators Arithmetic operators are used to perform mathematic operations like addition, division, multiplication, etc. on the given operands. They always work on two operands but the type of these operands should be the same. Swift supports the following arithmetic operators − Operator Name Example + Addition 20 + 30 = 50 – Subtraction 30 – 10 = 20 * Multiplication 10 * 10 = 100 / Division 20 / 5 = 4 % Modulus 10 % 2 = 0 Swift Comparison Operators Comparison operators are used to compare two operands and find the relationship between them. They return the result into a boolean(either true or false). Swift supports the following comparison operators − Operator Name Example (==) Equal 10 == 10 = true != Not Equal 34 != 30 = true > Greater than 90 > 34 = true < Less than 12 < 34 = true >= Greater than or Equal to 30 >= 10 = true <= Less than or Equal to 10 <= 32 = true Swift Logical Operators Logical operators are used to perform logical operations between the given expressions. It can also make decisions on multiple conditions. It generally works with Boolean values. Swift supports the following logical operators − Operator Name Example && Logical AND X && Y || Logical OR X || Y ! Logical NOT !X Swift Bitwise Operators Bitwise operators are used to manipulate individual bits of the integer. They are commonly used to perform bit-level operations. Swift supports the following bitwise operators − Operator Name Example & Bitwise AND X & Y | Bitwise OR X | Y ^ Bitwise XOR X ^ Y ~ Bitwise NOT ~X << Left Shift X << Y >> Right Shift X >> Y Swift Assignment Operators Assignment operators are used to assign and update the value of the given variable with the new value. Swift supports the following assignment operators − Operator Name Example (=) Assignment X = 10 += Assignment Add X = X + 12 -= Assignment Subtract X = X – 12 *= Assignment Multiply X = X * 12 /= Assignment Divide X = X / 12 %= Assignment Modulus X = X % 12 <<= Assignment Left Shift X = X << 12 >>= Assignment Right Shift X = X >> 12 &= Bitwise AND Assignment X = X & 12 ^= Bitwise Exclusive OR Assignment X = X ^12 |= Bitwise Inclusive OR Assignment X = X | 12 Swift Misc Operators Apart from the general operators Swift also supports some special operators and they are − Operator Name Example – Unary Minus -9 + Unary Plus 2 Condition ? X : Y Ternary Conditional If Condition is true ? Then value X : Otherwise value Y Swift Advance Operators Apart from the basic operators Swift also provides some advanced operators that are used to manipulate complex values and they are − Arithmetic Overflow Operators Identity Operators Identity Operators Let”s discuss each operator separately in detail. Swift Arithmetic Overflow Operators Arithmetic overflow operators are used to perform arithmetic operations and handle overflow very well if occurs. Or we can say that arithmetic operators work with those integers whose value may exceed the maximum or minimum bound. Swift supports the following arithmetic overflow operators − Operator Name Example &+ Overflow Addition Num1 &+ Num2 &- Overflow Subtraction Num1 &- Num2 &* Overflow Multiplication Num1 &* Num2 Swift Identity Operators Identity operators are used to determine whether the given variable refers to the same instance or not. These operators work with objects and classes. They are referenced type operators.
Swift – Logical Operators
Swift – Logical Operators ”; Previous Next Logical Operator in Swift Logical operators are used to perform logical operations on multiple expressions together. They always return boolean values either true or false. They are commonly used with conditional statements and loops to return decisions according to the boolean conditions. You can also combine them to manipulate boolean values while dealing with complex expressions. Swift supports the following logical operators − Operator Name Example && AND A && B = false || OR A || B = true ! NOT !(A && B) = true AND Operator in Swift The AND operator returns true if both the given expressions are true. If any one of the expressions is false, then this operator will return false. It is an infix operator means this operator only works when you place it in between two operands. Syntax Following is the syntax of the AND operator − var result = expression1 && expression2 Example Swift program to perform logical AND operation between two expressions using AND operator (&&). import Foundation let age = 22 let height = 185 if (age > 18) && (height > 182){ print(“You are eligible for the Police exam”) } else { print(“You are not eligible”) } Output You are eligible for the Police exam OR Operator in Swift The OR operator returns true if only one of the expressions among the two given expressions is true. If both the expressions are false, then this operator will return false. It is an infix operator means it will be placed between two operands to perform its action. Syntax Following is the syntax of the OR operator − var result = expression1 || expression2 Example Swift program to perform logical OR operation between two expressions using OR operator (||). import Foundation let marks = 640 let experience = 3 if (marks > 500) || (experience > 4){ print(“You are eligible for the PO Post”) } else { print(“You are not eligible”) } Output You are eligible for the PO Post NOT Operator in Swift A NOT operator is used to invert the boolean value. If the value is true, then it will convert into false. Whereas if the value is false then it will convert into true. It is a prefix operator and is placed just before the expression whose value you want to invert. Syntax Following is the syntax of the NOT operator − !(expression1 && expression2) Example Swift program to perform logical NOT operation on the expressions using NOT operator (!). import Foundation let x = 20 let y = 40 // Here expression gives a true result but NOT operator converts it into a false if !(x > 0 && y > 0) { print(“Given values are greater than 0”) } else { print(“Given values are less than 0”) } Output Given values are less than 0 Combining Logical Operators in Swift In Swift, we can also combine multiple logical operators in a single expression. It will create a long compound expression. As we know AND and OR are left-associative so in the compound expression the left-side expression will evaluate first. Example Swift program to combine multiple logical operators. import Foundation let password = “vsv@v3″ let username = “mona” let mainPassword = “MO@12s” if (password == “XP123” || username == “mona”) && mainPassword == “MO@12s”{ print(“Welcome to the digital locker”) } else { print(“Error!!! Please enter correct detail”) } Output Welcome to the digital locker swift_operators.htm Print Page Previous Next Advertisements ”;
Swift – Overview
Swift Overview ”; Previous Next Introduction to Swift Programming Language Swift is a new generation programming language developed by Apple Inc. for iOS and OS X development. Swift adopts the best of C and Objective-C, without the constraints of C compatibility. Due to modern syntax, safety features, and optimized performance Swift is fast, concise, expressive and easy to use. It makes the development process easier by providing features like automatic memory management, type inference, etc. Using Swift we can create dynamic applications for mobile, desktops or even for server-side applications. Its main aim is to provide a seamless and robust development experience to its users. History of Swift The history of Swift began in 2010 when an Apple engineer named Chris Lattner started working on a new programming language. His main aim was to craft a programming language that is easy to use and more expressive just like high-level languages and provides great performance like low-level languages. He achieves his goal by creating Swift programming language. So in 2014, Apple unveiled Swift to the world for the very first time at WWDC (World Wide Developer Conference) to replace Objective-C for iOS and macOS development. After that, the evolution process went on and created the following versions of Swift with updates and improvements. The latest version of Swift is 5.9. Version Release Year Swift 1.0 2014 Swift 1.2, Swift 2.0 2015 Swift 3.0 2016 Swift 4.0 2017 Swift 4.1, 4.2 2018 Swift 5.0, Swift 5.1 2019 Swift 5.3 2020 Swift 5.4, Swift 5.5 2021 Swift 5.6, Swift 5.7 2022 Swift 5.8, Swift 5.9 2023 Characteristics of Swift Swift offers various characters to its developers which will help them to develop optimized and dynamic applications for the Apple platform and the key characteristics are − Modern Syntax − Swift provides clean and expressive syntaxes so that the developer can develop concise and easy-to-read programs. It makes it a more approachable language for both beginner and experienced developers. Safety − It increases safety by removing common bugs and errors. It incorporates modern programming techniques which makes it more secure and reliable. Performance − It provides high performance just like low-level languages while maintaining the safety and expressiveness of the code. Interoperability − Swift is an interoperable language. It works seamlessly with other languages like Objective-C. Developers are allowed to use Swift and Objective-C code together in the same project. Open Source − Swift is an open-source programming language that enhances the collaboration, innovation and ongoing improvements of Swift. Optionals − Swift provides great support to the optionals so that developers can explicitly represent the absence of value. It handles null or undefined values very well without the risk of runtime crash. Type inference − Swift supports a strong type system but it also incorporates with type interface to reduce the requirement of explicit type annotation. This means the Swift compiler is capable of analyzing the variables and expressions and determining their type. Automatic Memory Management − Swift uses Automatic Reference Counting to manage memory. It handles memory allocation and deallocation automatically without any lag. Because allocating memory manually is the common source of errors for the developers. Closures − Swift provides great support to the closures. Closure is a block code that can be referenced, passed around and executed later. They enhance the modularity and flexibility of the program. Protocol-Oriented Programming − Swift encourages protocol-oriented programming. It emphasizes the use of protocols to create blueprints for functionality. It creates reusable, modular and composable codes. Application of Swift Swift is primarily used for developing applications for Apple’s platform but it can also be used in developing applications for other platforms. Although Swift has numerous application which is impossible to note down, so we provide some of the major applications of Swift − iOS App Development − It is the most preferred language for developing applications for iOS devices like iPad, iPhone, etc. macOS Development − It is also used for creating applications, utilities, and software for the macOS operating system. watchOS App development − Using Swift we can also create a wide range of health applications for Apple Watch. tvOS App Development − With the help of Swift we can also create various entertainment apps for tvOS. Cross-Platform Development − Swift is not limited to Apple”s platform, we can also create applications for other platforms using cross-platform development. For example, the SwiftUI framework is used to create applications or interfaces that run on both Windows and iOS. Server-Side Development − Swift is also used in server-side development. It allows developers to develop web applications, APIs, and services using server-side Swift frameworks such as Vapour, Kitura and Perfect. Disadvantages of Swift Every programming language has its pros and cons. Similarly, Swift also has various advantages and disadvantages. So, some of the major disadvantages of Swift are − Swift is primarily developed for creating applications for Apple’s devices. So outside Apple’s ecosystem, it has limited use because developers have many alternative options for the cross-platform language. Swift is a new programming language so it does not have mature tools like other programming languages. As compared to Java, JavaScript or Python it has a small developer pool. The compatibility with non-Apple platforms is limited. Swift supports type inference and optimization, so if the project is larger then the compilation time is long. Print Page Previous Next Advertisements
Solidity – Types
Solidity – Types ”; Previous Next While writing program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Value Types Solidity offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types − Type Keyword Values Boolean bool true/false Integer int/uint Signed and unsigned integers of varying sizes. Integer int8 to int256 Signed int from 8 bits to 256 bits. int256 is the same as int. Integer uint8 to uint256 Unsigned int from 8 bits to 256 bits. uint256 is the same as uint. Fixed Point Numbers fixed/unfixed Signed and unsigned fixed point numbers of varying sizes. Fixed Point Numbers fixed/unfixed Signed and unsigned fixed point numbers of varying sizes. Fixed Point Numbers fixedMxN Signed fixed point number where M represents number of bits taken by type and N represents the decimal points. M should be divisible by 8 and goes from 8 to 256. N can be from 0 to 80. fixed is same as fixed128x18. Fixed Point Numbers ufixedMxN Unsigned fixed point number where M represents number of bits taken by type and N represents the decimal points. M should be divisible by 8 and goes from 8 to 256. N can be from 0 to 80. ufixed is same as ufixed128x18. Note: You can also represent the signed and unsigned fixed-point numbers as fixedMxN/ufixedMxN where M represents the number of bits taken by type and N represents the decimal points. M should be divisible by 8 and goes from 8 to 256. N can be from 0 to 80. address address holds the 20 byte value representing the size of an Ethereum address. An address can be used to get the balance using .balance method and can be used to transfer balance to another address using .transfer method. address x = 0x212; address myAddress = this; if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10); Print Page Previous Next Advertisements ”;
Socket.IO – Logging & Debugging ”; Previous Next Socket.IO uses a very famous debugging module developed by ExpresJS”s main author, called debug. Earlier Socket.IO used to log everything to the console making it quite difficult to debug the problem. After the v1.0 release, you can specify what you want to log. Server-side The best way to see what information is available is to use the * − DEBUG=* node app.js This will colorize and output everything that happens to your server console. For example, we can consider the following screenshot. Client-side Paste this to console, click enter and refresh your page. This will again output everything related to Socket.io to your console. localStorage.debug = ”*”; You can limit the output to get the debug info with incoming data from the socket using the following command. localStorage.debug = ”socket.io-client:socket”; You can see the result like the following screenshot, if you use the second statement to log the info − There is a very good blog post related to socket.io debugging here. Print Page Previous Next Advertisements ”;