Swift Kya Hai




Swift IOS और OS X एप्लिकेशन Platforms के लिए एक नई Programming भाषा है जिसे C और Objective-C की संरचना के आधार पर विकसित किया गया है.

Swift सरल है और सुरक्षित Programming पैटर्न को Adopts करता है और Programming की आधुनिक विशेषताओं को जोड़ता है.

Swift मे Programming को आसान बनाने के लिए Programmers के लिए Flexible बेहतर और Upgraded की गई सुविधाओं के साथ जो अन्य Object Oriented Programming भाषाओं द्वारा प्रदान किए गये हैं.

Swift IOS और Mac OS के लिए app विकसित करने के लिए Apple’s की नई भाषा है.

और यह मोबाइल और Desktop में जगह सम्पादकीय Computer भाषा बनने के लिए विकसित है.

Swift आधुनिक Apple Engineering संस्कृति से Knowledge से बेहतर सोच में सबसे अच्छा Combination को जोड़ती है.

Swift खेल के मैदान की सुविधा के साथ आता है जहां Swift Programmers अपना कोड लिख सकते हैं और तुरंत परिणाम देखने के लिए उसे Execute कर सकते हैं.

Swift की विशेषताएँ

  • यह आधुनिक Programming सुविधाएं प्रदान करती है.

  • यह कोड को पढ़ने और समझने में बहुत आसान है.

  • यह सुरक्षित Programming पैटर्न का उपयोग करती है.

  • यह C और Objective-C का Syntax प्रदान करती है.

Swift in Hindi Basic Syntax

हमने Swift कार्यक्रम का एक टुकड़ा पहले ही Environment की स्थापना के दौरान देखा है.

अब तक हमने देखा है कि एक सरल Hello Program के साथ code लिखने के संकलन और Execute करने के लिए Environment कैसे स्थापित किया जाए.

अगर आप IOS Platform के लिए ऊपर Code Snippet लिखना चाहते हैं तो आपको UIKit को Import करना होगा और Program में इसमें बदलाव आएगा और यह इस प्रकार दिखाई देगा.

import UIKit
var myString = "Hello, Tutorialsroot!"
println(myString)

Swift में Import स्टेटमेंट का उपयोग किसी भी Objective-C Framework या Library को सीधे आपके Swift Program में Import करने के लिए किया जाता है.

Swift Programmers में Swift Applications से C और C ++ को भी जोड़ सकते हैं.

Comments in Swift

Comments Swift Program की वे पंक्तियां हैं जो संकलन के समय Swift Compiler द्वारा संकलित नहीं होती हैं. वे Code Snippet के कामकाज को समझने के लिए Programmers के लिए ग्रंथों की सहायता करने और Compiler द्वारा अनदेखा कर देता है.

ज्यादातर Comments / * के साथ शुरू होता हैं और * / और उन तारों के बीच समाप्त होता हैं जो Comments के रूप में कार्य करती है.

import UIKit

/* This is comment section */

var str = "Hello Tutorialsroot"

println(str)

/* This is another comment :: end of code */

Swift in Hindi Variables

एक Variables हमें Name संग्रहण प्रदान करता है जो हमारे Programs को संचालित कर सके हैं.

Variables Name में Mathematical प्रतीकों, तीरों, निजी-उपयोग Unicode Code Points या Line और Box Drawing वर्ण शामिल नहीं हो सकते हैं.

Variables और न ही वे एक संख्या से शुरू कर सकते हैं, हालांकि संख्याओं को Name के अंदर कहीं भी शामिल किया जा सकता है.

Swift में प्रत्येक Variables एक विशिष्ट प्रकार है, जो Variables की स्मृति का Size और Layout को निर्धारित करता है और Operations का सेट जो कि Variables पर लागू किया जा सकता है.

Variable Declaration

एक Variable Declaration किस प्रकार से Compiler को Type करता है. Variable के लिए Storage कैसे और कितना बना सकता है Variables की Storage Var Keyword का उपयोग करके की जाती हैं.

mport Cocoa

