فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: var بتتغير و let لأ
var rows = 30 // can changerows = 40let maxRows = 100 // cannot changeprint(rows)print(maxRows)maxRows = 200 // the compiler refuses this line
المثال ٢: Swift بيستنتج النوع
let count = 10 // Intvar price = 100.5 // Doublevar message = "Swift is nice!" // Stringprint(type(of: count))print(type(of: price))print(type(of: message))print(count + 5)print(price * 2)
المثال ٣: دالة بترجّع قيمة
func circleArea(radius: Double) -> Double { return 3.14159 * radius * radius}let a = circleArea(radius: 10)print(a)print(circleArea(radius: 2))
اختبار قصير
اختبر فهمك
1. Which line creates a value that cannot change?
2. Swift type inference means:
3. Which one is a limitation of mobile devices, not a capability?
4. In func circleArea(radius: Double) -> Double, what does the arrow part say?
5. Xcode runs on:
تمارين
جرّب بإيدك
التمرين 1
اعمل var اسمها total بتبدأ بـ 0، ضيف عليها 250، اطبع القيمة، وبعدين جرّب تعمل let لنفس الحاجة وشوف الفرق.
Create var total starting at 0, add 250, print it, then try the same with let and note the difference.
Starter code:
var total = 0// TODO: add 250 to total// TODO: print total
var total = 0 total += 250 print(total)
Expected output
250
التمرين 2
اكتب دالة photoSizeMB(width:height:) بترجّع حجم الصورة بالميجابايت، بافتراض 3 بايت لكل بكسل، واطبع حجم صورة 4000x3000.
Write photoSizeMB(width:height:) returning the photo size in MB at 3 bytes per pixel, and print the size of a 4000x3000 photo.
Starter code:
func photoSizeMB(width: Int, height: Int) -> Double { // TODO: bytes = width * height * 3, then divide by 1024 * 1024 return 0}print(photoSizeMB(width: 4000, height: 3000))
func photoSizeMB(width: Int, height: Int) -> Double {
let bytes = Double(width * height * 3)
return bytes / (1024 * 1024)
}
print(photoSizeMB(width: 4000, height: 3000))
Expected output
34.332275390625
مشروع صغير في آخر المحاضرة
كام صورة تاخد مكانها في الموبايل؟
الداتا: device-specs: our own table of three devices with free storage in GB (no real user data).
License / source: Our own sample table; Swift language reference by Apple (link only, nothing copied). Reference page
الخطوات
- خد الجدول: تلات أجهزة ومساحة فاضية لكل واحد بالجيجابايت.
- احسب حجم الصورة: 4000 في 3000 بكسل في 3 بايت = 36,000,000 بايت.
- حوّل الجيجابايت لبایت: 1 GB = 1024 في 1024 في 1024.
- اطبع لكل جهاز عدد الصور اللي بتدخل في المساحة الفاضية، والنتيجة المتوقعة تحت.
Starter code:
let phones = [("iPhone SE", 4.0), ("iPhone 15", 12.0), ("iPad Air", 20.0)]let bytesPerPhoto = 4000 * 3000 * 3let bytesPerGB = 1024.0 * 1024.0 * 1024.0 for (name, freeGB) in phones { // TODO: photos = free space in bytes / bytesPerPhoto // TODO: print name and the number of photos}
let phones = [("iPhone SE", 4.0), ("iPhone 15", 12.0), ("iPad Air", 20.0)]
let bytesPerPhoto = 4000 * 3000 * 3
let bytesPerGB = 1024.0 * 1024.0 * 1024.0
for (name, freeGB) in phones {
let photos = Int(freeGB * bytesPerGB) / bytesPerPhoto
print(name + ": " + String(photos) + " photos")
}
Expected output
iPhone SE: 119 photos iPhone 15: 357 photos iPad Air: 596 photos
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).