0Pricing
SwiftUI Academy · درس

فك ترميز JSON باستخدام Codable

حوّل JSON إلى هياكل نماذج Swift

فك ترميز JSON باستخدام Codable درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why JSON Needs Decoding

APIs speak JSON, a text format Swift cannot use directly. You decode that text into real Swift types you can work with. 📦

Meet Codable

The Codable protocol lets a type convert itself to and from JSON automatically, so you rarely write parsing code by hand.

A Codable Struct

Just add Codable to your struct. Swift generates the encoding and decoding for every stored property for you.

struct User: Codable {
    let id: Int
    let name: String
}

Matching JSON Keys

Swift maps each property name straight to the matching JSON key. So "name" in JSON fills the name property exactly.

{ "id": 1, "name": "Ada" }

Decoding with JSONDecoder

A JSONDecoder turns raw Data into your type. You tell it which type to expect, and it does the rest.

let user = try JSONDecoder()
    .decode(User.self, from: data)

Working With Data

Network responses arrive as Data, a bag of bytes. The decoder reads those bytes and produces your typed value.

Decoding Arrays

To decode a JSON list, ask for an array of your type. One call gives you a ready-to-use Swift array. 🎉

let users = try JSONDecoder()
    .decode([User].self, from: data)

Renaming with CodingKeys

When JSON keys differ from your names, add CodingKeys to map a snake_case key onto a clean Swift property.

enum CodingKeys: String, CodingKey {
    case name = "full_name"
}

Optional Properties

Mark a property optional when the key may be missing. The decoder simply sets it to nil instead of throwing.

let avatar: String?

Decoding Can Throw

If the JSON shape does not match your type, decoding throws. Always wrap the call in try so you can handle bad data.

Nested Models

Nested JSON objects become nested structs. Make each level Codable and Swift decodes the whole tree at once.

struct Post: Codable {
    let author: User
}

Quick Check

Quick check on turning JSON into Swift types.

Recap: Codable

You learned that Codable plus JSONDecoder maps JSON into typed Swift values, with CodingKeys for renames and optionals for missing keys. 🙌

الأسئلة الشائعة

هل درس «فك ترميز JSON باستخدام Codable» مجاني؟

نعم — نص درس «فك ترميز JSON باستخدام Codable» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «فك ترميز JSON باستخدام Codable»؟

حوّل JSON إلى هياكل نماذج Swift تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «فك ترميز JSON باستخدام Codable»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فك ترميز JSON باستخدام Codable
  2. استدعاء APIs باستخدام URLSession
  3. معدّل .task
  4. حالات التحميل والخطأ
← العودة إلى SwiftUI Academy