var firstvar = 58

println(firstvar)

Output

58

Type Annotations

जब आप एक Variable Declared करते हैं तो एक प्रकार की Annotation प्रदान कर सकते हैं ताकि Variable स्टोर किए जा सकने वाले मूल्यों के बारे में स्पष्ट किया जा सके.

Syntax

var variableName:<data type> = <optional initial value>

import Cocoa

var varA = 58
println(varA)

var varB:Float

varB = 3.14159
println(varB)

Output

Swift in Hindi Data Types

Swift विभिन्न प्रकार के Data Types प्रदान करता है जो कि Programmers को इसके मूल्य को सेट करने के लिए Variable के लिए उचित प्रकार का चयन करने की अनुमति देता है.

किसी भी Programming भाषा में Programming करते समय आपको सूचनाओं को संग्रहीत करने के लिए विभिन्न प्रकार के variables का उपयोग करने की आवश्यकता होती है.

Variable value को स्टोर करने के लिए कुछ भी नहीं है लेकिन Memory Space Reserved होता हैं इसका मतलब है कि जब आप एक Variable बनाते हैं तो आप Memory में कुछ Space Reserved करते हैं.

आप String, Character, Wide Character, Integer, Floating Point, Boolean आदि जैसे विभिन्न Data Types की जानकारी स्टोर करना चाहते हैं.

आपको किसी Variable के Data Types के आधार पर, Memory को Os करना होता है है और तब तय करता है कि Os में क्या Stores किया जा सकता है.

Built-in Data Types

Swift भाषा मे विभिन्न प्रकार के Data Types प्रदान किए जाते हैं.

Integers

यह संपूर्ण संख्याओं के लिए प्रयोग किया जाता है और अधिक विशेष रूप से, आप 32 या 64 bit Integer को परिभाषित करने के लिए Int32, Int64 का उपयोग कर सकते हैं. जबकि 32 या 64 bit Unsigned Integer Variables को परिभाषित करने के लिए UInt32 या UInt64 का उपयोग करते है उदाहरण के लिए, 42 और -23.

Float

इसका उपयोग 32-bit Floating-point Number और छोटे दशमलव अंकों वाले नंबरों का प्रतिनिधित्व करने के लिए किया जाता है. उदाहरण के लिए, 3.1415 9, 0.6, और -273.158 Swift दो Floating Point Number प्रकारों वाले Programmers प्रदान करता है.

Double

यह एक Bit Floating Point Number का प्रतिनिधित्व करता है. Programmer को इसका उपयोग करने की आवश्यकता होती है जब Floating Point Value बहुत बड़ी होती हैं और या विशेष रूप से सटीक होती हैं.

Bool

यह एक Boolean Value का प्रतिनिधित्व करता है जो या तो True या False है.

String

यह एक Unit के रूप में Character का एक आदेश संग्रह है जैसे की, "Hello.

Character

यह एक Single Character वाला अक्षर है जैसे की "C".

Optional

Swift एक अन्य सुविधा प्रदान करता है जहां Programmer एक Variable का प्रतिनिधित्व कर सकते हैं जो Value या कोई Value नहीं रख सकते हैं.

नीचे एक ऐसा Table है जिसमें अधिकतम और न्यूनतम Value दिखाया जा सकता है जिसे इस प्रकार के Variable में संग्रहीत किया जा सकता है.

Int8 1byte -127 to 127
UInt8 1byte 0 to 255
Int32 4bytes -2147483648 to 2147483647
UInt32 4bytes 0 to 4294967295
Int64 8bytes -9223372036854775808 to 9223372036854775807
UInt64 8bytes 0 to 18446744073709551615
Float 4bytes 1.2E-38 to 3.4E+38 (~6 digits)
Double 8bytes 2.3E-308 to 1.7E+308 (~15 digits)

Swift in Hindi Constants

Constants किसी भी Data Types की तरह हो सकता है जैसे कि Integer Constant या एक Floating Constant या एक Character Constant या एक String शब्दश है.

Constants की Value को बदला नहीं जा सकता है जबकि भविष्य में एक Variable को अलग-अलग Value पर सेट किया जाये.

Constants Swift Program में निश्चित मानों का उल्लेख करता है जो कार्यक्रम के निष्पादन के दौरान नहीं बदलता है.

Constants को केवल नियमित Variable की तरह व्यवहार किया जाता है इसके अलावा Data यह है कि उनकी परिभाषा के बाद उनकी Values को संशोधित नहीं किया जा सकता है.

Constants किसी भी Data Types में से कोई भी हो सकता है.

  • Integer Constants

  • Floating Constants

  • Character Constant

  • String Literal

Constants Declaration

इससे पहले कि आप Constants का उपयोग करें, आपको निम्न Keyword को दिए जाने के बारे में उन्हें घोषित करना होता है.

let constantName = <initial value>

Swift में एक Constants घोषित करने का तरीका दिखाने के लिए आसान सा उदाहरण.

import Cocoa

let constA = 42
println(constA)

Output

42

Swift in Hindi Operators

Operators एक Symbol है जो कम्पाइलर को विशिष्ट Mathematical या Logical Manipulations करने के लिए कहता है.

Objective-C अंतर्निहित Operators में समृद्ध है और निम्न प्रकार के Operator प्रदान करता है.

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • Logical Operators

  • Bitwise Operators

  • Misc Operators

  • Range Operators

Arithmetic Operators

Swift सभी Standard Types के लिए चार मानक Standard Arithmetic Operators का समर्थन करता है.

Operators Description
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
−− Decrement

Assignment Operator

Assignment Operator Variables को Value प्रदान करने के लिए उपयोग किया जाता है.

Operators Description
= Assign
+= Increments, then assigns
-+ Decrements, then assigns
*= Multiplies, then assigns
/= Divides, then assigns
%= Modulus, then assigns

Comparison Operators

Swift भाषा सभी Comparison Operators को दिखाती है.

Operators Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operators

Swift भाषा Logical Operators को Support करती है.

Operators Description
&& And और Operators दो Expressions पर एक Logical Conjunction का निष्पादन करते है (यदि दोनों Expressions True का Evaluation करते हैं, तो परिणाम True है .अगर किसी भी अभिव्यक्ति का Evaluation False है, तो परिणाम False है).
|| Or Operators दो Expressions पर एक Logical Disjunction का निष्पादन करता है (यदि दोनों या Expressions दोनों True का Evaluation करते हैं, तो परिणाम True है).
! Not Operator एक Expression पर Logical Negation करता है.

Bitwise Operator

Bitwise Operators Bits पर काम करते हैं और Bits ऑपरेशन द्वारा Bits करते हैं.

Operators Description
& Binary AND Operator
| Binary OR Operator
^ Binary XOR Operator
<< Binary Left Shift Operator
>> Binary Right Shift Operator

Swift in Hindi Characters

Swift में एक Character String Literal है जिसे Data Type Character द्वारा संबोधित किया गया है.

यह दो Character Constants का उपयोग करता है.

import Cocoa

let char1: Character = "X"
let char2: Character = "Y"

println("Value of char1 \(char1)")
println("Value of char2 \(char2)")

Output

यदि आप एक Character Type को Variable या स्थिर में एक से अधिक वर्ण को संग्रहित करने का प्रयास करते हैं तो Swift उसको अनुमति नहीं देता है.

Swift Playground में निम्नलिखित उदाहरण Type करने का प्रयास करें और संकलन से पहले आपको एक Error मिल जाएगी.

import Cocoa

let char: Character = "XY"

println("Value of char \(char)")

Output

Empty Character Variables

खाली Character Variable या निरंतर बनाने के लिए संभव नहीं है, जिसमें खाली मूल्य होगा.

import Cocoa

let char1: Character = ""
var char2: Character = ""

println("Value of char1 \(char1)")
println("Value of char2 \(char2)")

Output

Swift in Hindi Strings

Swift में Strings वर्णों का एक आदेश संग्रह है, जैसे "Hello, World" और वे Swift Data Type String द्वारा प्रस्तुत किए जाते हैं जो बदले में वर्ण प्रकार के Values का संग्रह दर्शाता है.

आप एक Strings का उपयोग कर या Strings क्लास का एक उदाहरण बनाकर निम्नानुसार बना सकते हैं.

import Cocoa

var stringA = "Hello, Tutorialsroot!"
println( stringA )

var stringB = String("Hello, Tutorialsroot!")
println( stringB )

Output

Empty String

आप खाली String का उपयोग कर एक खाली String बना सकते हैं या नीचे दिखाए गए अनुसार String Class का एक उदाहरण बना सकते हैं.

आप यह भी जांच सकते हैं कि कोई String रिक्त है या Boolean Property का उपयोग नहीं किया जा रहा है.

import Cocoa

var stringA = ""

if stringA.isEmpty {
   println( "stringA is empty" )
}else {
   println( "stringA is not empty" )
}

let stringB = String()

if stringB.isEmpty {
   println( "stringB is empty" )
}else {
   println( "stringB is not empty" )
}

Output

String Constants

आप निर्दिष्ट कर सकते हैं कि क्या आपके String को किसी Variable को निर्दिष्ट करके संशोधित किया जा सकता है. यह नीचे निरस्त किए गए Keyword के रूप में Keyword का उपयोग करके निरंतर को निर्दिष्ट करके स्थिर होगा.

import Cocoa

var stringA = "Hello, Swift!"
stringA + = "--Readers--"
println( stringA )

let stringB = String("Hello, Swift!")
stringB + = "--Readers--"
println( stringB )

Output

Swift in Hindi Loops

Loops एक विशिष्ट Block Codes के खंड को दोहराने के लिए एक कार्यक्रम में जाना जाता है. इससे एक बार फिर से लिखने का कार्य कम हो जाता है.

सामान्य Statements में क्रमिक रूप से क्रियान्वित किया जाता है. Function में पहला विवरण पहले निष्पादित होता है, दूसरे के बाद, और इसी तरह होता रहता है.

Programming भाषाएं विभिन्न नियंत्रण संरचनाएं प्रदान करती हैं जो अधिक Complicated Execution Paths की अनुमति देती हैं.

एक Loop Statement हमें Statement या कई बार Statements का समूह निष्पादित करने की अनुमति देता है.

अधिकांश Programming भाषाओं में एक Loop Statement के बाद सामान्य है.

Swift Programming भाषा Loop आवश्यकताओं को Maintenance करने के लिए निम्न प्रकार की Looping प्रदान करती है.

For-in Loops

आप अनुक्रम या चीज़ों के संग्रह पर पुनरावृति करने के लिए For-in Loops का उपयोग करते हैं .जैसे कि सरणी या शब्दकोश के तत्व String में Characters या Numbers की Category यहाँ सामान्य रूप है.

Syntax

for index in sequence {
statements
}

उपर्युक्त उदाहरण में, यह कई संख्याओं से अधिक चलती है Loop Index Variable (i) प्रत्येक बार Loop के माध्यम से Category में अगली संख्या के मान लेता है.

import Cocoa

var someInts:[Int] = [10, 20, 30]

for index in someInts {
   println( "Value of  index is \(index)")
}

Output

For Loop

Loop के लिए Repetition नियंत्रण संरचना है जो आपको Efficiently से एक Loop लिखने की अनुमति देता है जिसे किसी विशेष संख्या को निष्पादित करने की आवश्यकता होती है.

Syntax

for init; condition; increment{
   statement(s)
}

For Example

import Cocoa

var someInts:[Int] = [10, 20, 30]

for var index = 0; index < 3; ++index {
   println( "Value of someInts[\(index)] is \(someInts[index])")

Output

While Loop

Swift Programming भाषा में Loop Statement में एक बार जब तक दी गई Condition True है तब तक एक Target Statement का Execution किया जाता है.

Syntax

while condition {
   statement(s)
}

Programmers Loop का उपयोग कर सकते हैं जो कि For-condition-increment Loop की कार्यक्षमता को दोहराने के लिए निम्नानुसार हो.

import Cocoa
 
var index = 10

while index < 25 {
   println( "Value of index is \(index)")
   index = index + 1
}

Output

do...while Loop

Loop के Opposite और Loop के Opposite जो Loop के शीर्ष पर Loop की स्थिति का परीक्षण करते हैं. जबकि Loop पाश के निचले भाग में स्थित इसकी स्थिति जांचता है.

एक Do...While Loop एक While Loop के समान है, सिवाय इसके कि Do...While Loop जबकि Loop को कम से कम एक बार गारंटी के साथ निष्पादित नहीं कर देता है.

Syntax

do {
   statement(s);
}while( condition );

For Example

import Cocoa
 
var index = 10

do {
   println( "Value of index is \(index)")
   index = index + 1
}while index < 20 

Output

Swift in Hindi Arrays

Swift Arrays को किसी विशेष प्रकार के Sequential सूचियों में कई मानों को संग्रहीत करने के लिए लागू किया जाता है.

Swift ने सख्त जांच तंत्र की स्थापना की, जो Programmer को गलती से किसी भी गलत प्रकार में प्रवेश करने या सम्मिलित करने की अनुमति नहीं देता है. अर्थात Swift Programming में Arrays को वे प्रकार के मूल्यों के बारे में विशिष्ट हैं जो वे स्टोर कर सकते हैं.

एक ही Value कई स्थानों पर कई बार Arrays में प्रकट हो सकता है swift Arrays Objective-C’s के NSArray और NSMutableArray क्लास से अलग-अलग होते हैं जो किसी भी प्रकार की Object को स्टोर कर सकते हैं और जिन वस्तुओं की वापसी की प्रकृति के बारे में कोई जानकारी नहीं देते.

Swift में, किसी विशेष Array को स्टोर करने वाले मानों का प्रकार हमेशा स्पष्ट होता है या तो एक स्पष्ट प्रकार के annotation के माध्यम से, या प्रकार के अनुमान के माध्यम से, और एक क्लास प्रकार नहीं होना चाहिए.

Swift Arrays के types सुरक्षित होते हैं.

Accessing and Modifying Arrays

Arrays को Arrays के अंत में नई वस्तुओं को सम्मिलित करने के लिए Append () विधि या अतिरिक्त Assignment Operator (+ =) का उपयोग करके आसानी से संशोधित किया जा सकता है.

var arr = [Int]()

arr.append(100)

arr.append(200)

arr += [300]

var arr2 = arr[0]

print( "First element of array is \(arr2)" )

print( "Second element of array is \(arr[1])" )

print( "Third element of array is \(arr[2])" )

Output

Iterating Over an Array

आप Array के संपूर्ण सेट पर Iterate करने के लिए For in Loop का उपयोग कर सकते हैं.

import Cocoa

var someStrs = [String]()

someStrs.append("Facebook")
someStrs.append("YouTube")
someStrs += ["Google"]

for item in someStrs {
   println(item)
}

Output

The Empty Property

Programmers सामान्यत केवल पढ़ने के लिए खाली Array की Property का पता लगाने के लिए उपयोग करते हैं कि क्या कोई Array खाली है या नहीं.

import Cocoa

var arr1 = [Int](count:2, repeatedValue: 2)

var arr2 = [Int](count:3, repeatedValue: 4)

var arr3 = [Int]()

var arr4 = [Int](count:3, repeatedValue: 6)

println("arr1.isEmpty = \(arr1.isEmpty)")

println("arr2.isEmpty = \(arr2.isEmpty)")

println("arr3.isEmpty = \(arr3.isEmpty)")

println("arr4.isEmpty = \(arr4.isEmpty)")

Output

Swift in Hindi Decision Making

Decision Making वाली संरचनाओं के लिए आवश्यक है कि Programmer एक या अधिक शर्तों को निर्दिष्ट करता है कि Programmer द्वारा Evaluation या परीक्षण किया जा सकता है साथ ही एक Statement या Description के साथ निष्पादित किया जाता है. यदि स्थिति True होने के लिए निर्धारित होती है और वैकल्पिक रूप से, अन्य Statement का निष्पादन Condition False होना निर्धारित है.

Swift अपने Programming के अंतर्गत निम्नलिखित प्रकार के Decision Making Statements प्रदान करता है.

  • If Statement

  • If….else Statement

  • Else if Statement

  • Nested if Statement

  • Switch Statement

If Statement

सभी Programming भाषा में सबसे पहले और सबसे आम है अगर यह Users के अनुमति के माध्यम से कार्यक्रम के प्रवाह को नियंत्रित करता है. यह Conditional Statement केवल तभी काम करता है जब Condition True हो.

Syntax

if boolean_expression {

/* statement(s) will execute if the boolean expression is true */

}

For Example

import Cocoa

var a1:Int = 60;

if a1 > 42 {

/* if the condition is true then print */

println("1st Varable is greater than 42");

}
println("The value of variable is \(a1)")

if…else Statement

अगर यहां एक वैकल्पिक और Description के साथ अगर Statement का पालन किया जाता है और यह Description केवल Boolean Expression (i.e. the condition) false हो जाती है तो उसे केवल Execute किया जाता है.

Syntax

if boolean_expression {

// statement(s) will execute if the boolean expression is true

} else {

//  otherwise statement(s) will execute if the boolean expression is false

}

For Example

var g:Int = 1000;

if g < 60 {

println("Value is less than 60");

} else {

println("Value is not less than 60");

}

println("Value of variable is \(g)");

else if Statement

एक Statement अगर एक वैकल्पिक और else if…else Statement, जो एक से अधिक Conditions का परीक्षण करने के लिए बहुत ही उपयोगी है और इसे पीछा किया जा सकता है अगर if…else if Statement है.

Syntax

if boolean_expression_1 {

} else if boolean_expression_2 {

.

} else if boolean_expression_n {

. . . . . .

} else {

}

For Example

import Cocoa

var varA:Int = 100;

/* Check the boolean condition using if statement */
if varA == 20 {
   /* If condition is true then print the following */
   println("varA is equal to than 20");
} else if varA == 50 {
   /* If condition is true then print the following */
   println("varA is equal to than 50");
} else {
   /* If condition is false then print the following */
   println("None of the values is matching");
}
println("Value of variable varA is \(varA)");

Nested if Statement

यह हमेशा Swift में Legal होता है यदि Nest if-else Statement जिसका मतलब है कि Programmers एक if or else if Statement कर सकते हैं यदि Statement अगर if or else if Statement के अंदर हो.

Syntax

if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
   if boolean_expression_2 {
      /* Executes when the boolean expression 2 is true */
   }
}

For Example

import Cocoa

var varA:Int = 200;
var varB:Int = 300;

/* Check the boolean condition using if statement */
if varA == 200 {
   /* If condition is true then print the following */
   println("First condition is satisfied");
	
   if varB == 300 {
      /* If condition is true then print the following */
      println("Second condition is also satisfied");
   } 
}
println("Value of variable varA is \(varA)");
println("Value of variable varB is \(varB)");

Switch Statement

Swift में एक Switch Statement अपने निष्पादन को पूरा करता है जैसे ही बाद के मामलों के नीचे से गिरने की जगह जैसे कि C और C ++ Programing भाषाओं में होता है, जैसे पहले मिलान केस पूरा हो जाता है। निम्नलिखित C और C ++ में switch Statement का एक सामान्य सिंटैक्स है .

Syntax

switch expression {

case expression1  :

statement(s)

fallthrough // optional

case expression2, expression3  :

statement(s)

fallthrough //optional

default : // Optional

statement(s);

}

For Example

import Cocoa

var value = 20

switch index {

case 60  :

println( "Value of index is 60")

fallthrough

case 20,30  :

println( "the value is either 20 or 30")

fallthrough

case 40  :

println( "The value is 40")

default :

println( "Wrong Case Input")

}