فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: موديل بسيط
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
print("UIKit -> Cocoa Touch")
print("CoreGraphics -> Media Services")
print("Foundation -> Core Services")
print("CoreLocation -> Core Services")
Expected output
UIKit -> Cocoa Touch CoreGraphics -> Media Services Foundation -> Core Services CoreLocation -> Core Services
التمرين 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
struct Note {
var text: String
var done: Bool
}
struct NotesController {
var notes: [Note]
func rowsText() -> String {
return notes.map { ($0.done ? "[x] " : "[ ] ") + $0.text }.joined(separator: "\n")
}
}
let c = NotesController(notes: [Note(text: "Buy milk", done: true), Note(text: "Call Ali", done: false)])
print(c.rowsText())
Expected output
[x] Buy milk [ ] Call Ali
مشروع صغير في آخر المحاضرة
شاشة ملاحظات بـ 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
الخطوات
- اعمل موديل Note فيه text و done.
- خزّن خمس ملاحظات في مصفوفة جوه الموديل، منهم اتنين خلصوا.
- اعمل دالة في الكنترولر بترجّع سطر لكل ملاحظة، وعلامة [x] للخلصان و[ ] لغيره.
- اطبع السطور، وبعدها اطبع عدد اللي خلصوا. المتوقع تحت.
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()}
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
func rowsText() -> String {
return model.notes.map { ($0.done ? "[x] " : "[ ] ") + $0.text }.joined(separator: "\n")
}
func doneCount() -> Int {
return model.notes.filter { $0.done }.count
}
}
let c = NotesController(model: NotesModel())
print(c.rowsText())
print("done: " + String(c.doneCount()) + " of " + String(c.model.notes.count))
Expected output
[x] Buy milk [ ] Call Ali [ ] Pay the bill [x] Read 10 pages [ ] Walk 20 minutes done: 2 of 5
مشروع نهاية الفصل
ملف المشروع العملي للفصل 1
ملف 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).