← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 3 of 7

Swift के Playgrounds

एक playground एक special sandbox है जहां आप Swift code type कर सकते हैं और तुरंत देख सकते हैं कि यह क्या करता है, बिना एक पूरा app build किए।

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

markup
let message = "Playgrounds show results instantly"
print(message)

Command-Line Scripts as a Playground Alternative

Xcode के बाहर, swift command से चलाई एक plain .swift file similar तेज़ feedback देती है: top-level statements लिखें और तुरंत printed output देखने के लिए file चलाएं, जो बिल्कुल वही है जैसे इस course के examples काम करते हैं।

Note: इस course का हर example इसी तरह चलाया जा सकता है: इसे एक .swift file के रूप में save करें और swift filename.swift चलाएं।

उदाहरण: Command-Line Scripts as a Playground Alternative

markup
// 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)

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

markup
// 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)")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि Swift लिखने के लिए playgrounds ज़रूरी हैं; वे एक convenience tool हैं, कोई mandatory step नहीं।
  2. किसी playground में performance-heavy code लिखना और confused होना कि यह compiled app से धीमा क्यों चलता है -- playgrounds live feedback के लिए code को line by line interpret करते हैं।
  3. यह भूल जाना कि PlaygroundPage.current जैसी playground-only APIs एक normal compiled command-line program में मौजूद नहीं।
चैप्टर सारांश
  • Playgrounds Xcode और Swift Playgrounds app में available interactive Swift environments हैं।
  • वे हर line का result तुरंत दिखाते हैं, जो सीखने और experiment करने के लिए बढ़िया है।
  • Command-line .swift scripts (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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.