Swift परिचय
What is Swift?
Swift एक general-purpose, compiled programming language है जिसे Apple ने Objective-C के एक modern replacement के रूप में बनाया। यह C जैसी compiled language की performance को modern languages की safety features के साथ combine करती है, जैसे strong typing और optionals जो crashes की एक पूरी category रोकते हैं।
उदाहरण: What is Swift?
print("Swift is a modern programming language")
print("It is safe, fast, and expressive")
Login to try C/C++/Java/PHP code in the editor
Why Swift Was Created
Apple ने Objective-C में long-standing pain points fix करने के लिए Swift introduce किया, जैसे unsafe pointers और verbose syntax।
Swift strong type safety, missing values explicitly handle करने के लिए optionals, और कहीं ज़्यादा साफ़ syntax जोड़ती है, जबकि demanding applications के लिए काफी तेज़ रहती है।
उदाहरण: Why Swift Was Created
// Declare a constant `goals`, set to `["safety", "speed", "expressiveness"]`
let goals = ["safety", "speed", "expressiveness"]
// Loop over `goals`, binding each item to `goal`
for goal in goals {
// Print "Design goal: \(goal)" to the console
print("Design goal: \(goal)")
}
Login to try C/C++/Java/PHP code in the editor
Where Swift Is Used
Swift iOS, iPadOS, macOS, watchOS, और tvOS apps को power करती है, लेकिन यह Apple के platforms से आगे server-side frameworks और general scripting में भी बढ़ी है इसके open-source toolchain के कारण।
Companies इसे mobile apps से लेकर backend services तक हर चीज़ के लिए उपयोग करती हैं।
उदाहरण: Where Swift Is Used
// Declare a constant `usedFor`, set to `[("iOS apps", "Instagram, Airbnb"), ("Backend", "Vapor servers"), ("Scripting", "automation tools")]`
let usedFor = [("iOS apps", "Instagram, Airbnb"), ("Backend", "Vapor servers"), ("Scripting", "automation tools")]
for (area, example) in usedFor {
// Print "\(area): \(example)" to the console
print("\(area): \(example)")
}
Login to try C/C++/Java/PHP code in the editor
Compiled, Not Interpreted
Python या JavaScript के विपरीत, Swift source code चलने से पहले optimized machine code में ahead of time compile किया जाता है। यह compiler को आपका program execute होने से पहले ही type errors और दूसरी गलतियां पकड़ने देता है, और fast-running binaries produce करता है।
swift file.swift चलाना एक step में compile और run करता है, यही कारण है कि यह scripting जैसा लगता है भले ही Swift compiled है।उदाहरण: Compiled, Not Interpreted
print("This code is compiled before it runs")
print("The compiler already checked it for errors")
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Swift सिर्फ iPhone apps बनाने के लिए काम करती है और इसे plain command-line programs या servers के लिए उपयोग नहीं किया जा सकता।
- Swift को Objective-C के साथ confuse करना; Swift अपनी खुद की syntax वाली एक पूरी तरह अलग, नई language है।
- यह सोचना कि Swift code को हमेशा Xcode के अंदर चलना चाहिए; यह
swiftcommand से एक terminal से भी चल सकता है।
- Swift एक modern, open-source, compiled language है जिसे Apple ने बनाया और 2014 में release किया।
- यह safe (compile time पर गलतियां पकड़ते हुए), fast (native machine code में compile होते हुए), और expressive होने के लिए design की गई है।
- Swift iOS/macOS apps के लिए उपयोग होती है, लेकिन server-side code, command-line tools, और scripting के लिए भी।
- Swift code
swiftcommand के साथ एक standalone script के रूप में चल सकता है, बिना एक पूरे app project की ज़रूरत के।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: