0Pricing
SwiftUI Academy · درس

إنشاء الكيانات وحفظها

أدرج السجلات واحفظ السياق

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

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

Adding New Data

Reading is half the story. To grow your app you also need to create new records and store them safely.

You Need the Context

Every new object is born inside a managed object context, so grab the context from the environment first.

@Environment(\.managedObjectContext) var context

Making an Instance

Create an entity by initialising it with the context, which registers the new object for tracking.

let task = Task(context: context)

Setting Attributes

Assign values to the object's attributes just like setting properties on any Swift object.

task.title = "Buy milk"
task.isDone = false

Nothing Is Saved Yet

At this point the record only lives in memory. It becomes permanent when you save the context.

Calling save()

Persist all pending changes at once with a single save call, wrapped in a do-catch for safety.

do {
    try context.save()
} catch {
    print("Save failed: \(error)")
}

Saving Is Transactional

One save writes every pending insert, edit, and delete together, so your store never lands half-updated.

Updating Existing Records

To edit, change the object's attributes and call save again. Core Data tracks what is dirty for you.

Deleting a Record

Remove an object with delete on the context, then save to make the removal stick.

context.delete(task)
try? context.save()

Why Catch Errors

Saving can fail, for example if a required value is missing, so handling the error keeps your app from crashing.

Save After Each Action

A common habit is to save right after the user adds, edits, or deletes, so work is never lost on a sudden close.

Quick Check

You created and stored a record. One quick question about the flow.

Recap

You learned to create an entity with the context, set its attributes, and call save to insert, update, or delete records. ✨

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

هل درس «إنشاء الكيانات وحفظها» مجاني؟

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

ماذا ستتعلم في «إنشاء الكيانات وحفظها»؟

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

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

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

كم من الوقت يستغرق درس «إنشاء الكيانات وحفظها»؟

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

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

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

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

  1. حزمة Core Data
  2. الجلب باستخدام @FetchRequest
  3. إنشاء الكيانات وحفظها
  4. المسندات الشرطية ومسندات الترتيب
← العودة إلى SwiftUI Academy