الرئيسية / مقرر تطبيقات الموبايل / الفصل 1 الجزء 2

الفصل 1 (الجزء 2): The iOS layers and MVC

Four layers, what each one gives you, and who does what in an app

على طول: طبقات iOS الأربعة، وبعدها MVC يعني مين بيعمل إيه.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

طبقات iOS الأربعة
دورة MVC

أمثلة كود

أمثلة قصيرة وبسيطة

كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.

المثال ١: موديل بسيط

struct Note {                 // Model: data and rules only    var text: String    var done: Bool = false}var notes = [Note(text: "Buy milk"), Note(text: "Call Ali")]notes[0].done = truelet doneCount = notes.filter { $0.done }.countprint(doneCount)

    

المثال ٢: من الطبقة العليا للوسطى

import UIKitimport CoreGraphics let label = UILabel()          // Cocoa Touchlabel.text = "Hello"let size = CGSize(width: 100, height: 50)   // Media Servicesprint(size.width * size.height)print(label.text ?? "")

    

المثال ٣: الكنترولر بيعدّل الموديل

struct Counter {              // Model    var value = 0    mutating func add() { value += 1 }}var model = Counter()print("before: " + String(model.value))model.add()print("after: " + String(model.value))

    

اختبار قصير

اختبر فهمك

1. Which layer holds UIKit and SwiftUI?

2. Foundation (strings, arrays, dates) belongs to:

3. In MVC, the business rules live in:

4. A view should not:

5. Drawing a shape yourself with CoreGraphics means you moved down to:

تمارين

جرّب بإيدك

التمرين 1

صنّف أربع أدوات على طبقاتها الصح: UIKit، CoreGraphics، Foundation، CoreLocation. اطبع كل واحدة وجنبها اسم الطبقة.

Classify four frameworks into their layers: UIKit, CoreGraphics, Foundation, CoreLocation, and print each with its layer.

Starter code:

// TODO: print each framework with its layer

التمرين 2

اكتب موديل Note فيه text و done، وكنترولر عنده دالة rowsText() بترجّع سطر لكل ملاحظة فيه علامة [x] أو [ ]، واطبع الناتج لملاحظتين.

Write a Note model with text and done, and a controller with rowsText() returning one line per note marked done or not, then print it for two notes.

Starter code:

struct Note {    var text: String    var done: Bool}// TODO: a controller with rowsText() -> String

مشروع صغير في آخر المحاضرة

شاشة ملاحظات بـ MVC

الداتا: notes: our own list of five short notes, no real user data.

License / source: Our own sample data; Swift array reference by Apple (link only). Reference page

الخطوات

  1. اعمل موديل Note فيه text و done.
  2. خزّن خمس ملاحظات في مصفوفة جوه الموديل، منهم اتنين خلصوا.
  3. اعمل دالة في الكنترولر بترجّع سطر لكل ملاحظة، وعلامة [x] للخلصان و[ ] لغيره.
  4. اطبع السطور، وبعدها اطبع عدد اللي خلصوا. المتوقع تحت.

Starter code:

struct Note {    var text: String    var done: Bool}struct NotesModel {    var notes: [Note] = [        Note(text: "Buy milk", done: true),        Note(text: "Call Ali", done: false),        Note(text: "Pay the bill", done: false),        Note(text: "Read 10 pages", done: true),        Note(text: "Walk 20 minutes", done: false)    ]}struct NotesController {    let model: NotesModel    // TODO: rowsText() and doneCount()}

مشروع نهاية الفصل

ملف المشروع العملي للفصل 1

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the course notes "Mobile Application Development 1" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Apple developer documentation: Swift and the iOS layers (developer.apple.com/documentation).