Swift के Playgrounds
In this page:
What a Playground Is
एक Swift playground एक interactive scratchpad है, Xcode और standalone Swift Playgrounds app में built, जहां code की हर line चलती है और इसका result तुरंत एक sidebar में दिखाती है।
यह एक पूरा project बनाए बिना ideas के साथ जल्दी experiment करने के लिए design किया गया है।
उदाहरण: What a Playground Is
let message = "Playgrounds show results instantly"
print(message)
Login to try C/C++/Java/PHP code in the editor
Command-Line Scripts as a Playground Alternative
Xcode के बाहर, swift command से चलाई एक plain .swift file similar तेज़ feedback देती है: top-level statements लिखें और तुरंत printed output देखने के लिए file चलाएं, जो बिल्कुल वही है जैसे इस course के examples काम करते हैं।
.swift file के रूप में save करें और swift filename.swift चलाएं।उदाहरण: Command-Line Scripts as a Playground Alternative
// Declare a constant `numbers`, set to `[1, 2, 3, 4]`
let numbers = [1, 2, 3, 4]
// Declare a constant `doubled`, set to `numbers.map { $0 * 2 }`
let doubled = numbers.map { $0 * 2 }
// Print `doubled` to the console
print(doubled)
Login to try C/C++/Java/PHP code in the editor
Good Uses for a Playground
Playgrounds किसी logic के छोटे टुकड़े को test करने, एक नई API explore करने, या किसी language feature को एक बड़े codebase में commit करने से पहले यह जांचने के लिए सबसे अच्छे हैं कि यह कैसे behave करता है, क्योंकि आपको एक पूरा app build करने के overhead के बिना instant visual feedback मिलता है।
उदाहरण: Good Uses for a Playground
// Loop over `1...5`, binding each item to `n`
for n in 1...5 {
// Print "Testing square of \(n): \(n * n)" to the console
print("Testing square of \(n): \(n * n)")
}
Login to try C/C++/Java/PHP code in the editor
- यह मानना कि Swift लिखने के लिए playgrounds ज़रूरी हैं; वे एक convenience tool हैं, कोई mandatory step नहीं।
- किसी playground में performance-heavy code लिखना और confused होना कि यह compiled app से धीमा क्यों चलता है -- playgrounds live feedback के लिए code को line by line interpret करते हैं।
- यह भूल जाना कि
PlaygroundPage.currentजैसी playground-only APIs एक normal compiled command-line program में मौजूद नहीं।
- Playgrounds Xcode और Swift Playgrounds app में available interactive Swift environments हैं।
- वे हर line का result तुरंत दिखाते हैं, जो सीखने और experiment करने के लिए बढ़िया है।
- Command-line
.swiftscripts (swift file.swiftसे चलाए गए) बिना Xcode चाहिए similar instant feedback देते हैं। - Playgrounds किसी असली project में जोड़ने से पहले छोटे snippets test करने के लिए आदर्श हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: